Download src/lib/session.ts from do4lov3/tambi: direct link, hf CLI and curl.
- Browser
- Download file 1.46 kB
-
https://huggingface.co/spaces/do4lov3/tambi/resolve/main/src/lib/session.ts
- Command line
-
hf download hf://spaces/do4lov3/tambi/src/lib/session.ts
-
curl -L -o session.ts https://huggingface.co/spaces/do4lov3/tambi/resolve/main/src/lib/session.ts
1.46 kB
| import { cookies } from 'next/headers'; | |
| import { redirect } from 'next/navigation'; | |
| import { prisma } from '@/lib/db'; | |
| const SESSION_COOKIE = 'candidatesession'; | |
| const SESSION_DURATION_MS = 30 * 24 * 60 * 60 * 1000; // 30 days | |
| export async function getSessionCandidateId(): Promise<string | null> { | |
| const cookieStore = await cookies(); | |
| const sessionId = cookieStore.get(SESSION_COOKIE)?.value; | |
| if (!sessionId) return null; | |
| const session = await prisma.session.findUnique({ | |
| where: { id: sessionId }, | |
| }); | |
| if (!session || session.expiresAt < new Date()) { | |
| return null; | |
| } | |
| return session.candidateId; | |
| } | |
| export async function requireCandidate(): Promise<string> { | |
| const id = await getSessionCandidateId(); | |
| if (!id) { | |
| redirect('/registrarme'); | |
| } | |
| return id; | |
| } | |
| export async function createCandidateSession(candidateId: string): Promise<string> { | |
| const { randomUUID } = await import('node:crypto'); | |
| const id = randomUUID(); | |
| const expiresAt = new Date(Date.now() + SESSION_DURATION_MS); | |
| await prisma.session.create({ | |
| data: { id, candidateId, expiresAt }, | |
| }); | |
| return id; | |
| } | |
| export function sessionCookieHeader(sessionId: string): string { | |
| const maxAge = SESSION_DURATION_MS / 1000; | |
| return `${SESSION_COOKIE}=${sessionId}; HttpOnly; SameSite=Lax; Path=/; Max-Age=${maxAge}`; | |
| } | |
| export function clearSessionCookieHeader(): string { | |
| return `${SESSION_COOKIE}=; HttpOnly; SameSite=Lax; Path=/; Max-Age=0`; | |
| } | |