Run Comparison

// Auth helpers function getToken() { return localStorage.getItem('ksa_token'); } function getUser() { try { return JSON.parse(localStorage.getItem('ksa_user') || 'null'); } catch { return null; } } function setAuth(token, user) { localStorage.setItem('ksa_token', token); localStorage.setItem('ksa_user', JSON.stringify(user)); document.cookie = 'ksa_token=' + token + '; path=/; max-age=86400'; } function clearAuth() { localStorage.removeItem('ksa_token'); localStorage.removeItem('ksa_user'); document.cookie = 'ksa_token=; path=/; max-age=0'; } function logout() { clearAuth(); window.location.href = '/login'; } function authHeaders() { const t = getToken(); return t ? { 'Authorization': 'Bearer ' + t } : {}; } async function fetchJSON(url, opts = {}) { opts.headers = { ...authHeaders(), ...(opts.headers || {}) }; const resp = await fetch(url, opts); if (resp.status === 401) { clearAuth(); window.location.href = '/login'; return; } return resp.json(); }