page.tsx175 lines, 5.0 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | registerSchema, |
| 5 | type RegisterSchemaData, |
| 6 | } from "@/lib/validations/auth"; |
| 7 | import { useForm } from "react-hook-form"; |
| 8 | import { zodResolver } from "@hookform/resolvers/zod"; |
| 9 | import { useState } from "react"; |
| 10 | import { Alert, AlertDescription } from "@/components/ui/alert"; |
| 11 | import { |
| 12 | Form, |
| 13 | FormControl, |
| 14 | FormField, |
| 15 | FormItem, |
| 16 | FormLabel, |
| 17 | FormMessage, |
| 18 | } from "@/components/ui/form"; |
| 19 | import { Input } from "@/components/ui/input"; |
| 20 | import { Button } from "@/components/ui/button"; |
| 21 | import { useRegister } from "@/lib/hooks/queries/auth"; |
| 22 | import { toast } from "sonner"; |
| 23 | import { Loader2 } from "lucide-react"; |
| 24 | import Link from "next/link"; |
| 25 | |
| 26 | export default function RegisterPage() { |
| 27 | const register = useRegister(); |
| 28 | const [error, setError] = useState<string | null>(null); |
| 29 | |
| 30 | const form = useForm<RegisterSchemaData>({ |
| 31 | resolver: zodResolver(registerSchema), |
| 32 | defaultValues: { |
| 33 | name: "", |
| 34 | email: "", |
| 35 | password: "", |
| 36 | confirmPassword: "", |
| 37 | }, |
| 38 | }); |
| 39 | |
| 40 | const onSubmit = async (data: RegisterSchemaData) => { |
| 41 | try { |
| 42 | setError(null); |
| 43 | await register.mutateAsync({ |
| 44 | name: data.name, |
| 45 | email: data.email, |
| 46 | password: data.password, |
| 47 | }); |
| 48 | toast.success("Account created successfully"); |
| 49 | } catch (error: Error | unknown) { |
| 50 | const responseData = (error as { response?: { data?: any } })?.response |
| 51 | ?.data; |
| 52 | if (responseData) { |
| 53 | // Display the specific error message from the server |
| 54 | if (responseData.message) { |
| 55 | setError(responseData.message); |
| 56 | } else if (responseData.errors && responseData.errors.length > 0) { |
| 57 | // If there are multiple validation errors, display the first one |
| 58 | setError(responseData.errors[0].msg); |
| 59 | } else if (responseData.error) { |
| 60 | setError(responseData.error); |
| 61 | } else { |
| 62 | setError("Something went wrong. Please try again."); |
| 63 | } |
| 64 | } else { |
| 65 | setError("Unable to connect to the server. Please try again later."); |
| 66 | } |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | return ( |
| 71 | <div className="flex flex-col space-y-6"> |
| 72 | <div className="flex flex-col space-y-2 text-center"> |
| 73 | <h1 className="text-2xl font-semibold tracking-tight"> |
| 74 | Create an account |
| 75 | </h1> |
| 76 | <p className="text-sm text-muted-foreground"> |
| 77 | Enter your details below to create an account |
| 78 | </p> |
| 79 | </div> |
| 80 | |
| 81 | {error && ( |
| 82 | <Alert variant={"destructive"} className="text-sm"> |
| 83 | <AlertDescription>{error}</AlertDescription> |
| 84 | </Alert> |
| 85 | )} |
| 86 | |
| 87 | <Form {...form}> |
| 88 | <form |
| 89 | onSubmit={(e) => { |
| 90 | e.preventDefault(); |
| 91 | form.handleSubmit(onSubmit)(e); |
| 92 | }} |
| 93 | className="space-y-4" |
| 94 | > |
| 95 | <FormField |
| 96 | control={form.control} |
| 97 | name="name" |
| 98 | render={({ field }) => ( |
| 99 | <FormItem> |
| 100 | <FormLabel>Name</FormLabel> |
| 101 | <FormControl> |
| 102 | <Input placeholder="John Doe" {...field} /> |
| 103 | </FormControl> |
| 104 | <FormMessage className="text-xs" /> |
| 105 | </FormItem> |
| 106 | )} |
| 107 | /> |
| 108 | |
| 109 | <FormField |
| 110 | control={form.control} |
| 111 | name="email" |
| 112 | render={({ field }) => ( |
| 113 | <FormItem> |
| 114 | <FormLabel>Email</FormLabel> |
| 115 | <FormControl> |
| 116 | <Input placeholder="john@doe.com" {...field} /> |
| 117 | </FormControl> |
| 118 | <FormMessage className="text-xs" /> |
| 119 | </FormItem> |
| 120 | )} |
| 121 | /> |
| 122 | |
| 123 | <FormField |
| 124 | control={form.control} |
| 125 | name="password" |
| 126 | render={({ field }) => ( |
| 127 | <FormItem> |
| 128 | <FormLabel>Password</FormLabel> |
| 129 | <FormControl> |
| 130 | <Input placeholder="********" type="password" {...field} /> |
| 131 | </FormControl> |
| 132 | <FormMessage className="text-xs" /> |
| 133 | </FormItem> |
| 134 | )} |
| 135 | /> |
| 136 | |
| 137 | <FormField |
| 138 | control={form.control} |
| 139 | name="confirmPassword" |
| 140 | render={({ field }) => ( |
| 141 | <FormItem> |
| 142 | <FormLabel>Confirm Password</FormLabel> |
| 143 | <FormControl> |
| 144 | <Input placeholder="********" type="password" {...field} /> |
| 145 | </FormControl> |
| 146 | <FormMessage className="text-xs" /> |
| 147 | </FormItem> |
| 148 | )} |
| 149 | /> |
| 150 | |
| 151 | <div className="flex items-center justify-end"> |
| 152 | <Link |
| 153 | href="/auth/login" |
| 154 | className="text-sm text-muted-foreground hover:text-primary" |
| 155 | > |
| 156 | Already have an account? Sign in |
| 157 | </Link> |
| 158 | </div> |
| 159 | |
| 160 | <Button |
| 161 | type="submit" |
| 162 | className="w-full" |
| 163 | disabled={register.isPending} |
| 164 | > |
| 165 | {register.isPending ? ( |
| 166 | <Loader2 className="size-4 animate-spin" /> |
| 167 | ) : ( |
| 168 | "Register" |
| 169 | )} |
| 170 | </Button> |
| 171 | </form> |
| 172 | </Form> |
| 173 | </div> |
| 174 | ); |
| 175 | } |