mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-11-23 10:44:59 +01:00
Migrating frontend to Vue3
This commit is contained in:
@@ -1,19 +1,16 @@
|
||||
import axios from 'axios'
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
import router from '../router'
|
||||
import store from '../store'
|
||||
import { translate } from 'vue-gettext'
|
||||
import { bus } from '@/main'
|
||||
import router from '@/router'
|
||||
import { useAuthStore } from '@/stores'
|
||||
|
||||
const _axios = axios.create()
|
||||
const { gettext: $gettext } = translate
|
||||
|
||||
_axios.interceptors.request.use(
|
||||
function (config) {
|
||||
const authStore = useAuthStore()
|
||||
// Do something before request is sent
|
||||
if (store.state.auth.isAuthenticated) {
|
||||
config.headers['Accept-Language'] = store.state.auth.authUser.language
|
||||
if (authStore.isAuthenticated) {
|
||||
config.headers['Accept-Language'] = authStore.authUser.language
|
||||
}
|
||||
return config
|
||||
},
|
||||
@@ -35,35 +32,41 @@ _axios.interceptors.response.use(
|
||||
return Promise.reject(error)
|
||||
}
|
||||
if (error.response.status === 429) {
|
||||
bus.$emit('notification', { msg: $gettext('You are throttled, please try later.'), type: 'error' })
|
||||
return Promise.reject(error)
|
||||
}
|
||||
if (error.response.status !== 401 || router.currentRoute.path === '/login/') {
|
||||
if (error.response.status !== 401 || router.currentRoute.name === 'Login') {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
const refreshToken = Cookies.get('refreshToken')
|
||||
const authStore = useAuthStore()
|
||||
if (error.config.url.endsWith('/token/refresh/') || !refreshToken) {
|
||||
store.dispatch('auth/logout')
|
||||
if (router.history.current.name !== 'Login') {
|
||||
authStore.$reset()
|
||||
if (router.currentRoute.name !== 'Login') {
|
||||
router.push({ name: 'Login' })
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
return _axios.post('/token/refresh/', { refresh: refreshToken }).then(resp => {
|
||||
Cookies.set('token', resp.data.access, { sameSite: 'strict' })
|
||||
_axios.defaults.headers.common.Authorization = `Bearer ${resp.data.access}`
|
||||
const config = error.config
|
||||
config.headers.Authorization = `Bearer ${resp.data.access}`
|
||||
return new Promise((resolve, reject) => {
|
||||
_axios.request(config).then(resp => {
|
||||
resolve(resp)
|
||||
}).catch(error => {
|
||||
reject(error)
|
||||
return _axios
|
||||
.post('/token/refresh/', { refresh: refreshToken })
|
||||
.then((resp) => {
|
||||
Cookies.set('token', resp.data.access, { sameSite: 'strict' })
|
||||
_axios.defaults.headers.common.Authorization = `Bearer ${resp.data.access}`
|
||||
const config = error.config
|
||||
config.headers.Authorization = `Bearer ${resp.data.access}`
|
||||
return new Promise((resolve, reject) => {
|
||||
_axios
|
||||
.request(config)
|
||||
.then((resp) => {
|
||||
resolve(resp)
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error)
|
||||
})
|
||||
})
|
||||
})
|
||||
}).catch(error => {
|
||||
Promise.reject(error)
|
||||
})
|
||||
.catch((error) => {
|
||||
Promise.reject(error)
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user