Files
NightSpire/web-next/app/[locale]/login/LoginForm.tsx
T
Inna 3e47a3d240 Aplica el sistema de diseño (nw-input/nw-btn/nw-card) a todas las páginas
Reemplazo masivo de los estilos sueltos repetidos por las clases del sistema visual
en ~20 componentes/páginas (auth, cuenta, servicios de personaje, foro, admin, voto):
- inputs -> .nw-input, botones primarios -> .nw-btn, tarjetas -> .nw-card.
Cohesión visual completa con la home y la cabecera.

Verificado: build OK, páginas clave 200/redirect correctos, clases aplicadas.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-13 00:13:22 +00:00

99 lines
3.2 KiB
TypeScript

'use client'
import { useState } from 'react'
import { useTranslations } from 'next-intl'
import { useRouter } from '@/i18n/navigation'
import { Turnstile } from '@/components/Turnstile'
const ERROR_KEYS = ['invalidCredentials', 'missingFields', 'invalidRequest', 'captchaFailed'] as const
const SITE_KEY = process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY
export function LoginForm() {
const t = useTranslations('Login')
const router = useRouter()
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [showPw, setShowPw] = useState(false)
const [busy, setBusy] = useState(false)
const [captcha, setCaptcha] = useState('')
const [message, setMessage] = useState<{ ok: boolean; text: string } | null>(null)
async function handleSubmit(e: React.FormEvent) {
e.preventDefault()
if (busy) return
if (!email.trim() || !password) {
setMessage({ ok: false, text: t('missingFields') })
return
}
setBusy(true)
setMessage(null)
try {
const res = await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'same-origin',
body: JSON.stringify({ email: email.trim(), password, turnstileToken: captcha }),
})
const data: { success?: boolean; needsSelection?: boolean; error?: string } = await res.json()
if (data.success) {
setMessage({ ok: true, text: t('success') })
router.push(data.needsSelection ? '/select-account' : '/account')
} else {
const key = (ERROR_KEYS as readonly string[]).includes(data.error ?? '') ? data.error! : 'genericError'
setMessage({ ok: false, text: t(key) })
setBusy(false)
}
} catch {
setMessage({ ok: false, text: t('genericError') })
setBusy(false)
}
}
return (
<div className="mx-auto max-w-sm text-center">
<form onSubmit={handleSubmit} className="space-y-3">
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder={t('email')}
autoFocus
required
className="nw-input"
/>
<div className="relative">
<input
type={showPw ? 'text' : 'password'}
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder={t('password')}
maxLength={16}
required
className="nw-input pr-10"
/>
<button
type="button"
onClick={() => setShowPw((v) => !v)}
className="absolute inset-y-0 right-2 text-amber-200/60"
aria-label="toggle"
>
{showPw ? '🙈' : '👁'}
</button>
</div>
<Turnstile onVerify={setCaptcha} />
<button
type="submit"
disabled={busy || (!!SITE_KEY && !captcha)}
className="w-full nw-btn disabled:opacity-60"
>
{busy ? t('connecting') : t('submit')}
</button>
</form>
{message && (
<p className={`mt-3 ${message.ok ? 'text-green-400' : 'text-red-400'}`}>{message.text}</p>
)}
</div>
)
}