UNPKG

create-exam-project

Version:

Create exam projects with React + Express + PostgreSQL in seconds

39 lines (33 loc) 788 B
import axios from 'axios' const API_URL = '/api' const api = axios.create({ baseURL: API_URL, 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?.status === 401) { localStorage.removeItem('token') window.location.href = '/login' } return Promise.reject(error) } ) export default api