"use client"; import { ArrowLeftIcon, KeyRoundIcon, MailCheckIcon } from "lucide-react"; import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import { type FormEvent, useEffect, useId, useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { cn } from "@/lib/utils"; export interface ForgotPasswordProps { defaultEmail?: string; resendAfter?: number; onSend?: (email: string) => Promise; onSignIn?: () => void; className?: string; } const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/; async function demoSend() { await new Promise((resolve) => setTimeout(resolve, 900)); } const linkClass = "cursor-pointer rounded-sm font-medium text-foreground underline-offset-4 outline-none hover:underline focus-visible:ring-2 focus-visible:ring-ring"; export function ForgotPassword({ defaultEmail = "", resendAfter = 30, onSend = demoSend, onSignIn, className }: ForgotPasswordProps) { const id = useId(); const reduce = useReducedMotion(); const [email, setEmail] = useState(defaultEmail); const [busy, setBusy] = useState(false); const [tried, setTried] = useState(false); const [sent, setSent] = useState(false); const [left, setLeft] = useState(0); useEffect(() => { if (left <= 0) return; const timer = setTimeout(() => setLeft((value) => value - 1), 1000); return () => clearTimeout(timer); }, [left]); const valid = EMAIL.test(email.trim()); const error = tried && !valid ? "Enter the email you signed up with." : null; const send = async (event?: FormEvent) => { event?.preventDefault(); setTried(true); if (!valid || busy) return; setBusy(true); await onSend(email.trim()); setBusy(false); setSent(true); setLeft(resendAfter); }; return (
{sent ? : }

{sent ? "Check your email" : "Forgot your password?"}

{sent ? ( <> We sent a reset link to {email.trim()}. It expires in 30 minutes. ) : ( "Enter your email and we’ll send you a link to reset it." )}

{sent ? (

{left > 0 ? ( <> Didn’t get it? Resend in 0:{String(left).padStart(2, "0")} ) : ( <> Didn’t get it?{" "} {" "} or{" "} )}

) : (
setEmail(event.target.value)} placeholder="you@company.com" type="email" value={email} /> {error && (

{error}

)}
)}

Remembered it?{" "}

); }