'use client' import { useState } from 'react' import { useTranslations } from 'next-intl' import { Link } from '@/i18n/navigation' import { Turnstile } from '@/components/Turnstile' const ERROR_KEYS = [ 'missingFields', 'passwordMismatch', 'passwordTooLong', 'invalidEmail', 'emailExists', 'recruiterNotFound', 'captchaFailed', ] as const /** Campo de contraseña con ojo para mostrar/ocultar. */ function PasswordInput({ value, onChange, placeholder, ariaLabel, blockPaste, }: { value: string onChange: (v: string) => void placeholder: string ariaLabel: string blockPaste?: boolean }) { const [show, setShow] = useState(false) const noPaste = blockPaste ? (e: React.ClipboardEvent) => e.preventDefault() : undefined return ( <> onChange(e.target.value)} onPaste={noPaste} onCopy={noPaste} onCut={noPaste} />{' '} setShow((s) => !s)} /> ) } export function RegisterForm() { const t = useTranslations('Register') const [form, setForm] = useState({ password: '', confPassword: '', email: '', confEmail: '', recruiter: '' }) const [accepted, setAccepted] = useState(false) const [notUs, setNotUs] = useState(false) const [busy, setBusy] = useState(false) const [captcha, setCaptcha] = useState('') const [captchaKey, setCaptchaKey] = useState(0) // remonta el widget para pedir un token nuevo const [message, setMessage] = useState<{ ok: boolean; text: string } | null>(null) const canSubmit = accepted && notUs && !busy function upd(k: string, v: string) { setForm((f) => ({ ...f, [k]: v })) } /** Validación en cliente: muestra el error en rojo al instante, sin ir al servidor. */ function clientError(): string | null { if (!form.password || !form.confPassword || !form.email || !form.confEmail) return 'missingFields' if (form.password !== form.confPassword) return 'passwordMismatch' if (form.password.length > 16) return 'passwordTooLong' if (form.email.trim() !== form.confEmail.trim()) return 'emailMismatch' return null } async function handleSubmit(e: React.FormEvent) { e.preventDefault() if (!canSubmit) return const err = clientError() if (err) { setMessage({ ok: false, text: t(err) }) return } setBusy(true) setMessage(null) try { const res = await fetch('/api/auth/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'same-origin', body: JSON.stringify({ ...form, turnstileToken: captcha }), }) const data: { success?: boolean; error?: string } = await res.json() if (data.success) { setMessage({ ok: true, text: t('success') }) } else { const key = (ERROR_KEYS as readonly string[]).includes(data.error ?? '') ? data.error! : 'genericError' setMessage({ ok: false, text: t(key) }) // El token de Turnstile es de un solo uso: resetear tras un fallo. setCaptcha('') setCaptchaKey((k) => k + 1) } } catch { setMessage({ ok: false, text: t('genericError') }) setCaptcha('') setCaptchaKey((k) => k + 1) } finally { setBusy(false) } } const noPaste = (e: React.ClipboardEvent) => e.preventDefault() return ( <>
upd('password', v)} placeholder={t('password')} ariaLabel={t('showPassword')} />
upd('confPassword', v)} placeholder={t('confPassword')} ariaLabel={t('showPassword')} blockPaste />
upd('email', e.target.value)} />
upd('confEmail', e.target.value)} onPaste={noPaste} onCopy={noPaste} onCut={noPaste} />

{t('recruiterOptional')}
upd('recruiter', e.target.value)} />

{message && {message.text}}
) }