Spaces:
Running
Running
File size: 694 Bytes
06b628f 7c00f2a 06b628f 7c00f2a 06b628f 7c00f2a 06b628f 7c00f2a 06b628f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /**
* Authenticated fetch wrapper.
* Reads the JWT from localStorage and attaches it as a Bearer token
* on every request. Auto-clears credentials on 401 (expired/invalid token).
*/
export async function apiFetch(url, options = {}) {
const token = localStorage.getItem('soma_token');
const res = await fetch(url, {
...options,
headers: {
'Content-Type': 'application/json',
...(token ? { Authorization: `Bearer ${token}` } : {}),
...options.headers,
},
});
if (res.status === 401) {
localStorage.removeItem('soma_token');
localStorage.removeItem('soma_username');
window.dispatchEvent(new Event('soma-auth-expired'));
}
return res;
}
|