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

126
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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