import { useRef } from 'react'; import { Link } from '@tanstack/react-router'; import LoginFormContainer from '@/components/auth/LoginFormContainer'; import { useAppForm } from '@/components/form'; import type { InvisibleRecaptchaHandle } from '@/components/elements/InvisibleRecaptcha'; import InvisibleRecaptcha from '@/components/elements/InvisibleRecaptcha'; import { useSiteSettings } from '@/api/settings/queries'; import { useRequestPasswordResetEmail } from '@/api/auth/queries'; import { toast } from 'sonner'; const isEmail = (value: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value); export default function ForgotPasswordContainer() { const recaptchaRef = useRef(null); const requestReset = useRequestPasswordResetEmail(); const { enabled: recaptchaEnabled, siteKey } = useSiteSettings().recaptcha; const form = useAppForm({ defaultValues: { email: '' }, onSubmit: async ({ value }) => { let token = ''; if (recaptchaEnabled) { token = (await recaptchaRef.current?.execute()) ?? ''; if (!token) { toast.error('Captcha verification failed, please try again.'); return; } } try { await requestReset.mutateAsync({ body: { email: value.email, 'g-recaptcha-response': token, }, }); form.reset(); } catch { // Error toast is handled by the mutation. } }, }); return ( isEmail(value) ? undefined : 'A valid email address must be provided to continue.', }} > {(field) => ( )}
Send Email
{recaptchaEnabled && }
Return to Login
); }