According to the documentation https://reactrouter.com/docs/en/v6/examples/auth and common sense it is good practice to 'replace' login routes after success authentication.
The use of navigate("...", { replace: true }) to replace the /login route in the history stack so the user doesn't return to the login page when clicking the back button after logging in
Everything is clean if there is only one login route.
Question: what to do if there are multiple routes in authentication flow?
For example: /login/step-one-> /login/step-two -> /login/step-blabla. Number of steps may depend on user settings and/or authentication conditions. it would also be correct to let the user navigate between steps back in the authorization process (for example, choosing a different confirmation method).
Issues:
- Sample code from the documentation shows how to replace only the last step so history stack will be
/login/step-one->/login/step-two->/. So back button follows to a meaningless location. - It is possible to
navigate(-N)and then replace it. But number of steps back is unknown becauseuseLocationdoesn't providestate.idxto calculate difference between begin and end of authentication. Underlaying library hasgetIndexAndLocationmethod but it doesn't exported in react-router. https://github.com/remix-run/history/blob/3e9dab413f4eda8d6bce565388c5ddb7aeff9f7e/packages/history/index.ts#L370