I am currently following this auth0 guide to protect my routes. If a user enters my guarded path, it will correctly lead me to my auth0 universal login page. However, if for 'x' reason a user decides to click on the browser's back button, it will return to the guarded path which will once again redirect me to the auth0 universal login page! I would be creating a ProtectedRoute component similar to the tutorial:
<Route
component={withAuthenticationRequired(component, {
onRedirecting: () => <Loading />,
})}
{...args}
/>
Is there a way for me to redirect to the previous page before reaching the guarded path when I click on the browser's back button?
What I had in mind was to call a 'componentWillMount' function to check if the page was reached by calling the back button and then I would call history.push('/');
const useWillMount = (fn) => {
const willMount = useRef(true);
if (willMount.current && fn && typeof fn === 'function') {
fn();
}
willMount.current = false;
};
useWillMount(() => {
if (
String(window.performance.getEntriesByType('navigation')[0].type) ===
'back_forward'
) {
history.push('/');
}
});
Using this, it correctly redirects to the home page. However, after a few milliseconds, it immediately throws me back to auth0 universal login page. (I believe this happens because it renders the ProtectedRoute component even though I called the history.push). Am I going towards the correct path, or am I completely lost here?