import { useEffect, useState } from "react"; import { useNavigate, useSearchParams } from "react-router-dom"; import { authClient } from "../lib/auth-client.ts"; export function VerifyEmail() { const [searchParams] = useSearchParams(); const navigate = useNavigate(); const [status, setStatus] = useState<"verifying" | "success" | "error">("verifying"); const [resendEmail, setResendEmail] = useState(""); const [showResend, setShowResend] = useState(false); const [resending, setResending] = useState(false); const [resendMessage, setResendMessage] = useState(""); useEffect(() => { const token = searchParams.get("token"); const callbackURL = searchParams.get("callbackURL") || "/"; if (!token) { setStatus("error"); return; } authClient.verifyEmail({ query: { token } }) .then(() => { setStatus("success"); setTimeout(() => { navigate(callbackURL); }, 2000); }) .catch(() => { setStatus("error"); }); }, [searchParams, navigate]); async function handleResend() { if (!resendEmail) { setResendMessage("Please enter your email address."); return; } setResending(true); setResendMessage(""); try { const { error } = await authClient.sendVerificationEmail({ email: resendEmail }); if (error) { setResendMessage("Failed to resend. Please try again."); } else { setResendMessage("Verification email sent!"); setShowResend(false); } } finally { setResending(false); } } return (
Please wait while we verify your email address.
> )} {status === "success" && ( <>Redirecting you shortly...
> )} {status === "error" && ( <>The verification link may have expired or is invalid.
{!showResend ? ( ) : ({resendMessage}
)}