In my React JS (Vite + TS) app
Used solution in https://stackoverflow.com/a/53256982/22404569
I am using it for refreshing tokens, BE sends httpOnly
cookies as access_tokes so need for the FE to handle tokens.
Problem: I want to redirect user to /login when '/tokens-renew' API fails
My approach
I tried to do this using handelLogout()
. This approach cannot be done because hooks can only be called inside of a functional component.
I believe handelLogout()
is crucial as it manages all the flags that helps in keeping private route
Please note refreshing of tokens and retries of API is working perfectly fine, gets stuck in case of 401 for /tokens-renew
and i want to redirect user.
export const handleLogout = () => {
const dispatch = useDispatch();
const showToast = useToast();
const navigate = useNavigate();
// Clear the local storage and dispatch the signOut action to update the Login flag to false
localStorage.clear();
dispatch(signOut()); // Dispatch the action to sign out the user (e.g., clear authentication state in Redux)
navigate("/login"); // Navigate to the login page
showToast("Session expired.", EToastTypes.WARNING); //A custom Hook for Toast msg
};
let isTokenRenewing = false;
...
axios.interceptors.response.use(
res => res,
async (err) => {
// In case of token expire
if (err.response.status === 401 && !isTokenRenewing)//Cheks if any API has 401 and there was no req made prior to this
{
isTokenRenewing = true;
let url = import.meta.env.VITE_API_BASEURL + '/tokens-renew';
try {
const response = await axios.get(url);
if (response.status === 200) {
return axios(err.config)
}
}
catch (err) {
// For case request /tokens-renew got 401
// handleLogout()
}
}
// For case request was already made and got 401
else {
// handleLogout()
}
return Promise.reject(err);
}
);