register-form.tsx135 lines, 3.5 KB
| 1 | 'use client'; |
| 2 | |
| 3 | import { useForm } from 'react-hook-form'; |
| 4 | import { zodResolver } from '@hookform/resolvers/zod'; |
| 5 | import Link from 'next/link'; |
| 6 | import { Button } from '@/components/ui/button'; |
| 7 | import { Input } from '@/components/ui/input'; |
| 8 | import { |
| 9 | Form, |
| 10 | FormControl, |
| 11 | FormField, |
| 12 | FormItem, |
| 13 | FormLabel, |
| 14 | FormMessage, |
| 15 | } from '@/components/ui/form'; |
| 16 | import { useRegister } from '../hooks/use-auth'; |
| 17 | import { registerSchema, type RegisterFormData } from '../types'; |
| 18 | |
| 19 | export function RegisterForm() { |
| 20 | const { mutate: register, isPending } = useRegister(); |
| 21 | |
| 22 | const form = useForm<RegisterFormData>({ |
| 23 | resolver: zodResolver(registerSchema), |
| 24 | defaultValues: { |
| 25 | name: '', |
| 26 | email: '', |
| 27 | password: '', |
| 28 | confirmPassword: '', |
| 29 | }, |
| 30 | }); |
| 31 | |
| 32 | function onSubmit(data: RegisterFormData) { |
| 33 | register({ |
| 34 | name: data.name, |
| 35 | email: data.email, |
| 36 | password: data.password, |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | return ( |
| 41 | <Form {...form}> |
| 42 | <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 43 | <FormField |
| 44 | control={form.control} |
| 45 | name="name" |
| 46 | render={({ field }) => ( |
| 47 | <FormItem> |
| 48 | <FormLabel>Name</FormLabel> |
| 49 | <FormControl> |
| 50 | <Input |
| 51 | type="text" |
| 52 | placeholder="Your name" |
| 53 | autoComplete="name" |
| 54 | {...field} |
| 55 | /> |
| 56 | </FormControl> |
| 57 | <FormMessage /> |
| 58 | </FormItem> |
| 59 | )} |
| 60 | /> |
| 61 | |
| 62 | <FormField |
| 63 | control={form.control} |
| 64 | name="email" |
| 65 | render={({ field }) => ( |
| 66 | <FormItem> |
| 67 | <FormLabel>Email</FormLabel> |
| 68 | <FormControl> |
| 69 | <Input |
| 70 | type="email" |
| 71 | placeholder="you@example.com" |
| 72 | autoComplete="email" |
| 73 | {...field} |
| 74 | /> |
| 75 | </FormControl> |
| 76 | <FormMessage /> |
| 77 | </FormItem> |
| 78 | )} |
| 79 | /> |
| 80 | |
| 81 | <FormField |
| 82 | control={form.control} |
| 83 | name="password" |
| 84 | render={({ field }) => ( |
| 85 | <FormItem> |
| 86 | <FormLabel>Password</FormLabel> |
| 87 | <FormControl> |
| 88 | <Input |
| 89 | type="password" |
| 90 | placeholder="your password" |
| 91 | autoComplete="new-password" |
| 92 | {...field} |
| 93 | /> |
| 94 | </FormControl> |
| 95 | <FormMessage /> |
| 96 | </FormItem> |
| 97 | )} |
| 98 | /> |
| 99 | |
| 100 | <FormField |
| 101 | control={form.control} |
| 102 | name="confirmPassword" |
| 103 | render={({ field }) => ( |
| 104 | <FormItem> |
| 105 | <FormLabel>Confirm Password</FormLabel> |
| 106 | <FormControl> |
| 107 | <Input |
| 108 | type="password" |
| 109 | placeholder="confirm your password" |
| 110 | autoComplete="new-password" |
| 111 | {...field} |
| 112 | /> |
| 113 | </FormControl> |
| 114 | <FormMessage /> |
| 115 | </FormItem> |
| 116 | )} |
| 117 | /> |
| 118 | |
| 119 | <Button type="submit" className="w-full" disabled={isPending}> |
| 120 | {isPending ? 'Signing up...' : 'Create Account'} |
| 121 | </Button> |
| 122 | |
| 123 | <p className="text-center text-sm text-zinc-600 dark:text-zinc-400"> |
| 124 | Already have an account?{' '} |
| 125 | <Link |
| 126 | href="/login" |
| 127 | className="font-medium text-blue-600 hover:underline" |
| 128 | > |
| 129 | Sign in |
| 130 | </Link> |
| 131 | </p> |
| 132 | </form> |
| 133 | </Form> |
| 134 | ); |
| 135 | } |