"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import Link from "next/link"; import { useRouter, useSearchParams } from "next/navigation"; import { Suspense, useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { Loader2, MailCheck } from "lucide-react"; import { z } from "zod"; import { FormField } from "@/components/ui/form-field"; import { useCountdown } from "@/hooks/use-countdown"; import { authErrorMessage, retryAfterSeconds, useCurrentUser, useResendVerification, useVerifyEmail, } from "@/hooks/use-auth"; const verifySchema = z.object({ code: z .string() .length(6, "Enter the 6-digit code") .regex(/^\d{6}$/, "Digits only"), }); type VerifyForm = z.infer; function VerifyEmailCard() { const router = useRouter(); const searchParams = useSearchParams(); const email = searchParams.get("email") ?? ""; const verifyMutation = useVerifyEmail(); const resendMutation = useResendVerification(); const { data: currentUser } = useCurrentUser(); const countdown = useCountdown(); const [resent, setResent] = useState(false); const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(verifySchema) }); useEffect(() => { if (currentUser) { router.replace("/dashboard"); } }, [currentUser, router]); const onSubmit = handleSubmit(async (values) => { try { await verifyMutation.mutateAsync({ email, code: values.code }); router.push("/login?verified=1"); } catch { // Surfaced via verifyMutation.isError below - swallow here so a // wrong/expired code doesn't also trip Next's unhandled-rejection // dev overlay on top of the inline error message. } }); const onResend = async () => { setResent(false); try { await resendMutation.mutateAsync({ email }); setResent(true); countdown.start(30); } catch (error) { const retryAfter = retryAfterSeconds(error); if (retryAfter) countdown.start(retryAfter); } }; return (

Verify your email

{email ? ( <> We sent a 6-digit code to {email}. ) : ( "Enter the 6-digit code we emailed you." )}

{ e.target.value = e.target.value.replace(/\D/g, "").slice(0, 6); }, })} error={errors.code?.message} /> {verifyMutation.isError && (

{authErrorMessage(verifyMutation.error)}

)}
{resent && countdown.remaining === 0 && !resendMutation.isPending && (

Code resent - check your inbox.

)}

Back to sign in

); } export default function VerifyEmailPage() { return (
); }