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

141
Visualizações
implementing Logout by clearing history in react?

Im building an application including react router and I am trying to Logout out in app. I'm using history.push('/') to redirect to home page but by clicking on back arrow I'm again logged in app and redirected to previous screen. Is there any way to protect this.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I usually solve this by defining public and protected routes. Public routes are the ones which should be available to all users (login, register etc). Protected routes are the ones which only authenticate users can access.

For instance, lets say that we are storing current user's authentication key in our state. If we have /login public and /home protected routes, and a logout button, we can simply define the following logic:

const Router = props => {
    const [key, setKey] = useState("Auth Key")
    
    const publicRoutes = [<Route path="/login" component={Login} />]
    const protectedRoutes = [<Route path="/home" component={Home} />]
    return (
        <Switch>
            {key === null ? publicRoutes : protectedRoutes}
            <button onClick={() => setKey(null)>Log out</button>}
        </Switch>
    )
}

This way, you will only allow authenticated users to access your protected routes. You need to find a better way of authenticating your users though, because in this case, if the client just changes his state from null to pretty much anything else, he will have full access to your protected routes. You will probably need to use some sort of JWT authentication key in order to verify user identity.

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