Reputation: 139
Can we use axios interceptors at app.js instead of index.js? I just want to know if there is any drawback of using it inside app.js or inside any other file instead of index.js. For my scenario, I have to check if user is authorized for every back-end request call, and have to update local state on the basis of interceptor's result.
Upvotes: 2
Views: 642
Reputation: 870
You can create a interceptor file and include it any when you want to use axios
import axios from 'axios';
const axiosInterceptor = axios.interceptors.response.use((response) => {
if (response.status === 401) {
console.log("You are not authorized");
//redirect
}
return response;
}, (error) => {
if (error.response && error.response.data) {
return Promise.reject(error.response.data);
}
return Promise.reject(error.message);
});
export default axiosInterceptor;
And in your component
import axiosInterceptor from './utils/axiosInterceptor.js'
Upvotes: 1