redirection does not work
Unanswered
American black bear posted this in #help-forum
American black bearOP
axiosUser.interceptors.response.use(
(response) => {
return response;
},
(error) => {
if (error.response && error.response.status === 403) {
Cookies.remove('auth_token');
const router = useRouter();
return router.push('/auth/login');
}
return Promise.reject(error);
}
);I'm using axios to check all requests made here.
If the status is 403, it is because the token is invalid and the user must go to the login page.
But when this happens, nothing happens, the user doesn't go to login, he just stays on the same page.
I already tried with redirect()
What could it be?
1 Reply
@American black bear js
axiosUser.interceptors.response.use(
(response) => {
return response;
},
(error) => {
if (error.response && error.response.status === 403) {
Cookies.remove('auth_token');
const router = useRouter();
return router.push('/auth/login');
}
return Promise.reject(error);
}
);
I'm using axios to check all requests made here.
If the status is 403, it is because the token is invalid and the user must go to the login page.
But when this happens, nothing happens, the user doesn't go to login, he just stays on the same page.
I already tried with redirect()
What could it be?
Gharial
Hooks can only be used in the body of a function component so it (useRouter) won't work in an axios interceptor. You would need to try returning a redirect object in getServerSideProps (if that's what you're using) or using Next's res.writeHead(403, { Location: '/auth/login' }) followed by res.end() in API routes.