'use client'; import { FormEvent, useState } from 'react'; export function RecoveryForm() { const [email, setEmail] = useState(''); const [token, setToken] = useState(''); const [password, setPassword] = useState(''); const [message, setMessage] = useState(''); const [busy, setBusy] = useState(false); async function submitRequest(event: FormEvent) { event.preventDefault(); setBusy(true); setMessage(''); try { const response = await fetch('/api/auth/password-reset-request', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ email }) }); const body = await response.json().catch(() => ({})); if (!response.ok) throw new Error(body.detail ?? 'Unable to request reset.'); setMessage('If the account exists, reset instructions have been sent.'); } catch (error) { setMessage(error instanceof Error ? error.message : 'Unable to request reset.'); } finally { setBusy(false); } } async function submitReset(event: FormEvent) { event.preventDefault(); setBusy(true); setMessage(''); try { const response = await fetch('/api/auth/password-reset', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ token, password }) }); const body = await response.json().catch(() => ({})); if (!response.ok) throw new Error(body.detail ?? 'Unable to reset password.'); setMessage('Password reset complete. You can sign in now.'); setToken(''); setPassword(''); } catch (error) { setMessage(error instanceof Error ? error.message : 'Unable to reset password.'); } finally { setBusy(false); } } return
{message}
}