Files
Aether/frontend/src/services/api.ts
T

32 lines
877 B
TypeScript

import axios from 'axios';
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:8080/api/v1';
const apiClient = axios.create({
baseURL: API_BASE_URL,
withCredentials: true,
headers: {
'Content-Type': 'application/json',
},
});
// Interceptor для обработки ошибок
apiClient.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) {
// Redirect to login if unauthorized, but not if already on public pages
const publicPaths = ['/auth', '/verify-email', '/reset-password'];
const currentPath = window.location.pathname;
const isPublicPage = publicPaths.some(path => currentPath.startsWith(path));
if (!isPublicPage) {
window.location.href = '/auth';
}
}
return Promise.reject(error);
}
);
export default apiClient;