2020-01-26 21:04:33 +01:00
|
|
|
import Vue from 'vue'
|
|
|
|
import axios from 'axios'
|
|
|
|
import VueAxios from 'vue-axios'
|
|
|
|
import router from './routes.js'
|
|
|
|
|
|
|
|
Vue.use(VueAxios, axios)
|
|
|
|
|
|
|
|
Vue.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
|
|
|
|
|
2021-10-22 14:18:13 +02:00
|
|
|
// let token = document.head.querySelector('meta[name="csrf-token"]');
|
2020-01-26 21:04:33 +01:00
|
|
|
|
2021-10-22 14:18:13 +02:00
|
|
|
// if (token) {
|
|
|
|
// Vue.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
|
|
|
|
// } else {
|
|
|
|
// console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
|
|
|
|
// }
|
2020-01-26 21:04:33 +01:00
|
|
|
|
|
|
|
Vue.axios.interceptors.request.use(function (request) {
|
|
|
|
|
|
|
|
request.headers.common['Content-Type'] = 'application/json'
|
|
|
|
return request
|
|
|
|
})
|
|
|
|
|
|
|
|
Vue.axios.interceptors.response.use(response => response, error => {
|
|
|
|
|
2020-01-27 21:13:21 +01:00
|
|
|
// Return the error when we need to handle it at component level
|
2020-01-26 21:57:53 +01:00
|
|
|
if( error.config.hasOwnProperty('returnError') && error.config.returnError === true ) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
|
2020-01-27 21:13:21 +01:00
|
|
|
// Return the error for form validation at component level
|
2020-01-26 21:57:53 +01:00
|
|
|
if( error.response.status === 422 ) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
2020-01-26 21:04:33 +01:00
|
|
|
|
2020-01-27 21:13:21 +01:00
|
|
|
// Otherwise we push to a specific or generic error view
|
|
|
|
let routeName = 'genericError'
|
2020-01-27 13:55:14 +01:00
|
|
|
|
2020-02-27 09:30:02 +01:00
|
|
|
if ( error.response.status === 401 ) {
|
|
|
|
routeName = 'login'
|
|
|
|
}
|
2022-05-14 13:45:12 +02:00
|
|
|
|
2022-05-19 15:50:06 +02:00
|
|
|
if ( error.response.status === 407 ) {
|
|
|
|
router.push({ name: 'genericError', params: { err: error.response, closable: false } })
|
|
|
|
throw new Vue.axios.Cancel();
|
|
|
|
}
|
|
|
|
|
2022-05-14 13:45:12 +02:00
|
|
|
// api calls are stateless so when user inactivity is detected
|
|
|
|
// by the backend middleware it cannot logout the user directly
|
|
|
|
// so it returns a 418 response.
|
2022-07-13 14:56:25 +02:00
|
|
|
// We catch the 418 response and push the user to the autolock view
|
|
|
|
if ( error.response.status === 418 ) routeName = 'autolock'
|
2020-02-27 09:30:02 +01:00
|
|
|
|
2020-01-27 21:13:21 +01:00
|
|
|
if ( error.response.status === 404 ) routeName = '404'
|
2020-01-26 21:04:33 +01:00
|
|
|
|
2020-01-27 21:13:21 +01:00
|
|
|
router.push({ name: routeName, params: { err: error.response } })
|
|
|
|
throw new Vue.axios.Cancel();
|
2020-01-26 21:04:33 +01:00
|
|
|
|
|
|
|
})
|