Files
lokiloto/lib/member-auth.ts
T
Nazim KriandClaude Opus 5.5 e97b2e54ad Country selector in the header, auto-detected and remembered
Replaces the left sidebar with a header dropdown (flag + name), like
1xbet's region picker. The shown country resolves as: saved choice
(cookie, then the member's profile so it follows them across devices),
else Cloudflare's CF-IPCountry (Canada maps to Québec), else Québec.
favoriteCountry becomes nullable so geo-detection isn't overridden by
a default nobody chose. Emoji flags render as letters on Windows, so
flags are now SVGs in /public/flags; country names are in French.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 09:58:33 -04:00

42 lines
1.2 KiB
TypeScript

import 'server-only'
import { cookies } from 'next/headers'
import { createSessionToken, verifySessionToken, MEMBER_SESSION_COOKIE, SESSION_MAX_AGE } from './auth'
import { prisma } from './db'
export async function createMemberSession(userId: string): Promise<void> {
const token = await createSessionToken(userId)
const jar = await cookies()
jar.set(MEMBER_SESSION_COOKIE, token, {
httpOnly: true,
sameSite: 'lax',
secure: process.env.NODE_ENV === 'production',
maxAge: SESSION_MAX_AGE,
path: '/',
})
}
export async function destroyMemberSession(): Promise<void> {
const jar = await cookies()
jar.delete(MEMBER_SESSION_COOKIE)
}
export interface CurrentMember {
id: string
email: string
name: string
avatarUrl: string | null
publicNickname: string | null
favoriteCountry: string | null
}
export async function getCurrentMember(): Promise<CurrentMember | null> {
const jar = await cookies()
const session = await verifySessionToken(jar.get(MEMBER_SESSION_COOKIE)?.value)
if (!session) return null
const user = await prisma.user.findUnique({
where: { id: session.sub },
select: { id: true, email: true, name: true, avatarUrl: true, publicNickname: true, favoriteCountry: true },
})
return user
}