0

I'm working on a react app, the issue is that after logging in and click on back button inside browser I can see the login page,

What should be the best approach to prevent this?

  1. Create a middleware in server side to protect this routes?
  2. Create a function that checks when the user clicks back/forward button?

Can you guide me a little bit??

  • If you're using `react-router`, you can just have your login route redirect the user elsewhere if the user has already logged in, since you probably store some state about the logged in user in Redux after login. – cbr Jun 23 '20 at 17:24
  • Check out my solution here: https://stackoverflow.com/a/68599142/1991020 – Isaac Pak Jul 31 '21 at 04:41

2 Answers2

0

If you are using react-router you can use redirect in your login component

if (userIsLogged) {
  return <Redirect to={"/homepage"} />;
} 
ludwiguer
  • 2,177
  • 2
  • 15
  • 21
  • what if I need to call a function instead of routing to an specific route? –  Jun 23 '20 at 18:15
0

You can use Route Gurad Module

https://www.npmjs.com/package/react-router-guards or refer to this example as well.

How to restrict access to routes in react-router?

Mushahid
  • 18
  • 1
  • 10