Add member accounts, follow-based leaderboard, and dedicated Postgres
Members register via email+password (scrypt) or Google OAuth — same signed-cookie + scrypt pattern as LokiLab (lib/auth.ts, member-password.ts, member-auth.ts, google-oauth.ts), own AUTH_SECRET and own OAuth client, own dedicated Postgres database (lokiloto-db on Coolify), fully isolated from LokiLab's own DB and auth. Leaderboard tracks who follows Loki's picks most accurately — members star a pick from the results panel (FollowButton), resolution reuses LokiLab's own lastDraw.won, no money or pooled play involved anywhere. Leaderboard page renders a horizontal bar chart (recharts) plus a ranked list. Profile page: public nickname (required to appear on the leaderboard), list of followed picks. Header now carries nav + a user menu (avatar dropdown when signed in, "Connexion" button otherwise). Dockerfile runs prisma migrate deploy at container start via docker-entrypoint.sh, same pattern as LokiLab. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
fc69d4a668
commit
c3a0a7b765
+77
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* Signed session cookie (HMAC-SHA256 over a base64url payload). Runtime-agnostic:
|
||||
* only Web Crypto, so it works in route handlers and server actions alike.
|
||||
* Same scheme as LokiLab (lib/auth.ts), own AUTH_SECRET, own cookie name.
|
||||
*/
|
||||
export const MEMBER_SESSION_COOKIE = 'lokiloto_session'
|
||||
export const SESSION_MAX_AGE = 7 * 24 * 3600
|
||||
|
||||
export interface Session {
|
||||
sub: string
|
||||
exp: number
|
||||
}
|
||||
|
||||
function secret(): string {
|
||||
const s = process.env.AUTH_SECRET
|
||||
if (!s || s.length < 16) throw new Error('AUTH_SECRET is not configured')
|
||||
return s
|
||||
}
|
||||
|
||||
function toBase64Url(bytes: Uint8Array): string {
|
||||
let bin = ''
|
||||
for (const b of bytes) bin += String.fromCharCode(b)
|
||||
return btoa(bin).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
|
||||
}
|
||||
|
||||
function fromBase64Url(s: string): Uint8Array {
|
||||
const b64 = s.replace(/-/g, '+').replace(/_/g, '/') + '='.repeat((4 - (s.length % 4)) % 4)
|
||||
const bin = atob(b64)
|
||||
const out = new Uint8Array(bin.length)
|
||||
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i)
|
||||
return out
|
||||
}
|
||||
|
||||
async function sign(data: string): Promise<string> {
|
||||
const enc = new TextEncoder()
|
||||
const key = await crypto.subtle.importKey('raw', enc.encode(secret()), { name: 'HMAC', hash: 'SHA-256' }, false, [
|
||||
'sign',
|
||||
])
|
||||
const sig = await crypto.subtle.sign('HMAC', key, enc.encode(data))
|
||||
return toBase64Url(new Uint8Array(sig))
|
||||
}
|
||||
|
||||
function safeEqual(a: string, b: string): boolean {
|
||||
if (a.length !== b.length) return false
|
||||
let diff = 0
|
||||
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i)
|
||||
return diff === 0
|
||||
}
|
||||
|
||||
export async function createSessionToken(sub: string): Promise<string> {
|
||||
const body: Session = { sub, exp: Math.floor(Date.now() / 1000) + SESSION_MAX_AGE }
|
||||
const payload = toBase64Url(new TextEncoder().encode(JSON.stringify(body)))
|
||||
return `${payload}.${await sign(payload)}`
|
||||
}
|
||||
|
||||
export async function verifySessionToken(token: string | undefined | null): Promise<Session | null> {
|
||||
if (!token) return null
|
||||
const dot = token.indexOf('.')
|
||||
if (dot <= 0) return null
|
||||
const payload = token.slice(0, dot)
|
||||
const sig = token.slice(dot + 1)
|
||||
let expected: string
|
||||
try {
|
||||
expected = await sign(payload)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
if (!safeEqual(sig, expected)) return null
|
||||
try {
|
||||
const data = JSON.parse(new TextDecoder().decode(fromBase64Url(payload))) as Partial<Session>
|
||||
if (typeof data.sub !== 'string' || typeof data.exp !== 'number') return null
|
||||
if (data.exp <= Math.floor(Date.now() / 1000)) return null
|
||||
return { sub: data.sub, exp: data.exp }
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user