Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!