Redirect to previous path on login - React Router v4

后端 未结 4 605
时光取名叫无心
时光取名叫无心 2020-12-31 02:51

I am using react router v4 and trying to implement a functionality where no matter what route the user clicks on, he gets taken to the login page i.e. if he is not logged in

4条回答
  •  情书的邮戳
    2020-12-31 03:39

    Following this example you can make a component PrivateRoute to wrap Route and use that whenever you need a route that requires auth.

    This is the component code from the example.

    const PrivateRoute = ({ component: Component, ...rest }) => (
         (
                fakeAuth.isAuthenticated ? 
                () 
                : 
                (
                  
                )
            )}
        />
    )
    

提交回复
热议问题