fea1316f3c
- Las 4 opciones exigían Gmail (`GMAIL_RE`), así que las cuentas ya creadas no podían recuperarse: de las 17 bnet que existen, 16 NO son de Gmail. Ahora solo se valida que sea un correo (`EMAIL_RE`), en un único sitio antes del tipo. El REGISTRO sigue exigiendo Gmail: eso no se toca. - «Contraseña» se pedía por nombre de usuario («15#1»); ahora por correo, como las otras tres. El campo del formulario deja de alternar texto/correo. En el flujo de contraseña, el correo que se guarda en `passwordreset` se toma de la BD y NO del tecleado: el verifier SRP6 de bnet se deriva del correo, así que `resetPassword` necesita exactamente la misma forma (MAYÚSCULAS) o generaría un verifier con el que no se podría entrar. Quedan sin uso y se borran: `usesUsername`, la clave de error `invalidUsername` y el texto `username`. `Recover.invalidEmail` decía «Introduce un correo de Gmail válido» y pasa a ser genérico. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
118 lines
4.0 KiB
TypeScript
118 lines
4.0 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { useTranslations } from 'next-intl'
|
|
import { Turnstile } from '@/components/Turnstile'
|
|
|
|
const ERROR_KEYS = ['invalidEmail', 'captchaFailed'] as const
|
|
type RecoverType = 'password' | 'accountname' | 'securitytoken' | 'activation'
|
|
|
|
export function RecoverForm() {
|
|
const t = useTranslations('Recover')
|
|
const [type, setType] = useState<RecoverType>('password')
|
|
const [value, setValue] = useState('')
|
|
const [captcha, setCaptcha] = useState('')
|
|
const [captchaKey, setCaptchaKey] = useState(0) // remonta el widget para pedir un token nuevo
|
|
const [busy, setBusy] = useState(false)
|
|
const [sent, setSent] = useState(false)
|
|
const [message, setMessage] = useState<{ ok: boolean; text: string } | null>(null)
|
|
|
|
function changeType(newType: RecoverType) {
|
|
setType(newType)
|
|
setValue('')
|
|
setMessage(null)
|
|
setSent(false)
|
|
setCaptcha('')
|
|
setCaptchaKey((k) => k + 1)
|
|
}
|
|
|
|
function resetCaptcha() {
|
|
setCaptcha('')
|
|
setCaptchaKey((k) => k + 1)
|
|
}
|
|
|
|
async function handleSubmit(e: React.FormEvent) {
|
|
e.preventDefault()
|
|
if (busy || sent) return
|
|
setBusy(true)
|
|
setMessage(null)
|
|
try {
|
|
const res = await fetch('/api/auth/recover', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ type, value: value.trim(), turnstileToken: captcha }),
|
|
})
|
|
const data: { success?: boolean; error?: string } = await res.json()
|
|
if (data.success) {
|
|
setMessage({ ok: true, text: t('success') })
|
|
setSent(true)
|
|
// Como el original: tras 5 s se restaura el botón y se limpia el formulario.
|
|
setTimeout(() => {
|
|
setSent(false)
|
|
setValue('')
|
|
setMessage(null)
|
|
resetCaptcha()
|
|
}, 5000)
|
|
} else {
|
|
const key = (ERROR_KEYS as readonly string[]).includes(data.error ?? '') ? data.error! : 'genericError'
|
|
setMessage({ ok: false, text: t(key) })
|
|
setTimeout(() => {
|
|
setMessage(null)
|
|
resetCaptcha()
|
|
}, 5000)
|
|
}
|
|
} catch {
|
|
setMessage({ ok: false, text: t('genericError') })
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<p>{t('prompt')}</p>
|
|
<br />
|
|
<form noValidate onSubmit={handleSubmit} acceptCharset="utf-8">
|
|
<table className="middle-center-table">
|
|
<tbody>
|
|
<tr>
|
|
<td>
|
|
<select value={type} onChange={(e) => changeType(e.target.value as RecoverType)}>
|
|
<option value="password">{t('typePassword')}</option>
|
|
<option value="accountname">{t('typeAccountName')}</option>
|
|
<option value="securitytoken">{t('typeSecurityToken')}</option>
|
|
<option value="activation">{t('typeActivation')}</option>
|
|
</select>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
{/* Las 4 opciones piden CORREO: con Battle.net la cuenta es el correo.
|
|
La de contraseña pedía el usuario («15#1»), que ya no vale. */}
|
|
<input type="email" maxLength={254} placeholder={t('email')} required value={value} onChange={(e) => setValue(e.target.value)} />
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
<Turnstile key={captchaKey} onVerify={setCaptcha} />
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
<button type="submit" className="recover-button" disabled={busy || sent} style={sent ? { color: '#d79602' } : undefined}>
|
|
{sent ? t('sent') : busy ? t('sending') : t('submit')}
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</form>
|
|
<hr />
|
|
<div className="alert-message" id="recover-response" style={{ display: message ? 'block' : 'none' }}>
|
|
{message && <span className={message.ok ? 'ok-form-response' : 'red-form-response'}>{message.text}</span>}
|
|
</div>
|
|
<br />
|
|
</>
|
|
)
|
|
}
|