39 lines
960 B
TypeScript
39 lines
960 B
TypeScript
const API_BASE_URL = import.meta.env.VITE_API_URL || "http://localhost:8080/api";
|
|
|
|
export async function authFetch(url: string, options?: RequestInit): Promise<Response> {
|
|
const token = localStorage.getItem('auth_token');
|
|
|
|
const headers: Record<string, string> = {
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
// Merge existing headers if provided
|
|
if (options?.headers) {
|
|
const existingHeaders = new Headers(options.headers);
|
|
existingHeaders.forEach((value, key) => {
|
|
headers[key] = value;
|
|
});
|
|
}
|
|
|
|
// Add Authorization header if token exists
|
|
if (token) {
|
|
headers['Authorization'] = `Bearer ${token}`;
|
|
}
|
|
|
|
const response = await fetch(url, {
|
|
...options,
|
|
headers,
|
|
});
|
|
|
|
// Handle 401: Token expired or invalid
|
|
if (response.status === 401) {
|
|
localStorage.removeItem('auth_token');
|
|
window.location.href = '/login';
|
|
throw new Error('Unauthorized');
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
export { API_BASE_URL };
|