You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.0 KiB
JavaScript

2 months ago
import axios from 'axios'
import router from './router'
const api = axios.create({
baseURL: import.meta.env.VITE_API_URL,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
});
2 months ago
2 months ago
api.interceptors.request.use(
(config) => {
const token = localStorage.getItem('token')
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
},
(error) => {
return Promise.reject(error)
}
)
2 months ago
2 months ago
api.interceptors.response.use(
(response) => {
return response
},
async (error) => {
const originalRequest = error.config
2 months ago
2 months ago
if (error.response?.status === 401 && !originalRequest._retry) {
originalRequest._retry = true
2 months ago
2 months ago
localStorage.removeItem('token')
localStorage.removeItem('user')
2 months ago
router.push('account/auth/login')
2 months ago
return Promise.reject(error)
}
2 months ago
2 months ago
if (error.response?.status === 403) {
router.push('/unauthorized')
}
return Promise.reject(error)
}
)
export default api