register-form.tsx166 lines, 4.4 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { zodResolver } from "@hookform/resolvers/zod"; |
| 4 | import { useRouter } from "next/navigation"; |
| 5 | import { useState } from "react"; |
| 6 | import { useForm } from "react-hook-form"; |
| 7 | import { z } from "zod"; |
| 8 | import { Button } from "../ui/button"; |
| 9 | import { |
| 10 | Form, |
| 11 | FormControl, |
| 12 | FormField, |
| 13 | FormItem, |
| 14 | FormLabel, |
| 15 | FormMessage, |
| 16 | } from "../ui/form"; |
| 17 | import { Input } from "../ui/input"; |
| 18 | import { Loader2 } from "lucide-react"; |
| 19 | import { registerUser } from "@/server/actions/auth/register"; |
| 20 | |
| 21 | const registerSchema = z |
| 22 | .object({ |
| 23 | name: z.string().min(2), |
| 24 | email: z.string().email(), |
| 25 | password: z.string().min(6), |
| 26 | confirmPassword: z.string().min(6), |
| 27 | }) |
| 28 | .refine((data) => data.password === data.confirmPassword, { |
| 29 | message: "Passwords do not match", |
| 30 | path: ["confirmPassword"], |
| 31 | }); |
| 32 | |
| 33 | type RegisterFormValues = z.infer<typeof registerSchema>; |
| 34 | |
| 35 | export function RegisterForm() { |
| 36 | const router = useRouter(); |
| 37 | const [isLoading, setIsLoading] = useState(false); |
| 38 | const [error, setError] = useState<string | null>(null); |
| 39 | |
| 40 | const form = useForm<RegisterFormValues>({ |
| 41 | resolver: zodResolver(registerSchema), |
| 42 | defaultValues: { |
| 43 | name: "", |
| 44 | email: "", |
| 45 | password: "", |
| 46 | confirmPassword: "", |
| 47 | }, |
| 48 | }); |
| 49 | |
| 50 | async function onSubmit(data: RegisterFormValues) { |
| 51 | setIsLoading(true); |
| 52 | setError(null); |
| 53 | |
| 54 | try { |
| 55 | const formData = new FormData(); |
| 56 | formData.append("name", data.name); |
| 57 | formData.append("email", data.email); |
| 58 | formData.append("password", data.password); |
| 59 | |
| 60 | const response = await registerUser(formData); |
| 61 | |
| 62 | if (!response.success) { |
| 63 | setError(response.error || "An error occurred. Please try again."); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | router.push("/login?registered=true"); |
| 68 | } catch (error) { |
| 69 | setError("An error occurred. Please try again."); |
| 70 | console.error(error); |
| 71 | } finally { |
| 72 | setIsLoading(false); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return ( |
| 77 | <Form {...form}> |
| 78 | <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 79 | <FormField |
| 80 | control={form.control} |
| 81 | name="name" |
| 82 | render={({ field }) => ( |
| 83 | <FormItem> |
| 84 | <FormLabel>Name</FormLabel> |
| 85 | <FormControl> |
| 86 | <Input |
| 87 | placeholder="John Doe" |
| 88 | autoComplete="name" |
| 89 | disabled={isLoading} |
| 90 | {...field} |
| 91 | /> |
| 92 | </FormControl> |
| 93 | <FormMessage /> |
| 94 | </FormItem> |
| 95 | )} |
| 96 | /> |
| 97 | <FormField |
| 98 | control={form.control} |
| 99 | name="email" |
| 100 | render={({ field }) => ( |
| 101 | <FormItem> |
| 102 | <FormLabel>Email</FormLabel> |
| 103 | <FormControl> |
| 104 | <Input |
| 105 | placeholder="example@example.com" |
| 106 | type="email" |
| 107 | autoComplete="email" |
| 108 | disabled={isLoading} |
| 109 | {...field} |
| 110 | /> |
| 111 | </FormControl> |
| 112 | <FormMessage /> |
| 113 | </FormItem> |
| 114 | )} |
| 115 | /> |
| 116 | <FormField |
| 117 | control={form.control} |
| 118 | name="password" |
| 119 | render={({ field }) => ( |
| 120 | <FormItem> |
| 121 | <FormLabel>Password</FormLabel> |
| 122 | <FormControl> |
| 123 | <Input |
| 124 | placeholder="********" |
| 125 | type="password" |
| 126 | autoComplete="current-password" |
| 127 | disabled={isLoading} |
| 128 | {...field} |
| 129 | /> |
| 130 | </FormControl> |
| 131 | <FormMessage /> |
| 132 | </FormItem> |
| 133 | )} |
| 134 | /> |
| 135 | <FormField |
| 136 | control={form.control} |
| 137 | name="confirmPassword" |
| 138 | render={({ field }) => ( |
| 139 | <FormItem> |
| 140 | <FormLabel>Confirm Password</FormLabel> |
| 141 | <FormControl> |
| 142 | <Input |
| 143 | placeholder="********" |
| 144 | type="password" |
| 145 | autoComplete="new-password" |
| 146 | disabled={isLoading} |
| 147 | {...field} |
| 148 | /> |
| 149 | </FormControl> |
| 150 | <FormMessage /> |
| 151 | </FormItem> |
| 152 | )} |
| 153 | /> |
| 154 | |
| 155 | {error && ( |
| 156 | <div className="text-sm text-destructive font-medium">{error}</div> |
| 157 | )} |
| 158 | |
| 159 | <Button type="submit" className="w-full" disabled={isLoading}> |
| 160 | {isLoading && <Loader2 className="mr-2 size-4 animate-spin" />} |
| 161 | Create Account |
| 162 | </Button> |
| 163 | </form> |
| 164 | </Form> |
| 165 | ); |
| 166 | } |