2a6629af79
Cloud saves were fully built but never wired up — useCloudSave() hook was never called, no load-from-cloud flow existed, and there was no way to continue a saved game. Logout was completely missing (no endpoint, no UI). Accounts felt like a gate behind the invite wall rather than real accounts. Backend: add tokenVersion to users for server-side token invalidation, POST /auth/logout bumps it to revoke all JWTs, GET /auth/me returns profile, GET /saves/latest returns most recent save with full gameData. All createToken calls now include tokenVersion. Auth middleware rejects tokens with stale tokenVersion. Frontend: wire up useCloudSave() in App (auto-saves every 60 ticks with error handling), fetch cloud save on startup for registered users, show "Continue Your Game" card on NewGameScreen, add Log Out button with confirmation in Settings, show username in sidebar, 401 interceptor clears auth and reloads. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { sign, verify } from 'hono/jwt';
|
|
|
|
const JWT_EXPIRY_SECONDS = 30 * 24 * 60 * 60;
|
|
|
|
export function getJwtSecret(): string {
|
|
const secret = process.env.JWT_SECRET;
|
|
if (!secret) throw new Error('JWT_SECRET env var is required');
|
|
return secret;
|
|
}
|
|
|
|
export async function createToken(
|
|
userId: string,
|
|
email: string | null,
|
|
role: string,
|
|
username: string | null,
|
|
mustResetPassword: boolean,
|
|
tokenVersion: number = 0,
|
|
): Promise<string> {
|
|
const now = Math.floor(Date.now() / 1000);
|
|
return sign(
|
|
{ sub: userId, email, role, username, mustResetPassword, tokenVersion, iat: now, exp: now + JWT_EXPIRY_SECONDS },
|
|
getJwtSecret(),
|
|
);
|
|
}
|
|
|
|
export async function verifyToken(token: string): Promise<{
|
|
sub: string;
|
|
email: string | null;
|
|
role: string;
|
|
username: string | null;
|
|
mustResetPassword: boolean;
|
|
tokenVersion: number;
|
|
}> {
|
|
const payload = await verify(token, getJwtSecret(), 'HS256');
|
|
return {
|
|
sub: payload.sub as string,
|
|
email: (payload.email as string) ?? null,
|
|
role: (payload.role as string) ?? 'user',
|
|
username: (payload.username as string) ?? null,
|
|
mustResetPassword: (payload.mustResetPassword as boolean) ?? false,
|
|
tokenVersion: (payload.tokenVersion as number) ?? 0,
|
|
};
|
|
}
|