I want to handle 401 unauthorised error when my server outputs it , I want to dispatch a action in order to do that . I am seeing that many are using axios.interceptors how do I go about it . what are interceptors? Please explain in detail as to what it is and help me . Im new to react-redux framework. Here is my route handler in express:
router.get('/api/me', function(req, res) {
if(req.user) {
res.status(200).send({
email : req.user.local.email,
isCurrentUser: true
});
}else {
res.status(401).send({
isCurrentUser: false
})
}
})
here is my async action creator:
export const fetchCurrentUser = () => {
return async (dispatch) => {
const res = await axios.get(`${ROOT_URL}/me`);
if(res.status === 200) {
dispatch({ type: types.YES_FETCH_CURRENT_USER, payload: res.data });
}else if(res.status === 401) {
dispatch({type: types.NO_FETCH_CURRENT_USER})
}
}
};