| 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; | |
| } | |