| "use client" |
|
|
| import type React from "react" |
| import { useState } from "react" |
| import { useRouter } from "next/navigation" |
| import { Button } from "@/components/ui/button" |
| import { Input } from "@/components/ui/input" |
| import { Label } from "@/components/ui/label" |
| import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from "@/components/ui/card" |
| import { useToast } from "@/hooks/use-toast" |
| import { Scale, Eye, EyeOff } from "lucide-react" |
| import { apiClient } from "@/lib/api-client" |
| import { setAuthData } from "@/lib/auth-utils" |
| import { useAuth } from "@/context/auth-context" |
|
|
| export function RegisterForm() { |
| const [formData, setFormData] = useState({ |
| name: "", |
| email: "", |
| password: "", |
| confirmPassword: "", |
| }) |
| const [showPassword, setShowPassword] = useState(false) |
| const [showConfirmPassword, setShowConfirmPassword] = useState(false) |
| const [isLoading, setIsLoading] = useState(false) |
| const router = useRouter() |
| const { toast } = useToast() |
| const { login } = useAuth() |
|
|
| const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| setFormData({ ...formData, [e.target.name]: e.target.value }) |
| } |
|
|
| const handleSubmit = async (e: React.FormEvent) => { |
| e.preventDefault() |
|
|
| |
| if (formData.password !== formData.confirmPassword) { |
| toast({ |
| title: "Error", |
| description: "Passwords do not match", |
| variant: "destructive", |
| }) |
| return |
| } |
|
|
| |
| if (formData.password.length < 6) { |
| toast({ |
| title: "Error", |
| description: "Password must be at least 6 characters long", |
| variant: "destructive", |
| }) |
| return |
| } |
|
|
| setIsLoading(true) |
|
|
| try { |
| |
| const response = await apiClient.register( |
| formData.email, |
| formData.password, |
| formData.name |
| ) |
|
|
| console.log("Registration response:", response) |
|
|
| |
| if (response.access_token) { |
| setAuthData(response.access_token, response.refresh_token, response.user) |
|
|
| |
| login(response.access_token) |
|
|
| console.log("Stored user data:", localStorage.getItem("user")) |
|
|
| toast({ |
| title: "Success", |
| description: "Account created successfully! Redirecting...", |
| }) |
|
|
| |
| setTimeout(() => { |
| router.push("/dashboard") |
| }, 1500) |
| } |
| } catch (error) { |
| console.error("Registration error:", error) |
| toast({ |
| title: "Registration Failed", |
| description: error instanceof Error ? error.message : "An error occurred during registration", |
| variant: "destructive", |
| }) |
| } finally { |
| setIsLoading(false) |
| } |
| } |
|
|
| return ( |
| <Card className="w-full max-w-md mx-auto border-primary/20 shadow-xl overflow-hidden"> |
| <CardHeader className="text-center pt-8"> |
| <div className="flex justify-center mb-4"> |
| <div className="h-12 w-12 rounded-full bg-primary/10 flex items-center justify-center text-primary"> |
| <Scale className="h-6 w-6" /> |
| </div> |
| </div> |
| <CardTitle className="text-2xl font-bold">Create Your Account</CardTitle> |
| <CardDescription> |
| Enter your basic information to get started |
| </CardDescription> |
| </CardHeader> |
| |
| <form onSubmit={handleSubmit}> |
| <CardContent className="space-y-4 pt-4"> |
| <div className="space-y-2"> |
| <Label htmlFor="name">Full Name</Label> |
| <Input |
| name="name" |
| id="name" |
| placeholder="John Doe" |
| required |
| value={formData.name} |
| onChange={handleInputChange} |
| /> |
| </div> |
| |
| <div className="space-y-2"> |
| <Label htmlFor="email">Email Address</Label> |
| <Input |
| name="email" |
| id="email" |
| type="email" |
| placeholder="john@example.com" |
| required |
| value={formData.email} |
| onChange={handleInputChange} |
| /> |
| </div> |
| |
| <div className="space-y-2"> |
| <Label htmlFor="password">Password</Label> |
| <div className="relative"> |
| <Input |
| name="password" |
| id="password" |
| type={showPassword ? "text" : "password"} |
| required |
| value={formData.password} |
| onChange={handleInputChange} |
| className="pr-10" |
| /> |
| <button |
| type="button" |
| onClick={() => setShowPassword(!showPassword)} |
| className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground" |
| > |
| {showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />} |
| </button> |
| </div> |
| </div> |
| |
| <div className="space-y-2"> |
| <Label htmlFor="confirmPassword">Confirm Password</Label> |
| <div className="relative"> |
| <Input |
| name="confirmPassword" |
| id="confirmPassword" |
| type={showConfirmPassword ? "text" : "password"} |
| required |
| value={formData.confirmPassword} |
| onChange={handleInputChange} |
| className="pr-10" |
| /> |
| <button |
| type="button" |
| onClick={() => setShowConfirmPassword(!showConfirmPassword)} |
| className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground" |
| > |
| {showConfirmPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />} |
| </button> |
| </div> |
| </div> |
| </CardContent> |
| |
| <CardFooter className="flex justify-center pb-8"> |
| <Button type="submit" className="w-full bg-primary hover:bg-primary/90" disabled={isLoading}> |
| {isLoading ? "Creating Account..." : "Create Account"} |
| </Button> |
| </CardFooter> |
| </form> |
| </Card> |
| ) |
| } |
|
|