e00a439dd9
- Route handlers: /api/auth/logout (destroy sesión), /api/auth/select-account (fija la cuenta de juego elegida, valida que pertenece a la bnet en sesión). - app/[locale]/select-account: página SSR (guarda: sin bnetId -> login) + lista de cuentas de juego (SelectAccountForm cliente) -> /account. - app/[locale]/account: página protegida (sin bnetId -> login; sin username -> select-account) con datos de sesión + LogoutButton. - Textos en messages (Account, SelectAccount). Verificado: guardas locale-aware (ES /account -> /login, EN /en/account -> /en/login, 307), logout OK, select-account sin sesión 401. Redirect i18n con next-intl. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { useTranslations } from 'next-intl'
|
|
import { useRouter } from '@/i18n/navigation'
|
|
|
|
interface GameAccount {
|
|
id: number
|
|
username: string
|
|
}
|
|
|
|
export function SelectAccountForm({ accounts }: { accounts: GameAccount[] }) {
|
|
const t = useTranslations('SelectAccount')
|
|
const router = useRouter()
|
|
const [busy, setBusy] = useState<number | null>(null)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
async function select(id: number) {
|
|
if (busy !== null) return
|
|
setBusy(id)
|
|
setError(null)
|
|
try {
|
|
const res = await fetch('/api/auth/select-account', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({ accountId: id }),
|
|
})
|
|
const data: { success?: boolean } = await res.json()
|
|
if (data.success) router.push('/account')
|
|
else {
|
|
setError(t('error'))
|
|
setBusy(null)
|
|
}
|
|
} catch {
|
|
setError(t('error'))
|
|
setBusy(null)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-2">
|
|
{accounts.map((a) => (
|
|
<button
|
|
key={a.id}
|
|
onClick={() => select(a.id)}
|
|
disabled={busy !== null}
|
|
className="w-full rounded bg-amber-600 px-4 py-2 font-semibold text-[#1b120b] disabled:opacity-60"
|
|
>
|
|
{busy === a.id ? t('entering') : a.username}
|
|
</button>
|
|
))}
|
|
{error && <p className="text-red-400">{error}</p>}
|
|
</div>
|
|
)
|
|
}
|