mirror of
https://github.com/stackblitz/bolt.new
synced 2025-06-26 18:17:50 +00:00
18 lines
452 B
TypeScript
18 lines
452 B
TypeScript
export async function authenticatedFetch(url: string, options: RequestInit = {}) {
|
|
const token = localStorage.getItem('token');
|
|
const headers = {
|
|
...options.headers,
|
|
'Authorization': `Bearer ${token}`,
|
|
};
|
|
|
|
const response = await fetch(url, { ...options, headers });
|
|
|
|
if (response.status === 401) {
|
|
// Token is invalid or expired
|
|
localStorage.removeItem('token');
|
|
window.location.href = '/login';
|
|
}
|
|
|
|
return response;
|
|
}
|