Files
data-entry-app/frontend/src/lib/session.ts
T

121 lines
2.6 KiB
TypeScript
Raw Normal View History

2026-04-25 20:43:37 +12:00
import { browser } from '$app/environment';
2026-04-27 21:53:36 +12:00
import { readable, writable } from 'svelte/store';
2026-04-25 20:43:37 +12:00
2026-04-25 22:51:36 +12:00
export type AppSession = {
2026-04-25 20:43:37 +12:00
name: string;
email: string;
role: string;
2026-04-25 22:51:36 +12:00
token: string;
tenant_id?: string | null;
client_role?: string | null;
user_id?: number | null;
client_account_id?: number | null;
module_permissions?: Record<string, string>;
};
const ACCESS_LEVEL_ORDER: Record<string, number> = {
none: 0,
view: 1,
edit: 2,
manage: 3
2026-04-25 20:43:37 +12:00
};
2026-04-25 22:51:36 +12:00
const CLIENT_STORAGE_KEY = 'data-entry-app-client-session';
const ADMIN_STORAGE_KEY = 'data-entry-app-admin-session';
2026-04-25 20:43:37 +12:00
2026-04-25 22:51:36 +12:00
function readStoredSession(storageKey: string): AppSession | null {
2026-04-25 20:43:37 +12:00
if (!browser) {
return null;
}
2026-04-25 22:51:36 +12:00
const value = localStorage.getItem(storageKey);
2026-04-25 20:43:37 +12:00
if (!value) {
return null;
}
try {
2026-04-25 22:51:36 +12:00
return JSON.parse(value) as AppSession;
2026-04-25 20:43:37 +12:00
} catch {
2026-04-25 22:51:36 +12:00
localStorage.removeItem(storageKey);
2026-04-25 20:43:37 +12:00
return null;
}
}
2026-04-25 22:51:36 +12:00
function createSessionStore(storageKey: string) {
const store = writable<AppSession | null>(readStoredSession(storageKey));
2026-04-25 20:43:37 +12:00
if (browser) {
window.addEventListener('storage', (event) => {
2026-04-25 22:51:36 +12:00
if (event.key === storageKey) {
store.set(readStoredSession(storageKey));
2026-04-25 20:43:37 +12:00
}
});
}
return {
subscribe: store.subscribe,
2026-04-25 22:51:36 +12:00
set(session: AppSession) {
2026-04-25 20:43:37 +12:00
if (browser) {
2026-04-25 22:51:36 +12:00
localStorage.setItem(storageKey, JSON.stringify(session));
2026-04-25 20:43:37 +12:00
}
store.set(session);
},
clear() {
if (browser) {
2026-04-25 22:51:36 +12:00
localStorage.removeItem(storageKey);
2026-04-25 20:43:37 +12:00
}
store.set(null);
}
};
}
2026-04-25 22:51:36 +12:00
export function getStoredClientSession() {
return readStoredSession(CLIENT_STORAGE_KEY);
}
export function getStoredAdminSession() {
return readStoredSession(ADMIN_STORAGE_KEY);
}
export function hasStoredClientSession() {
return getStoredClientSession() !== null;
}
export function hasStoredAdminSession() {
return getStoredAdminSession() !== null;
}
export function hasModuleAccess(
session: AppSession | null | undefined,
moduleKey: string,
minimumLevel: 'view' | 'edit' | 'manage' = 'view'
) {
if (!session) {
return false;
}
if (session.role === 'admin') {
return true;
}
const currentLevel = session.module_permissions?.[moduleKey] ?? 'none';
return (ACCESS_LEVEL_ORDER[currentLevel] ?? 0) >= ACCESS_LEVEL_ORDER[minimumLevel];
}
2026-04-27 21:53:36 +12:00
export const sessionHydrated = readable(false, (set) => {
if (!browser) {
return undefined;
}
const frame = window.requestAnimationFrame(() => {
set(true);
});
return () => {
window.cancelAnimationFrame(frame);
};
});
2026-04-25 22:51:36 +12:00
export const clientSession = createSessionStore(CLIENT_STORAGE_KEY);
export const adminSession = createSessionStore(ADMIN_STORAGE_KEY);