v3.0.0/src/site/plugins/axios.js

33 lines
843 B
JavaScript
Raw Normal View History

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-12-24 15:45:16 +01:00
$axios.onRequest(config => {
if (store.state.auth.token) {
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 => {
if (process.env.NODE_ENV !== 'production') console.error('[AXIOS Error]', error);
2019-04-24 10:36:47 +02:00
if (process.browser) {
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
});
} else {
store.dispatch('alert/set', {
text: `[AXIOS]: ${error.message}`,
2020-12-24 09:40:50 +01:00
error: true
});
}
}
/* if (error.response?.data?.message.indexOf('Token expired') !== -1) {
store.dispatch('auth/logout');
} */
2019-04-24 10:36:47 +02:00
}
2019-03-28 16:35:22 +01:00
});
}