eatback / workspaces /assets /workspace-api.js
kritpakin's picture
School A/B: real Thai School Lunch menus and weekly nutrition ratings, data-source table, honest static mode, district view
f74e6c3 verified
Raw History Blame Contribute Delete
3.67 kB
// Public fictional fixtures only. This is a browser simulation, NOT authentication
// or tenant isolation. The real local server continues to enforce its own sessions.
(() => {
window.EATBACK_WORKSPACE_STATIC = true;
const nativeFetch = window.fetch.bind(window);
const key = 'eatback.workspace.static.profile';
const accounts = {
schoolA: { password: 'EatBack-A-2026', id: 'school_a', display_name: 'ผู้ดูแลโรงเรียน A' },
schoolB: { password: 'EatBack-B-2026', id: 'school_b', display_name: 'ผู้ดูแลโรงเรียน B' },
};
const result = (status, payload) => new Response(JSON.stringify({ ...payload, fictional_demo: true, auth_mode: 'browser_simulation' }), {
status, headers: { 'Content-Type': 'application/json', 'Cache-Control': 'no-store', 'X-EatBack-Mode': 'browser_simulation' },
});
function profile() {
try {
const value = JSON.parse(sessionStorage.getItem(key));
return value && value.expires > Date.now() && accounts[value.username]?.id === value.id ? value : null;
} catch { return null; }
}
async function fixture(file) {
const response = await nativeFetch(`/workspaces/data/${file}.json`, { cache: 'no-cache' });
if (!response.ok) throw new Error('Could not load the public demo fixture');
const data = await response.json();
if (data.fictional_demo !== true) throw new Error('Only fictional fixtures are supported');
return data;
}
window.fetch = async (input, init = {}) => {
const url = new URL(typeof input === 'string' || input instanceof URL ? input : input.url, location.href);
if (url.origin !== location.origin || !url.pathname.startsWith('/api/')) return nativeFetch(input, init);
const method = String(init.method || input?.method || 'GET').toUpperCase();
const path = url.pathname;
if (method === 'POST' && path === '/api/login') {
let body;
try { body = JSON.parse(init.body ?? await input.clone().text()); } catch { return result(400, { error: 'invalid_request' }); }
const account = accounts[body?.username];
if (!account || account.password !== body?.password) return result(401, { error: 'invalid_credentials' });
try { sessionStorage.setItem(key, JSON.stringify({ id: account.id, username: body.username, expires: Date.now() + 7200000 })); }
catch { return result(503, { error: 'browser_storage_unavailable' }); }
return result(200, { ok: true, redirect: '/workspaces/dashboard.html' });
}
if (method === 'GET' && path === '/api/district-demo') return result(200, await fixture('district'));
const current = profile();
if (!current) return result(401, { error: 'no_demo_profile_selected' });
if (method === 'GET' && path === '/api/me') {
const school = await fixture(current.id);
return result(200, { user: { id: current.id, school_id: current.id, username: current.username,
display_name: accounts[current.username].display_name, school_name: school.school.name }, csrf_token: 'static-demo-only' });
}
if (method === 'POST' && path === '/api/logout') {
sessionStorage.removeItem(key);
return result(200, { ok: true });
}
const match = /^\/api\/workspaces\/(school_[ab])\/dashboard$/.exec(path);
if (method === 'GET' && match) {
if (match[1] !== current.id) return result(403, { error: 'simulated_wrong_workspace', simulated: true,
note: 'Browser demonstration only. All fictional fixtures are public; this is not a security boundary.' });
return result(200, await fixture(current.id));
}
return result(404, { error: 'not_found' });
};
})();