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

127
Visualizações
ExpressJS Post request not redirecting user upon submission

I am creating a webapp using NodeJS and ExpressJS, I have a login form on my website that when completed, sends a post request to the server. The server handles this request and finds them in the MongoDB database, it even attaches the session key as a cookie and the user ends up receiving that, but they are not being redirected to the dashboard page, or even redirected at all. Their session does not time out and the browser is not waiting on a response.

app.post('/login', function(req, res){
    var email = req.body.email;
    var password = req.body.password;

    User.findOne({'email' : email}, function(err, user){
        if (err)
        {
            console.log(err);
        }
        if (user)
        {
            var hash = user.password;
            if (passwordManagement.verifyPassword(password, hash))
            {
                var newSession = webSession.generateKey(email)
                webSession.SessionKeys.push(newSession);
                res.cookie('session', newSession.key);
                res.redirect('/dashboard')
            }
        }
        else
        {
            res.redirect('/login');
        }
    })
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are some control path issues present in your code.

  1. You are not exiting, if there is an error with finding the user by email.
  2. You are not doing anything, if user present for the email, but password does not match.

You might want to rewrite as follows:

app.post('/login', function(req, res, next){
    var email = req.body.email;
    var password = req.body.password;

    User.findOne({'email' : email}, function(err, user){
        if (err)
        {
            console.log(err);
            return next(err);
        }

        if(!user) {
          return res.redirect('/login');
        }
        var hash = user.password;
        if (!passwordManagement.verifyPassword(password, hash))
        {
           return res.redirect('/login');
        }

        var newSession = webSession.generateKey(email)
        webSession.SessionKeys.push(newSession);
        res.cookie('session', newSession.key);
        res.redirect('/dashboard')
    })
});

Note that, I have added next parameter to route handler & using it in case of error. Plus, I have rewritten the code for better readability.

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