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
@@ -0,0 +1,40 @@
|
||||
import 'server-only'
|
||||
import { randomBytes, scryptSync, timingSafeEqual } from 'node:crypto'
|
||||
|
||||
/** scrypt password hashing, same scheme as LokiLab. */
|
||||
export function hashPassword(password: string): string {
|
||||
const salt = randomBytes(16).toString('hex')
|
||||
const hash = scryptSync(password, salt, 64).toString('hex')
|
||||
return `scrypt$${salt}$${hash}`
|
||||
}
|
||||
|
||||
export function verifyPassword(password: string, stored: string): boolean {
|
||||
const [algo, salt, hex] = stored.split('$')
|
||||
if (algo !== 'scrypt' || !salt || !hex) return false
|
||||
const expected = Buffer.from(hex, 'hex')
|
||||
const actual = scryptSync(password, salt, expected.length)
|
||||
return actual.length === expected.length && timingSafeEqual(actual, expected)
|
||||
}
|
||||
|
||||
const MAX_FAILURES = 8
|
||||
const LOCK_MS = 15 * 60 * 1000
|
||||
const failures = new Map<string, { count: number; until: number }>()
|
||||
|
||||
export function loginAllowed(ip: string): boolean {
|
||||
const f = failures.get(ip)
|
||||
if (!f) return true
|
||||
if (f.until && f.until > Date.now()) return false
|
||||
if (f.until && f.until <= Date.now()) failures.delete(ip)
|
||||
return true
|
||||
}
|
||||
|
||||
export function recordLoginFailure(ip: string): void {
|
||||
const f = failures.get(ip) ?? { count: 0, until: 0 }
|
||||
f.count += 1
|
||||
if (f.count >= MAX_FAILURES) f.until = Date.now() + LOCK_MS
|
||||
failures.set(ip, f)
|
||||
}
|
||||
|
||||
export function clearLoginFailures(ip: string): void {
|
||||
failures.delete(ip)
|
||||
}
|
||||
Reference in New Issue
Block a user