v1.3 - client and admin scaffolding

This commit is contained in:
2026-04-25 22:51:36 +12:00
parent bc211ffcc8
commit 8cf9bfb441
54 changed files with 8868 additions and 1234 deletions
+34 -14
View File
@@ -1,58 +1,78 @@
import { browser } from '$app/environment';
import { writable } from 'svelte/store';
export type OperatorSession = {
export type AppSession = {
name: string;
email: string;
role: string;
token: string;
tenant_id?: string | null;
};
const STORAGE_KEY = 'data-entry-app-operator-session';
const CLIENT_STORAGE_KEY = 'data-entry-app-client-session';
const ADMIN_STORAGE_KEY = 'data-entry-app-admin-session';
function readSession(): OperatorSession | null {
function readStoredSession(storageKey: string): AppSession | null {
if (!browser) {
return null;
}
const value = localStorage.getItem(STORAGE_KEY);
const value = localStorage.getItem(storageKey);
if (!value) {
return null;
}
try {
return JSON.parse(value) as OperatorSession;
return JSON.parse(value) as AppSession;
} catch {
localStorage.removeItem(STORAGE_KEY);
localStorage.removeItem(storageKey);
return null;
}
}
function createOperatorSessionStore() {
const store = writable<OperatorSession | null>(readSession());
function createSessionStore(storageKey: string) {
const store = writable<AppSession | null>(readStoredSession(storageKey));
if (browser) {
window.addEventListener('storage', (event) => {
if (event.key === STORAGE_KEY) {
store.set(readSession());
if (event.key === storageKey) {
store.set(readStoredSession(storageKey));
}
});
}
return {
subscribe: store.subscribe,
set(session: OperatorSession) {
set(session: AppSession) {
if (browser) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(session));
localStorage.setItem(storageKey, JSON.stringify(session));
}
store.set(session);
},
clear() {
if (browser) {
localStorage.removeItem(STORAGE_KEY);
localStorage.removeItem(storageKey);
}
store.set(null);
}
};
}
export const operatorSession = createOperatorSessionStore();
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 const clientSession = createSessionStore(CLIENT_STORAGE_KEY);
export const adminSession = createSessionStore(ADMIN_STORAGE_KEY);