|
| 1 | +import { Button } from '@/components/ui/button' |
| 2 | +import { |
| 3 | + Card, |
| 4 | + CardContent, |
| 5 | + CardDescription, |
| 6 | + CardHeader, |
| 7 | + CardTitle, |
| 8 | +} from '@/components/ui/card' |
| 9 | +import { |
| 10 | + Field, |
| 11 | + FieldDescription, |
| 12 | + FieldError, |
| 13 | + FieldGroup, |
| 14 | + FieldLabel, |
| 15 | +} from '@/components/ui/field' |
| 16 | +import { |
| 17 | + InputOTP, |
| 18 | + InputOTPGroup, |
| 19 | + InputOTPSlot, |
| 20 | +} from '@/components/ui/input-otp' |
| 21 | +import { api } from '@/lib/api' |
| 22 | +import { useMutation, useQueryClient } from '@tanstack/react-query' |
| 23 | +import { useForm } from '@tanstack/react-form' |
| 24 | +import { z } from 'zod' |
| 25 | +import { useNavigate, useRouter } from '@tanstack/react-router' |
| 26 | +import { router } from 'better-auth/api' |
| 27 | + |
| 28 | +const otpSchema = z.string().length(6, 'Please enter the 6-digit code') |
| 29 | + |
| 30 | +interface OTPFormProps extends React.ComponentProps<typeof Card> { |
| 31 | + email: string |
| 32 | +} |
| 33 | + |
| 34 | +export function OTPForm({ email, ...props }: OTPFormProps) { |
| 35 | + const navigate = useNavigate() |
| 36 | + const queryClient = useQueryClient() |
| 37 | + const router = useRouter() |
| 38 | + |
| 39 | + const verifyOtpMutation = useMutation({ |
| 40 | + mutationFn: async (otp: string) => api.auth.signInWithOtp(email, otp), |
| 41 | + onSuccess: async () => { |
| 42 | + await queryClient.invalidateQueries() |
| 43 | + router.invalidate() |
| 44 | + navigate({ to: '/' }) |
| 45 | + }, |
| 46 | + }) |
| 47 | + |
| 48 | + const form = useForm({ |
| 49 | + defaultValues: { otp: '' }, |
| 50 | + onSubmit: async ({ value }) => verifyOtpMutation.mutateAsync(value.otp), |
| 51 | + }) |
| 52 | + |
| 53 | + return ( |
| 54 | + <Card {...props}> |
| 55 | + <CardHeader> |
| 56 | + <CardTitle>Enter verification code</CardTitle> |
| 57 | + <CardDescription>We sent a 6-digit code to {email}.</CardDescription> |
| 58 | + </CardHeader> |
| 59 | + <CardContent> |
| 60 | + <FieldGroup> |
| 61 | + <form.Field |
| 62 | + name="otp" |
| 63 | + validators={{ |
| 64 | + onBlur: ({ value }) => { |
| 65 | + const result = otpSchema.safeParse(value) |
| 66 | + return result.success |
| 67 | + ? undefined |
| 68 | + : result.error.issues[0]?.message |
| 69 | + }, |
| 70 | + }} |
| 71 | + > |
| 72 | + {(field) => ( |
| 73 | + <Field data-invalid={field.state.meta.errors.length > 0}> |
| 74 | + <FieldLabel htmlFor={field.name}>Verification code</FieldLabel> |
| 75 | + <InputOTP |
| 76 | + maxLength={6} |
| 77 | + id={field.name} |
| 78 | + value={field.state.value} |
| 79 | + onChange={(value) => field.handleChange(value)} |
| 80 | + onBlur={field.handleBlur} |
| 81 | + > |
| 82 | + <InputOTPGroup className="gap-2.5 *:data-[slot=input-otp-slot]:rounded-md *:data-[slot=input-otp-slot]:border"> |
| 83 | + <InputOTPSlot index={0} /> |
| 84 | + <InputOTPSlot index={1} /> |
| 85 | + <InputOTPSlot index={2} /> |
| 86 | + <InputOTPSlot index={3} /> |
| 87 | + <InputOTPSlot index={4} /> |
| 88 | + <InputOTPSlot index={5} /> |
| 89 | + </InputOTPGroup> |
| 90 | + </InputOTP> |
| 91 | + <FieldError |
| 92 | + errors={field.state.meta.errors.map((e) => ({ |
| 93 | + message: typeof e === 'string' ? e : undefined, |
| 94 | + }))} |
| 95 | + /> |
| 96 | + <FieldDescription> |
| 97 | + Enter the 6-digit code sent to your email. |
| 98 | + </FieldDescription> |
| 99 | + </Field> |
| 100 | + )} |
| 101 | + </form.Field> |
| 102 | + <FieldGroup> |
| 103 | + <Button |
| 104 | + onClick={() => form.handleSubmit()} |
| 105 | + disabled={verifyOtpMutation.isPending || !form.state.canSubmit} |
| 106 | + > |
| 107 | + {verifyOtpMutation.isPending ? 'Verifying...' : 'Verify'} |
| 108 | + </Button> |
| 109 | + <FieldDescription className="text-center"> |
| 110 | + Didn't receive the code? <a href="#">Resend</a> |
| 111 | + </FieldDescription> |
| 112 | + </FieldGroup> |
| 113 | + </FieldGroup> |
| 114 | + </CardContent> |
| 115 | + </Card> |
| 116 | + ) |
| 117 | +} |
0 commit comments