Why Passport with Local Strategy is hanging on Authentication

Posted on
If you are using custom callback for authentication using Passportjs for Local Database Authentication, you may encounter the situation that the Post request hangs and the authentication never seems to end. There may be many reasons but what we’ve found most common is we are using passport.authenticate() middleware in a callback but do not provide (req, res, next) to this middleware. So, in the snippet below, check line number 12 in which we provide (req, res, next) to middleware
router.post('/login', function(req, res, next) {
    passport.authenticate('local', (err, user, info) => {
      if (err) { return next(err) }
      if (!user) {
        return res.render('/login', {
          form: req.body
        });
      }
      req.logIn(user, function(err) {
        if (err) { return next(err); }
        return res.redirect('/myaccount');
      });
    })(req, res, next); // <=== Do not forget to add these.
});

Leave a Reply

Your email address will not be published. Required fields are marked *