blob: c8f1edae5022989fc69db18ce56892c47b4ae5f5 [file] [log] [blame]
Derick Montaguefded0d12019-12-11 06:16:40 -06001import Axios from 'axios';
Derick Montague126eaab2019-12-23 13:33:52 -06002import router from '../router';
Yoshie Muranaka68069e12020-05-15 08:06:46 -07003import store from '@/store';
Yoshie Muranaka74c24f12019-12-03 10:45:46 -08004
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -08005const api = Axios.create({
6 withCredentials: true
7});
Yoshie Muranaka74c24f12019-12-03 10:45:46 -08008
Derick Montague227c41a2019-12-20 17:08:59 -06009api.interceptors.response.use(undefined, error => {
10 let response = error.response;
Derick Montague126eaab2019-12-23 13:33:52 -060011
Derick Montague227c41a2019-12-20 17:08:59 -060012 // TODO: Provide user with a notification and way to keep system active
13 if (response.status == 401) {
Derick Montague676f2fc2019-12-23 20:53:49 -060014 if (response.config.url != '/login') {
15 window.location = '/login';
Yoshie Muranaka68069e12020-05-15 08:06:46 -070016 // Commit logout to remove XSRF-TOKEN cookie
17 store.commit('authentication/logout');
Derick Montague676f2fc2019-12-23 20:53:49 -060018 }
Derick Montague227c41a2019-12-20 17:08:59 -060019 }
Derick Montague126eaab2019-12-23 13:33:52 -060020
21 if (response.status == 403) {
22 router.push({ name: 'unauthorized' });
23 }
Derick Montague676f2fc2019-12-23 20:53:49 -060024
25 return Promise.reject(error);
Derick Montague227c41a2019-12-20 17:08:59 -060026});
27
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080028export default {
29 get(path) {
30 return api.get(path);
31 },
32 delete(path, payload) {
33 return api.delete(path, payload);
34 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070035 post(path, payload, config) {
36 return api.post(path, payload, config);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080037 },
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);
Yoshie Muranaka183c2752020-02-12 11:30:49 -080046 },
47 spread(callback) {
48 return Axios.spread(callback);
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080049 }
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080050};