ffplayout/plugins/axios.js

50 lines
1.6 KiB
JavaScript
Raw Normal View History

export default function ({ $axios, store, redirect, route }) {
2020-04-30 10:53:13 +02:00
$axios.onRequest((config) => {
const token = store.state.auth.jwtToken
if (token) {
config.headers.common.Authorization = `Bearer ${token}`
}
2022-06-30 18:39:01 +02:00
// disable progress on auth
if (config.url.includes('auth') || config.url.includes('process') || config.url.includes('current')) {
2020-04-30 10:53:13 +02:00
config.progress = false
}
})
2020-05-24 17:52:05 +02:00
$axios.interceptors.response.use((response) => {
return response
}, (error) => {
const originalRequest = error.config
// prevent infinite loop
2022-06-30 18:39:01 +02:00
if (error.response.status === 401 && route.path !== '/') {
2020-05-24 17:52:05 +02:00
store.commit('auth/REMOVE_TOKEN')
redirect('/')
return Promise.reject(error)
}
if (error.response.status === 401 && !originalRequest._retry && !originalRequest.url.includes('auth/token')) {
2020-05-24 17:52:05 +02:00
originalRequest._retry = true
2022-06-30 18:39:01 +02:00
store.commit('auth/REMOVE_TOKEN')
store.commit('auth/UPDATE_IS_LOGIN', false)
redirect('/')
2020-05-24 17:52:05 +02:00
}
return Promise.reject(error)
})
2020-04-30 10:53:13 +02:00
$axios.onError((error) => {
const code = parseInt(error.response && error.response.status)
if (code === 401 && route.path !== '/') {
2020-04-30 10:53:13 +02:00
redirect('/')
} else if (code !== 401 && code !== 409 && code !== 503) {
2021-10-31 15:49:21 +01:00
store.commit('UPDATE_VARIANT', 'danger')
store.commit('UPDATE_SHOW_ERROR_ALERT', true)
store.commit('UPDATE_ERROR_ALERT_MESSAGE', error)
2020-04-30 10:53:13 +02:00
}
return error.response
2020-04-30 10:53:13 +02:00
})
}