1import Axios from 'axios'; 2import router from '../router'; 3import store from '@/store'; 4 5const api = Axios.create({ 6 withCredentials: true 7}); 8 9api.interceptors.response.use(undefined, error => { 10 let response = error.response; 11 12 // TODO: Provide user with a notification and way to keep system active 13 if (response.status == 401) { 14 if (response.config.url != '/login') { 15 window.location = '/login'; 16 // Commit logout to remove XSRF-TOKEN cookie 17 store.commit('authentication/logout'); 18 } 19 } 20 21 if (response.status == 403) { 22 router.push({ name: 'unauthorized' }); 23 } 24 25 return Promise.reject(error); 26}); 27 28export default { 29 get(path) { 30 return api.get(path); 31 }, 32 delete(path, payload) { 33 return api.delete(path, payload); 34 }, 35 post(path, payload, config) { 36 return api.post(path, payload, config); 37 }, 38 patch(path, payload) { 39 return api.patch(path, payload); 40 }, 41 put(path, payload) { 42 return api.put(path, payload); 43 }, 44 all(promises) { 45 return Axios.all(promises); 46 }, 47 spread(callback) { 48 return Axios.spread(callback); 49 } 50}; 51