2022-09-20 19:59:52 +00:00
|
|
|
import axios from 'axios'
|
2023-12-01 17:57:14 +00:00
|
|
|
import { useAuthStore } from '../stores/auth';
|
2022-09-20 19:59:52 +00:00
|
|
|
import router from '~/router'
|
|
|
|
import Cookies from 'js-cookie'
|
|
|
|
|
|
|
|
function addPasswordToFormRequest (request) {
|
|
|
|
const url = request.url
|
|
|
|
if (!url.startsWith('/api/forms/')) return request
|
|
|
|
|
|
|
|
const slug = url.split('/')[3]
|
|
|
|
if (slug !== undefined && slug !== '' && Cookies.get('password-' + slug) !== undefined) {
|
|
|
|
request.headers['form-password'] = Cookies.get('password-' + slug)
|
|
|
|
}
|
|
|
|
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request interceptor
|
|
|
|
axios.interceptors.request.use(request => {
|
2023-12-01 17:57:14 +00:00
|
|
|
const authStore = useAuthStore()
|
|
|
|
const token = authStore.token
|
2022-09-20 19:59:52 +00:00
|
|
|
if (token) {
|
|
|
|
request.headers.common.Authorization = `Bearer ${token}`
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:57:14 +00:00
|
|
|
request.headers.common['Accept-Language'] = 'en-US'
|
2022-09-20 19:59:52 +00:00
|
|
|
// request.headers['X-Socket-Id'] = Echo.socketId()
|
|
|
|
|
|
|
|
request = addPasswordToFormRequest(request)
|
|
|
|
|
|
|
|
return request
|
|
|
|
})
|
|
|
|
|
|
|
|
// Response interceptor
|
|
|
|
axios.interceptors.response.use(response => response, error => {
|
2023-12-01 17:57:14 +00:00
|
|
|
const authStore = useAuthStore()
|
2022-09-20 19:59:52 +00:00
|
|
|
const { status } = error.response
|
|
|
|
if (status >= 500) {
|
|
|
|
console.log(status)
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:57:14 +00:00
|
|
|
if (status === 401 && authStore.check) {
|
|
|
|
authStore.logout()
|
2022-09-20 19:59:52 +00:00
|
|
|
router.push({ name: 'login' })
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
})
|