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';
|
|
|
|
|
|
|
|
let token = document.head.querySelector('meta[name="csrf-token"]');
|
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Vue.axios.interceptors.request.use(function (request) {
|
|
|
|
|
|
|
|
const authToken = localStorage.getItem('jwt')
|
|
|
|
|
|
|
|
if(authToken) {
|
|
|
|
request.headers.common['Authorization'] = 'Bearer ' + authToken
|
|
|
|
}
|
|
|
|
|
|
|
|
request.headers.common['Content-Type'] = 'application/json'
|
|
|
|
|
|
|
|
return request
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
Vue.axios.interceptors.response.use(response => response, error => {
|
|
|
|
|
2020-01-26 21:57:53 +01:00
|
|
|
// Return the error when it has been asked
|
|
|
|
if( error.config.hasOwnProperty('returnError') && error.config.returnError === true ) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( error.response.status === 422 ) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
2020-01-26 21:04:33 +01:00
|
|
|
|
2020-01-26 21:57:53 +01:00
|
|
|
// Otherwise we push to the error views
|
|
|
|
if ( error.response.status === 404 ) {
|
2020-01-27 13:55:14 +01:00
|
|
|
|
|
|
|
router.push({name: '404'})
|
|
|
|
throw new Vue.axios.Cancel('pushed to 404');
|
2020-01-26 21:04:33 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-01-26 21:57:53 +01:00
|
|
|
router.push({ name: 'genericError', params: { err: error.response } })
|
2020-01-27 13:55:14 +01:00
|
|
|
throw new Vue.axios.Cancel('pushed to generic error');
|
2020-01-26 21:04:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
})
|