Saturday, 3 June 2017

Passport.js crashes the server when entering the wrong password

My passport.js file looks like this.

    passport.use(new LocalStrategy({
    usernameField: 'usernameOrEmail',
    passwordField: 'password',
    passReqToCallback: true,
    session: true
  },
  function(req, usernameOrEmail, password, done) {
    bookshelf.transaction((t) => {
      return User.login(usernameOrEmail, password, req, t)
    })
    .then((user) => done(null, user))
    .catch((err) => {
      if (err.message == 'EmptyResponse') {
        err.message = "The username you have requested does not exist. Kindly contact your administrator";
      }
      done(null, false, { message: err.message })
    });
  }
));

Also this is how i login

router.get('/login', (req, res, next) => {
  res.locals.notifications = []
  res.render('login');
});
router.post('/login',
  passport.authenticate('local', {
    successReturnToOrRedirect: '/dashboard',
    failureRedirect: '/login',
    failureFlash: true
  }));

The passport js is correctley logging in if the username and password is correct. also it is redirecting correctley if the username or password is wrong or both but it crashes the server how can i figure out what is going wrong and where? Or how to fix this?.



via arunwebber

No comments:

Post a Comment