Spaces:
Runtime error
Runtime error
File size: 913 Bytes
20256e7 | 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 30 31 32 33 34 35 36 37 38 | import axios from 'axios';
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000/v1';
const client = axios.create({
baseURL: API_URL,
headers: {
'Content-Type': 'application/json',
},
});
client.interceptors.request.use(
(config) => {
const token = localStorage.getItem('token');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => {
return Promise.reject(error);
}
);
client.interceptors.response.use(
(response) => response,
(error) => {
if (error.response && error.response.status === 401) {
// Optional: Clear token and redirect to login if 401 occurs
// localStorage.removeItem('token');
// window.location.href = '/login';
}
return Promise.reject(error);
}
);
export default client;
|