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

153
Visualizações
Nodejs Ajax - redirect to the login page

I'm trying to solve this problem. I use Node Express and jQuery. The user has been logged out (from another page of the site), but is trying to send an ajax request. How to redirect it to the login page? I can throw an error like so:

if (!req.isAuthenticated()) {
    return res.json(function() {});
}

and after on the client side:

$.ajax({
    type: 'GET',
    ...
    error: function() {
        document.location.href = '/logout';
    }
})

But can it be done in some other way? For example, if you do not use Ajax, then I do this:

if (!req.isAuthenticated()) {
    return redirect('/login');
}

Something similar but with Ajax?

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

0

Since you mentioned you use Express, you can call the redirect function on the response:

if (!req.isAuthenticated()) {
    res.redirect("/login");
}
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