Cooldown definido por el servidor
El servidor aplica el límite real. Deja que le diga al cliente cuánto esperar.
Devolver retryAfter desde onResend
<Cooldown.Root
storageKey={`otp:${email}`}
duration={30}
onResend={async () => {
const res = await fetch('/api/otp/resend', {
method: 'POST',
body: JSON.stringify({ email }),
})
if (res.status === 429) {
return { retryAfter: Number(res.headers.get('Retry-After') ?? 30) }
}
if (!res.ok) throw new Error('No se pudo enviar el código')
const { nextAllowedAt } = await res.json()
return { endsAt: nextAllowedAt }
}}
/>- No devuelve nada: se usa
duration. { retryAfter }: segundos a partir de ahora.{ endsAt }: un timestamp epoch en ms.- Error lanzado: estado
error. El intento no cuenta y el usuario puede reintentar enseguida.
Empezar sin enviar
Si el primer código lo envía otra petición, como el formulario de registro, inicia el cooldown tú mismo:
const cd = useCooldown({ storageKey: `otp:${email}` })
const { retryAfter } = await api.signup(form)
cd.start({ retryAfter })start({ countAttempt: false }) alarga el cooldown actual sin gastar un intento.