I would like to restrict access to my router and disabled the back button.
For example, if the user manually types on the browser tab or click in an URL besides event/:eventId
, I want to redirect it to event/:eventId
Current:
- The user visits
event/1234/friends
. - It gets redirected to
event/1234
- The user clicks the back button and is be able to see
event/1234/friends
. (It should not be able to visitevent/1234/friends
).
Note: This bug only happens on mobile. You can't reproduce it on desktop.
Desired:
- The user visits
event/1234/friends
. - It gets redirected to
event/1234
- Disable the back button or if the user clicks the back button, won't be able to visit
event/1234/friends
This how my code looks like
const eventPathname = props.history?.location?.pathname;
const matches = eventPathname.match(/^\/event\/([^/]+)\/.+$/);
if (!!matches) {
const defaultPathname = `/event/${matches[1]}`;
props.history.length = 1;
window.location.replace(defaultPathname);
props.history.push(defaultPathname);
}
And here is a code sandbox:
https://codesandbox.io/s/keen-silence-47ztr
Remember that you can't reproduce it on desktop, only on mobile.
I checked several threads on StackOverflow like 1, 2, 3, 4 but I couldn't find a proper answer. Any help of how can I achieve this?