You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
import axios from 'axios'
|
|
|
|
|
|
|
|
|
|
const instance = axios.create({
|
|
|
|
|
baseURL: import.meta.env.VITE_API_URL || 'http://localhost:5091/api/god/',
|
|
|
|
|
timeout: 10000,
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
instance.interceptors.request.use(
|
|
|
|
|
(config) => {
|
|
|
|
|
const token = localStorage.getItem('auth_token')
|
|
|
|
|
if (token) {
|
|
|
|
|
config.headers.Authorization = `Bearer ${token}`
|
|
|
|
|
}
|
|
|
|
|
return config
|
|
|
|
|
},
|
|
|
|
|
(error) => Promise.reject(error)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
instance.interceptors.response.use(
|
|
|
|
|
(response) => response.data,
|
|
|
|
|
(error) => {
|
|
|
|
|
if (error.response?.status === 401) {
|
|
|
|
|
localStorage.removeItem('auth_token')
|
|
|
|
|
localStorage.removeItem('user')
|
|
|
|
|
window.location.href = '/login'
|
|
|
|
|
}
|
|
|
|
|
if (error.code === 'ERR_NETWORK' || error.code === 'ECONNABORTED' || !error.response) {
|
|
|
|
|
window.location.href = '/404'
|
|
|
|
|
}
|
|
|
|
|
return Promise.reject(error.response?.data || error.message)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export default instance
|