35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
'use client';
|
|
|
|
// v1.1.0 (ISS-107) — client-side session handling shared by every authenticated fetcher, the
|
|
// sign-in screen and the admin panel. The server mints sessions at /api/login + /api/register;
|
|
// we only store the id in localStorage and echo it back as X-Flight-Session.
|
|
|
|
export const SESSION_STORAGE_KEY = 'flight.session';
|
|
|
|
export function storedSession(): string | null {
|
|
if (typeof window === 'undefined') return null;
|
|
try {
|
|
return window.localStorage.getItem(SESSION_STORAGE_KEY);
|
|
} catch {
|
|
return null; // storage blocked (private mode) — treat as signed out
|
|
}
|
|
}
|
|
|
|
// Headers for an authenticated fetch. Empty object when no session is stored, so callers can
|
|
// spread it into their existing header objects without branching.
|
|
export function sessionHeaders(session: string | null = storedSession()): Record<string, string> {
|
|
return session ? { 'X-Flight-Session': session } : {};
|
|
}
|
|
|
|
export function storeSession(sessionId: string): void {
|
|
window.localStorage.setItem(SESSION_STORAGE_KEY, sessionId);
|
|
}
|
|
|
|
export function clearStoredSession(): void {
|
|
try {
|
|
window.localStorage.removeItem(SESSION_STORAGE_KEY);
|
|
} catch {
|
|
// storage blocked — nothing to clear
|
|
}
|
|
}
|