Spaces:
Sleeping
Sleeping
File size: 982 Bytes
30cd0c9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import type { UserSession } from "@/services/orchestrationApi";
const SESSION_STORAGE_KEY = "chatbot_user";
export function getCurrentSession(): UserSession | null {
try {
const raw = localStorage.getItem(SESSION_STORAGE_KEY);
return raw ? (JSON.parse(raw) as UserSession) : null;
} catch {
return null;
}
}
export function writeSession(session: UserSession): void {
localStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(session));
}
export function clearSession(): void {
localStorage.removeItem(SESSION_STORAGE_KEY);
}
export function isSessionUsable(session = getCurrentSession()): boolean {
if (!session?.user_id || !session.access_token || !session.refresh_token) return false;
if (!session.refresh_expires_at) return true;
const refreshExpiry = new Date(session.refresh_expires_at).getTime();
if (Number.isNaN(refreshExpiry)) return true;
if (refreshExpiry <= Date.now()) {
clearSession();
return false;
}
return true;
}
|