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,89 @@
|
||||
'use server'
|
||||
|
||||
import { headers } from 'next/headers'
|
||||
import { redirect } from 'next/navigation'
|
||||
import { z } from 'zod'
|
||||
import { prisma } from '@/lib/db'
|
||||
import { createMemberSession } from '@/lib/member-auth'
|
||||
import { hashPassword, verifyPassword, loginAllowed, recordLoginFailure, clearLoginFailures } from '@/lib/member-password'
|
||||
|
||||
export interface MemberAuthState {
|
||||
ok: boolean
|
||||
message: string
|
||||
}
|
||||
|
||||
const signupSchema = z.object({
|
||||
name: z.string().trim().min(1).max(80),
|
||||
email: z.string().trim().toLowerCase().email(),
|
||||
password: z.string().min(8).max(200),
|
||||
next: z.string().startsWith('/').refine((s) => !s.startsWith('//'), { message: 'invalid next' }),
|
||||
})
|
||||
|
||||
const loginSchema = z.object({
|
||||
email: z.string().trim().toLowerCase().email(),
|
||||
password: z.string().min(1).max(200),
|
||||
next: z.string().startsWith('/').refine((s) => !s.startsWith('//'), { message: 'invalid next' }),
|
||||
})
|
||||
|
||||
async function clientIp(): Promise<string> {
|
||||
const h = await headers()
|
||||
return (h.get('x-forwarded-for')?.split(',')[0] ?? h.get('x-real-ip') ?? 'unknown').trim()
|
||||
}
|
||||
|
||||
export async function signupWithPassword(_prev: MemberAuthState, formData: FormData): Promise<MemberAuthState> {
|
||||
const ip = await clientIp()
|
||||
if (!loginAllowed(ip)) return { ok: false, message: 'Trop de tentatives. Réessayez dans 15 minutes.' }
|
||||
|
||||
const parsed = signupSchema.safeParse({
|
||||
name: formData.get('name'),
|
||||
email: formData.get('email'),
|
||||
password: formData.get('password'),
|
||||
next: formData.get('next') || '/profil',
|
||||
})
|
||||
if (!parsed.success) {
|
||||
recordLoginFailure(ip)
|
||||
return { ok: false, message: 'Nom, courriel valide et mot de passe (8 caractères min.) requis.' }
|
||||
}
|
||||
|
||||
const existing = await prisma.user.findUnique({ where: { email: parsed.data.email } })
|
||||
if (existing) {
|
||||
return { ok: false, message: 'Un compte existe déjà avec ce courriel. Connectez-vous plutôt.' }
|
||||
}
|
||||
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
email: parsed.data.email,
|
||||
name: parsed.data.name,
|
||||
passwordHash: hashPassword(parsed.data.password),
|
||||
},
|
||||
})
|
||||
|
||||
clearLoginFailures(ip)
|
||||
await createMemberSession(user.id)
|
||||
redirect(parsed.data.next)
|
||||
}
|
||||
|
||||
export async function loginWithPassword(_prev: MemberAuthState, formData: FormData): Promise<MemberAuthState> {
|
||||
const ip = await clientIp()
|
||||
if (!loginAllowed(ip)) return { ok: false, message: 'Trop de tentatives. Réessayez dans 15 minutes.' }
|
||||
|
||||
const parsed = loginSchema.safeParse({
|
||||
email: formData.get('email'),
|
||||
password: formData.get('password'),
|
||||
next: formData.get('next') || '/profil',
|
||||
})
|
||||
if (!parsed.success) {
|
||||
recordLoginFailure(ip)
|
||||
return { ok: false, message: 'Courriel ou mot de passe incorrect.' }
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({ where: { email: parsed.data.email } })
|
||||
if (!user?.passwordHash || !verifyPassword(parsed.data.password, user.passwordHash)) {
|
||||
recordLoginFailure(ip)
|
||||
return { ok: false, message: 'Courriel ou mot de passe incorrect.' }
|
||||
}
|
||||
|
||||
clearLoginFailures(ip)
|
||||
await createMemberSession(user.id)
|
||||
redirect(parsed.data.next)
|
||||
}
|
||||
Reference in New Issue
Block a user