import { useState, useEffect } from "react"; import { useSearchParams, Link } from "react-router-dom"; import { Mail, CheckCircle, XCircle, Loader2, ArrowRight, RefreshCw } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { BannerAlert } from "@/components/auth/BannerAlert"; import { api, ApiError } from "@/lib/api"; type VerificationState = "verifying" | "success" | "error" | "resend"; export default function VerifyEmailPage() { const [searchParams] = useSearchParams(); const token = searchParams.get("token"); const [state, setState] = useState(token ? "verifying" : "resend"); const [errorMessage, setErrorMessage] = useState(""); const [resendEmail, setResendEmail] = useState(""); const [isResending, setIsResending] = useState(false); const [resendSuccess, setResendSuccess] = useState(false); useEffect(() => { if (token && state === "verifying") { verifyToken(token); } }, [token]); const verifyToken = async (verificationToken: string) => { try { await api.auth.verifyEmail(verificationToken); setState("success"); } catch (err) { setState("error"); if (err instanceof ApiError) { setErrorMessage(err.message || "This verification link has expired or is invalid."); } else { setErrorMessage("This verification link has expired or is invalid."); } } }; const handleResendVerification = async (e: React.FormEvent) => { e.preventDefault(); setIsResending(true); setResendSuccess(false); try { await api.auth.resendVerification(resendEmail); } catch { // Always show success to avoid leaking account existence } finally { setIsResending(false); setResendSuccess(true); } }; // Loading / Verifying state if (state === "verifying") { return (

Verifying your email

Please wait while we confirm your email address...

); } // Success state if (state === "success") { return (

Email verified

Your email has been successfully verified. You can now sign in to your account.

); } // Error state if (state === "error") { return (

Verification failed

{errorMessage}

); } // Resend verification form return (

Resend verification

Enter your email to receive a new verification link

{resendSuccess && ( )}
setResendEmail(e.target.value)} className="pl-10" required autoComplete="email" />

Back to sign in

); }