Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
I want to restrict the page routing when user is logged in

I have made an application where I am implementing routing using react-router-dom.

So the scenario is when a user logs in, he is redirected to localhost:3000/home url and home page is getting rendered. When he logs out he is coming to landing page which is on localhost:3000.

But if the user changes the url to localhost:3000 when logged in, everything is coming blank and he is showing as logged in, I believe this is the default behavior of routing.

The requirement is, to redirect user to home page until he is logged in even if he changes url by typing to localhost in the browser.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

add a condition to check if the user is log in and use the Navigate from react-router-dom to redirect the user . add this in your component

   return (
    { ("add condition here to check if user is login") ? (
        <Navigate to="/home " />
      ) : (<div>Component body</div>)
    }
    )
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try, this if not log in then redirect to the login page and the login user then redirects to the login page.

 <HashRouter>
        {!auth.token && (
          <Routes>
            <Route path="/" element={<Login />} />
            <Route path="*" element={<Navigate to="/" />} />
          </Routes>
        )}
        {auth.token && (
          <>
              <Routes>
                <Route exact path="/home" element={<Home />} />
                <Route path="*" element={<Navigate to="/home" />} />
              </Routes>
          </>
        )}
</HashRouter>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Hello your problem is easy to solve just structure your routes this way:

<Route
    exact
    path="/login"
    render={() => {
    if (userLoggedIn) {
       return <Redirect to="/home" />;
    } 
    return <LoginPage />; 
    }}
/>

Or do it this way:

<Route
    exact
    path="/home"
    render={() => {
    if (!userLoggedIn) {
       return <Redirect to="/login" />;
    } 
    return <HomePage/>; 
    }}
/>

I hope this helps you, if you need any other clarification please mention me in the comments below

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda