aboutsummaryrefslogtreecommitdiff
path: root/src/site/plugins/axios.js
blob: cdcfc019653e432196aabda6df842af7198fb345 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
export default function({ $axios, store }) {
	$axios.setHeader('accept', 'application/vnd.hostess.json');

	$axios.onRequest(config => {
		if (store.state.auth.token) {
			config.headers.common.Authorization = `bearer ${store.state.auth.token}`;
		}
	});

	$axios.onError(error => {
		if (process.env.NODE_ENV !== 'production') console.error('[AXIOS Error]', error);
		if (process.browser) {
			if (process.env.NODE_ENV !== 'production') {
				if (error.response?.data?.message) {
					store.dispatch('alert/set', {
						text: error.response.data.message,
						error: true
					});
				} else {
					store.dispatch('alert/set', {
						text: `[AXIOS]: ${error.message}`,
						error: true
					});
				}
			}

			/* if (error.response?.data?.message.indexOf('Token expired') !== -1) {
				store.dispatch('auth/logout');
			} */
		}
	});
}