| |
| |
| |
| |
| |
| |
|
|
| export interface ActiveUser { |
| userId: string |
| via: 'demo' | 'passkey' |
| } |
|
|
| let token: string | null = null |
| let activeUser: ActiveUser | null = null |
| const listeners = new Set<() => void>() |
|
|
| function emit(): void { |
| listeners.forEach((fn) => fn()) |
| } |
|
|
| export function setToken(t: string | null): void { |
| token = t |
| emit() |
| } |
|
|
| export function getToken(): string | null { |
| return token |
| } |
|
|
| export function setActiveUser(user: ActiveUser | null): void { |
| activeUser = user |
| emit() |
| } |
|
|
| export function getActiveUser(): ActiveUser | null { |
| return activeUser |
| } |
|
|
| export function isAuthenticated(): boolean { |
| return token !== null |
| } |
|
|
| |
| export function logout(): void { |
| token = null |
| activeUser = null |
| emit() |
| } |
|
|
| export function onSessionChange(fn: () => void): () => void { |
| listeners.add(fn) |
| return () => listeners.delete(fn) |
| } |
|
|