mirror of
https://github.com/ferdzo/iotDashboard.git
synced 2026-04-05 09:06:26 +00:00
37 lines
960 B
TypeScript
37 lines
960 B
TypeScript
import axios from 'axios';
|
|
|
|
// Use Vite proxy in development, or env variable in production
|
|
const API_BASE_URL = import.meta.env.VITE_API_URL || '/api';
|
|
|
|
export const apiClient = axios.create({
|
|
baseURL: API_BASE_URL,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
withCredentials: true, // For session auth
|
|
});
|
|
|
|
// Add request interceptor for JWT token (if using JWT)
|
|
apiClient.interceptors.request.use((config) => {
|
|
const token = localStorage.getItem('access_token');
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
return config;
|
|
});
|
|
|
|
// Add response interceptor for error handling
|
|
apiClient.interceptors.response.use(
|
|
(response) => response,
|
|
(error) => {
|
|
if (error.response?.status === 401) {
|
|
// Handle unauthorized - redirect to login
|
|
localStorage.removeItem('access_token');
|
|
// window.location.href = '/login';
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
export default apiClient;
|