Answer the question
In order to leave comments, you need to log in
How to properly use spoilers in axios?
Hello, I wanted to learn from more experienced colleagues how to use interceptors correctly both when requesting and when responding. Is it possible to control routing on requests?
axios.interceptors.request.use(
function(config) {
if (!config.url.includes('Authenticate') && !config.headers.common['X-RANDOMIZE-TOKEN']) {
const token = localStorage.getItem('token');
if (token) {
config.headers.common['X-RANDOMIZE-TOKEN'] = token;
axios.defaults.headers.common['X-RANDOMIZE-TOKEN'] = token;
} else {
router.replace('/login');
return config;
}
}
return config;
},
function(error) {
// Do something with request error
return Promise.reject(error);
}
);
axios.interceptors.response.use(
response => {
return response;
},
function(error) {
EventBus.$emit('error-notify', error);
if (error.response.status === 401) {
router.replace('/login');
}
return Promise.reject(error.response);
}
);
Answer the question
In order to leave comments, you need to log in
I will show a few practices, how spoilers make life easier for me, and for many, in general.
Here and below, I will show exactly the use of interceptors when working with JWT authorization (tokens).
1) Authorization header substitution
const createSetAuthInterceptor = options => config => {
if (options.access) {
config.headers.Authorization = options.access;
} else {
delete config.headers.Authorization;
}
return config;
};
const setAuthCb = createSetAuthInterceptor(store.state.auth);
axios.interceptors.request.use(setAuthCb);
let refreshTokenPromise;
const createUpdateAuthInterceptor = (store, http) => async error => {
const message = get(error, 'response.data.message');
if (!['Token expired', 'Invalid token'].includes(message)) {
return Promise.reject(error);
}
if (!refreshTokenPromise) {
refreshTokenPromise = store.dispatch('refreshToken');
}
await refreshTokenPromise;
refreshTokenPromise = null;
return http(error.config);
};
const updateAuthCb = createUpdateAuthInterceptor(store, axios);
axios.interceptors.response.use(null, updateAuthCb);
It is possible - this is one of the practices, i.e. when you make a request to the api for the first time, for example, to get the current user, and in case you get an access error, go to the authorization page.
But you can also implement similar behavior using the view router hooks. Those. in the .beforeEach hook, you make a user request if you immediately switch to authorization in the exception:
store.dispatch('urrent_user')
.then(r => {
// next processing
})
.catch(e => {
// goto login
})
In general, interceptors for another
. For example, you can turn on the loading indicator on the "download start event", and turn it off on the "download end event"
And what about the connection between routing and Ajax, there are hooks in the view . Here in them you can process data when page transitions occur
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question