aboutsummaryrefslogtreecommitdiff
path: root/src/site/plugins/axios.js
blob: cff149c1f2219fcb2a081db18e7d1c4709f79e89 (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
export default function({ $axios, store }) {
	$axios.setHeader('accept', 'application/vnd.lolisafe.json');

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

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

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