1

I want to restrict some routes if my user is logged in, if the user is logged in and tries to go to /login it would redirect him to the home page otherwise it would redirect him to the login page.

I am using Laravel API with reactjs. However when I try to accomplish what I require, it does not work, and gives me this error "Uncaught Error: [Navigate] is not a <Route> component"

I tried using <Redirect/> but it is replaced in react-router-dom v6

Here is what I tried.

import { BrowserRouter as Router, Route, Routes, Navigate} from "react-router-dom";
import Login from "./Login";

       <Router>
            <Routes>
                <Route path="/" element={<Main/>}/>
                <Route path="/login">
                    {localStorage.getItem("auth_token") ? <Navigate to="/"/> : <Route path="/login" element={<Login/>}/>}
                </Route>
                <Route path="/login">
                    {localStorage.getItem("auth_token") ? <Navigate to="/"/> : <Route path="/register" element={<Register/>}/>}
                </Route>
            
                {/* <Route path="/login" element={<Login/>}/>
                <Route path="/register" element={<Register/>}/> */}
            </Routes>
        </Router>

EDIT:

Here is what I tried based on some other stackoverflow questions, it is still not redirecting to the login page if the user is not logged in and the auth token is not found.

  <Route path="/login" element={localStorage.getItem("auth_token") ? <Navigate to="/" /> : <Login/>}/>
motionless570
  • 925
  • 1
  • 10
  • 26
  • Does this answer your question? [Navigate is not a component. All component children of must be a or ](https://stackoverflow.com/questions/70171991/navigate-is-not-a-route-component-all-component-children-of-routes-must-be) – twjs Jul 10 '22 at 10:53
  • Duplicate of https://stackoverflow.com/questions/70171991/navigate-is-not-a-route-component-all-component-children-of-routes-must-be – twjs Jul 10 '22 at 10:55
  • @twjs this solved half the problem, now when i am logged in and i log out it just doesn't let redirect to the login page. Please check my edited code – motionless570 Jul 10 '22 at 11:04
  • : Login}/> Login doesn't need to be wrapped in tags – twjs Jul 10 '22 at 11:11
  • @twjs i tried your suggestion, still not working and is giving me this error when trying to access /login `Functions are not valid as a React child. This may happen if you return a Component instead of from render. Or maybe you meant to call this function rather than return it.` – motionless570 Jul 10 '22 at 11:14
  • I see okay, so the previous syntax was correct. What happens exactly when a user logs out ? What page are they redirected to ? Maybe you can redirect them directly to login page using the useNavigate hook -> https://reactrouter.com/docs/en/v6/hooks/use-navigate – twjs Jul 10 '22 at 11:25
  • @twjs when a user log out he should go to the home page. how can i do that with use navigate, everytime i try to use it it gives me this error `useNavigate() may be used only in the context of a Router component` – motionless570 Jul 10 '22 at 11:37
  • Check out the implementation provided in the docs, might help -> https://stackblitz.com/github/remix-run/react-router/tree/main/examples/auth?file=src%2FApp.tsx – twjs Jul 10 '22 at 12:29

3 Answers3

0

You should use in this case.

<Route exact path="/login">
  {localStorage.getItem("auth_token") ? <Redirect to="/" /> : <Route path="/login" element={<Login />}/>}
</Route>

I was glad to answer you, I hope it helps you!

Crenat
  • 31
  • 3
0

Maybe, you can go inside your Main component and write this line of code, Now I don't know how you are extracting the data whether the user is logged in or not, but I had a similar situation and I used local storage to check whether the user is logged in or not

    if(!loggedInUser) {
        return <Navigate to="/login" />
    }
Mridul Gupta
  • 807
  • 1
  • 6
  • 8
0

Go to Login.js, then

***import { useNavigate } from "react-router-dom"***


 const Login = () => { 

   ***const navigate = useNavigate()
   if(localStorage.getItem("auth_token")) {
    navigate('/')
    }***

}