"use client"; import { useRef, useState } from "react"; import Link from "next/link"; import { Loader2, MailCheck } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"; import { authClient } from "@/lib/auth/auth-client"; import { Turnstile, type TurnstileHandle } from "./turnstile"; export function ForgotPasswordForm({ turnstileSiteKey }: { turnstileSiteKey: string | null }) { const [loading, setLoading] = useState(false); const [sent, setSent] = useState(false); // See sign-in-form: rendered only when configured, verified server-side. const [captchaToken, setCaptchaToken] = useState(""); const turnstileRef = useRef(null); const captchaRequired = !!turnstileSiteKey; async function onSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); const form = new FormData(e.currentTarget); const { error } = await authClient.requestPasswordReset( { email: String(form.get("email")), redirectTo: "/reset-password", }, captchaRequired ? { headers: { "x-captcha-response": captchaToken } } : undefined ); setLoading(false); if (error) { // Single-use token: re-challenge so the retry fails on its real cause. turnstileRef.current?.reset(); toast.error(error.message ?? "Something went wrong"); return; } setSent(true); } if (sent) { return ( Check your inbox If an account exists for that email, we've sent a reset link. ); } return ( Forgot your password? Enter your email and we'll send you a reset link.
{turnstileSiteKey && ( toast.error("Could not load the security check. Please refresh.")} className="flex justify-center" /> )}

Back to sign in

); }