blob: 4ba0e0cdc8bc7236e7dd22a3fde5cdb8d902c0d6 (
plain) (
blame)
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
26
27
28
29
|
// Utility for making authenticated API requests
export const fetchWithAuth = async (endpoint, options = {}) => {
const token = localStorage.getItem('token');
const apiUrl = localStorage.getItem('apiUrl');
if (!token || !apiUrl) {
throw new Error('Not authenticated');
}
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
...options.headers
};
const response = await fetch(`${apiUrl}${endpoint}`, {
...options,
headers
});
// Handle token expiration
if (response.status === 401) {
localStorage.removeItem('token');
window.location.href = '/';
throw new Error('Session expired');
}
return response;
};
|