2020-12-24 15:45:16 +01:00
|
|
|
export default function({ $axios, store }) {
|
2020-12-25 12:45:22 +01:00
|
|
|
$axios.setHeader('accept', 'application/vnd.chibisafe.json');
|
2020-07-02 01:50:55 +02:00
|
|
|
|
2020-12-24 15:45:16 +01:00
|
|
|
$axios.onRequest(config => {
|
2020-07-02 01:50:55 +02:00
|
|
|
if (store.state.auth.token) {
|
2020-07-08 03:00:12 +02:00
|
|
|
config.headers.common.Authorization = `bearer ${store.state.auth.token}`;
|
2019-04-24 10:36:47 +02:00
|
|
|
}
|
2019-03-28 16:35:22 +01:00
|
|
|
});
|
|
|
|
|
2020-12-24 15:45:16 +01:00
|
|
|
$axios.onError(error => {
|
2020-07-04 02:53:06 +02:00
|
|
|
if (process.env.NODE_ENV !== 'production') console.error('[AXIOS Error]', error);
|
2019-04-24 10:36:47 +02:00
|
|
|
if (process.browser) {
|
2020-07-04 02:53:06 +02:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
if (error.response?.data?.message) {
|
|
|
|
store.dispatch('alert/set', {
|
|
|
|
text: error.response.data.message,
|
2020-12-24 09:40:50 +01:00
|
|
|
error: true
|
2020-07-04 02:53:06 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
store.dispatch('alert/set', {
|
|
|
|
text: `[AXIOS]: ${error.message}`,
|
2020-12-24 09:40:50 +01:00
|
|
|
error: true
|
2020-07-04 02:53:06 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 15:02:23 +02:00
|
|
|
|
2020-07-10 00:13:51 +02:00
|
|
|
/* if (error.response?.data?.message.indexOf('Token expired') !== -1) {
|
2020-07-02 01:50:55 +02:00
|
|
|
store.dispatch('auth/logout');
|
2020-07-10 00:13:51 +02:00
|
|
|
} */
|
2019-04-24 10:36:47 +02:00
|
|
|
}
|
2019-03-28 16:35:22 +01:00
|
|
|
});
|
|
|
|
}
|