Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda