Files
lokiloto/lib/google-oauth.ts
Nazim KriandClaude Sonnet 5 c3a0a7b765 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>
2026-10-30 09:02:26 -04:00

64 lines
2.0 KiB
TypeScript

import 'server-only'
/**
* Google OAuth (Authorization Code flow), fetch-only — same pattern as
* LokiLab. Uses a LokiLoto-dedicated OAuth client (project-specific env var
* names, not shared with any other devyouz-lab project's Google client).
*/
const CLIENT_ID = process.env.LOKILOTO_GOOGLE_OAUTH_CLIENT_ID || ''
const CLIENT_SECRET = process.env.LOKILOTO_GOOGLE_OAUTH_CLIENT_SECRET || ''
function appUrl(): string {
return process.env.NEXT_PUBLIC_SITE_URL || 'https://lotoloki.top'
}
export const googleEnabled = !!(CLIENT_ID && CLIENT_SECRET)
export function googleRedirectUri(): string {
return `${appUrl()}/api/auth/google/callback`
}
export function getGoogleAuthUrl(state: string): string {
const params = new URLSearchParams({
client_id: CLIENT_ID,
redirect_uri: googleRedirectUri(),
response_type: 'code',
scope: 'openid email profile',
access_type: 'online',
prompt: 'select_account',
state,
})
return `https://accounts.google.com/o/oauth2/v2/auth?${params}`
}
export interface GoogleProfile {
sub: string
email: string
name: string
picture?: string
email_verified?: boolean
}
export async function exchangeGoogleCode(code: string): Promise<GoogleProfile | null> {
const tokenRes = await fetch('https://oauth2.googleapis.com/token', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
code,
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
redirect_uri: googleRedirectUri(),
grant_type: 'authorization_code',
}),
})
if (!tokenRes.ok) return null
const tokenData = (await tokenRes.json()) as { access_token?: string }
if (!tokenData.access_token) return null
const profileRes = await fetch('https://www.googleapis.com/oauth2/v3/userinfo', {
headers: { Authorization: `Bearer ${tokenData.access_token}` },
})
if (!profileRes.ok) return null
return profileRes.json() as Promise<GoogleProfile>
}