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)
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import type { Metadata } from "next"
|
||||
import Link from "next/link"
|
||||
import { redirect } from "next/navigation"
|
||||
import { getCurrentMember } from "@/lib/member-auth"
|
||||
import { MemberAuthForm } from "@/components/member-auth-form"
|
||||
import { LokiMark } from "@/components/loki-mark"
|
||||
|
||||
export const metadata: Metadata = { title: "Connexion — LokiLoto" }
|
||||
export const instant = false
|
||||
|
||||
const ERROR_FR: Record<string, string> = {
|
||||
oauth_state: "La connexion a expiré ou a été interrompue. Réessayez.",
|
||||
oauth_failed: "La connexion avec Google a échoué. Réessayez.",
|
||||
google_not_configured: "La connexion Google n'est pas encore activée sur ce site.",
|
||||
}
|
||||
|
||||
export default async function LoginPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<Record<string, string | string[] | undefined>>
|
||||
}) {
|
||||
const member = await getCurrentMember()
|
||||
if (member) redirect("/profil")
|
||||
|
||||
const sp = await searchParams
|
||||
const errorKey = typeof sp.error === "string" ? sp.error : null
|
||||
const error = errorKey ? (ERROR_FR[errorKey] ?? "Une erreur est survenue. Réessayez.") : null
|
||||
const next = typeof sp.next === "string" && sp.next.startsWith("/") ? sp.next : "/profil"
|
||||
|
||||
return (
|
||||
<main className="flex flex-1 items-center justify-center px-4 py-12">
|
||||
<div className="w-full max-w-sm">
|
||||
<Link href="/" className="mb-8 inline-flex items-center gap-2">
|
||||
<LokiMark className="h-8 w-8 text-accent" />
|
||||
<span className="text-lg font-bold tracking-tight">LokiLoto</span>
|
||||
</Link>
|
||||
|
||||
<h1 className="text-xl font-bold tracking-tight">Connexion</h1>
|
||||
<p className="mt-1 text-sm text-muted-foreground">
|
||||
Suivez les choix de Loki, apparaissez au classement, gardez votre pays favori.
|
||||
</p>
|
||||
|
||||
{error && <p className="mt-4 text-xs text-destructive">{error}</p>}
|
||||
|
||||
<div className="mt-6">
|
||||
<MemberAuthForm next={next} />
|
||||
</div>
|
||||
|
||||
<div className="my-5 flex items-center gap-3 text-[11px] text-muted-foreground">
|
||||
<span className="h-px flex-1 bg-border" />
|
||||
ou
|
||||
<span className="h-px flex-1 bg-border" />
|
||||
</div>
|
||||
|
||||
<a
|
||||
href={`/api/auth/google?next=${encodeURIComponent(next)}`}
|
||||
className="flex h-9 w-full items-center justify-center gap-3 rounded-md border border-border bg-panel text-sm font-medium transition-colors hover:bg-panel-hover"
|
||||
>
|
||||
<svg viewBox="0 0 24 24" className="h-4 w-4 shrink-0" aria-hidden="true">
|
||||
<path
|
||||
fill="#4285F4"
|
||||
d="M23.52 12.27c0-.85-.07-1.47-.23-2.12H12v3.85h6.6c-.13 1.1-.86 2.76-2.47 3.87l-.02.15 3.59 2.78.25.02c2.28-2.1 3.57-5.2 3.57-8.55"
|
||||
/>
|
||||
<path
|
||||
fill="#34A853"
|
||||
d="M12 24c3.24 0 5.95-1.07 7.93-2.91l-3.78-2.93c-1.02.7-2.4 1.19-4.15 1.19-3.18 0-5.88-2.1-6.84-5l-.14.01-3.73 2.89-.05.13C3.21 21.3 7.26 24 12 24"
|
||||
/>
|
||||
<path
|
||||
fill="#FBBC05"
|
||||
d="M5.16 14.35A7.3 7.3 0 0 1 4.75 12c0-.81.14-1.6.4-2.35l-.01-.16-3.78-2.94-.12.06A11.97 11.97 0 0 0 0 12c0 1.93.46 3.76 1.24 5.39l3.92-3.04"
|
||||
/>
|
||||
<path
|
||||
fill="#EA4335"
|
||||
d="M12 4.75c2.25 0 3.77.97 4.64 1.78l3.39-3.31C17.94 1.19 15.24 0 12 0 7.26 0 3.21 2.7 1.24 6.61l3.91 3.04c.97-2.9 3.67-5 6.85-5"
|
||||
/>
|
||||
</svg>
|
||||
Continuer avec Google
|
||||
</a>
|
||||
|
||||
<p className="mt-6 text-[11px] leading-relaxed text-muted-foreground">
|
||||
Un compte LokiLoto sert à suivre vos pronostics et apparaître au classement. Aucun billet
|
||||
n'est vendu, aucun argent ne transite par ce site.
|
||||
</p>
|
||||
<Link href="/" className="mt-6 inline-block text-xs text-muted-foreground hover:text-foreground">
|
||||
← Retour au site
|
||||
</Link>
|
||||
</div>
|
||||
</main>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user