Something went wrong. Try again.
The logic for serving and storing associations between users and apps
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930export const COOKIE_NAME = "loggedin_session";export const SESSION_ID_RE = /^[0-9a-f]{32}$/;export const COOKIE_MAX_AGE_SEC = 7 * 24 * 60 * 60;
export function newSessionId(): string { const bytes = new Uint8Array(16); crypto.getRandomValues(bytes); return [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");}
export function parseSessionId(cookieHeader: string | null): string | null { if (!cookieHeader) return null; for (const part of cookieHeader.split(";")) { const trimmed = part.trim(); const eq = trimmed.indexOf("="); if (eq === -1) continue; const name = trimmed.slice(0, eq); const value = trimmed.slice(eq + 1); if (name === COOKIE_NAME && SESSION_ID_RE.test(value)) return value; } return null;}
export function sessionCookieHeader(id: string, secure: boolean): string { if (secure) { return `${COOKIE_NAME}=${id}; Path=/; HttpOnly; Secure; SameSite=None; Max-Age=${COOKIE_MAX_AGE_SEC}`; } return `${COOKIE_NAME}=${id}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${COOKIE_MAX_AGE_SEC}`;}