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

183
Vistas
Redirect user if not logged in react js jwt node js

There are already many answers but I couldn't use them according to my scenario in which my login component is a child component and i am using react-router-dom of 5.1.2 version currently i am using local storage to save authToken but I know it is not a good approach. Where should I save the auth token then such that whenever someone tries to hit login route manually or by button they are redirected because token was expired my route that i am currently using in app.js

 <Route path={process.env.PUBLIC_URL + "/login-register"} component={(!isVerified)?LoginRegister:HomeFashion}/>

and this is the child component login

const login = async()=>{
try{
  const options = {
username:username,
password:password

  }
var response = await axios.post(CONFIG.url+'api/login',options)
localStorage.setItem("__user",JSON.stringify(response.data))

history.push({
  pathname: '/',
  // search: '?update=true',  // query string
  state: {  // location state
    user: response.data, 
    
  },
});

}catch(err){
  isError(true)
}
}

React.useEffect(()=>{

  if(location.state!==undefined){
   setMsg(location.state.msg)
if(msg)
 {
   alert(msg)
 }
}

  },[location])

what i am doing is when i am successfully logged in i get token and user data in response form backend I store it in localStorage and redirects things according to it

what is the right way to do so?

about 4 years ago · Juan Pablo Isaza
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