I am implementing a feature to prevent unauthorized users from accessing personal account pages. Currently, Axios interceptors monitor error responses with a status of 401, indicating that a user is not authorized to view certain pages. If this status is detected, the user is automatically redirected to the login page using the following code:
axiosInstance.interceptors.response.use(null, (error) => {
if (error.response.status === 401) {
if (router.history.current.fullPath.includes('profile')) {
router.replace({
path: '/login',
});
}
this.$cookie.delete('token');
}
if (error.response.status === 404) {
router.push({ name: 'error' });
}
if (error.response.status === 500) {
router.push({ name: 'server_error' });
}
return Promise.reject(error);
});
While this functionality works as intended, I have noticed some odd behavior in the interface. When an unauthorized user attempts to access a restricted page, they briefly see the page before being redirected to the login page. Is there a way to skip this brief visibility and go straight to the login page without revealing the blocked page?