import { useState } from 'react' interface Props { valid: boolean token: string resetUrl: string recoverUrl: string csrfToken: string } export function ResetPassword({ valid, token, resetUrl, recoverUrl, csrfToken }: Props) { const [password, setPassword] = useState('') const [confPassword, setConfPassword] = useState('') const [showPw, setShowPw] = useState(false) const [busy, setBusy] = useState(false) const [message, setMessage] = useState<{ ok: boolean; text: string } | null>(null) const [done, setDone] = useState(false) if (!valid) { return (
) } async function handleSubmit(e: React.FormEvent) { e.preventDefault() if (busy || done) return if (password.trim() === '' || password.trim() !== confPassword.trim()) { setMessage({ ok: false, text: 'Las contraseñas no coinciden.' }) return } setBusy(true) setMessage(null) const body = new URLSearchParams() body.set('token', token) body.set('new-password', password.trim()) body.set('conf-password', confPassword.trim()) body.set('csrfmiddlewaretoken', csrfToken) try { const resp = await fetch(resetUrl, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'X-CSRFToken': csrfToken, Accept: 'application/json', }, credentials: 'same-origin', body: body.toString(), }) if (!resp.ok) throw new Error(`HTTP ${resp.status}`) const data: { success?: boolean; message?: string; redirect?: string } = await resp.json() setMessage({ ok: !!data.success, text: data.message || '' }) if (data.success) { setDone(true) if (data.redirect) { setTimeout(() => { window.location.href = data.redirect as string }, 2500) } } else { setBusy(false) } } catch { setMessage({ ok: false, text: 'Error en el servidor. Inténtalo de nuevo más tarde.' }) setBusy(false) } } return (