Login end-to-end en Next.js: iron-session + auth SRP6 + página i18n

- lib/session.ts: sesión cifrada httpOnly con iron-session (SESSION_SECRET en
  .env.local). SessionData {bnetId, bnetEmail, username, accountId}.
- lib/auth.ts: authenticate(email,password) verifica contra battlenet_accounts con
  bnetVerify (SRP6 v2); getGameAccounts(bnetId). Resiliente si acore no está.
- app/api/auth/login/route.ts: POST -> autentica, fija sesión, resuelve cuentas de
  juego (1 -> auto; 0/varias -> needsSelection). Devuelve JSON.
- app/[locale]/login: página SSR + LoginForm (cliente, next-intl, router i18n).
  Textos en messages/*.json (namespace Login).
- tsconfig target ES2020 (literales BigInt de bnet.ts).

Validado end-to-end: con una cuenta creada por bnet.py (como haría AzerothCore),
el login TS la verifica OK y emite la cookie de sesión; password incorrecta ->
invalidCredentials; sin campos -> missingFields. Página en ES y EN.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-12 22:42:23 +00:00
parent 10ba2d32df
commit 60c2e44afd
11 changed files with 309 additions and 4 deletions
+8
View File
@@ -0,0 +1,8 @@
import type { SessionData } from './session'
import type { GameAccount } from './auth'
/** Fija la cuenta de juego elegida en la sesión (equiv. a _set_game_account_session). */
export function setGameAccountSession(session: SessionData, game: GameAccount): void {
session.username = game.username
session.accountId = game.id
}
+46
View File
@@ -0,0 +1,46 @@
import type { RowDataPacket } from 'mysql2'
import { db, DB } from './db'
import { bnetVerify, normalizeEmail } from './bnet'
export interface BnetAccount {
id: number
email: string
}
export interface GameAccount {
id: number
username: string
index: number
}
/** Verifica email+password contra battlenet_accounts (SRP6 v2). Null si falla. */
export async function authenticate(email: string, password: string): Promise<BnetAccount | null> {
try {
const [rows] = await db(DB.auth).query<RowDataPacket[]>(
'SELECT id, email, salt, verifier FROM battlenet_accounts WHERE email = ?',
[normalizeEmail(email)],
)
const row = rows[0]
if (!row) return null
const salt: Buffer | null = row.salt ?? null
const verifier: Buffer | null = row.verifier ?? null
if (!bnetVerify(email, password, salt, verifier)) return null
return { id: row.id, email: row.email }
} catch {
// La BD de cuentas (AzerothCore) puede no estar disponible/poblada.
return null
}
}
/** Cuentas de juego enlazadas a una cuenta Battle.net. */
export async function getGameAccounts(bnetId: number): Promise<GameAccount[]> {
try {
const [rows] = await db(DB.auth).query<RowDataPacket[]>(
'SELECT id, username, battlenet_index FROM account WHERE battlenet_account = ? ORDER BY battlenet_index',
[bnetId],
)
return rows.map((r) => ({ id: r.id, username: r.username, index: r.battlenet_index }))
} catch {
return []
}
}
+25
View File
@@ -0,0 +1,25 @@
import { getIronSession, type SessionOptions } from 'iron-session'
import { cookies } from 'next/headers'
// Sesión cifrada en cookie httpOnly (equivalente a la sesión Django de antes).
export interface SessionData {
bnetId?: number
bnetEmail?: string
username?: string // cuenta de juego elegida (<bnetId>#<index>)
accountId?: number
}
export const sessionOptions: SessionOptions = {
password: process.env.SESSION_SECRET as string,
cookieName: 'novawow_session',
cookieOptions: {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
},
}
export async function getSession() {
return getIronSession<SessionData>(await cookies(), sessionOptions)
}