/
Codename-Nik
/
WebApp
Обзор
Документация
Войти
/
Codename-Nik
/
WebApp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
frontend/src/utils/axios.ts
47 строк
1 KB
Nikolay Pokhodnya
first commit
03 ноя 2025, 19:47
03 ноя 2025, 19:47
a9fbdee
Код
Авторство
О чём код?
import axios from 'axios' import { useAuthStore } from '@/stores/auth' const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000/api' const api = axios.create({ baseURL: API_BASE_URL, timeout: 10000, headers: { 'Content-Type': 'application/json', } }) api.interceptors.request.use( (config) => { const token = localStorage.getItem('token') if (token) { config.headers.Authorization = `Bearer ${token}` } return config }, (error) => { return Promise.reject(error) } ) api.interceptors.response.use( (response) => response, (error) => { if (!error.response) { console.error('Network error - backend is not reachable:', error.message) return Promise.reject(new Error('Не удалось подключиться к серверу. Проверьте интернет-соединение.')) } if (error.response?.status === 401) { const auth = useAuthStore() auth.logout() if (!window.location.pathname.includes('/login')) { window.location.href = '/login' } } return Promise.reject(error) } ) export default api