Passportjs自定义callback – “密码错误”消息

对于常规authentication,“错误密码”消息可通过failureFlash获得

app.post('/login', passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login', failureFlash: true }) ); passport.use(new LocalStrategy( function(username, password, done) { User.findOne({ username: username }, function (err, user) { if (err) { return done(err); } if (!user) { return done(null, false, { message: 'Incorrect username.' }); } if (!user.validPassword(password)) { return done(null, false, { message: 'Incorrect password.' }); } return done(null, user); }); } )); 

但是,如果我使用自定义callback,我怎样才能访问“不正确的密码”的消息,并显示给用户? 因为自定义callback似乎只检查(!用户)。 我需要自定义callback,因为我通过ajax进行login,我不能有redirect。

 app.get('/login', function(req, res, next) { passport.authenticate('local', function(err, user, info) { if (err) { return next(err); } if (!user) { return res.status(401).send({"ok": false}); } req.logIn(user, function(err) { if (err) { return next(err); } return return res.send({"ok": true}); }); })(req, res, next); }); 

该消息在自定义callback处理程序的info参数中,使访问和发送给用户非常容易。

另一方面,我不会指定用户名或密码是否是login失败的原因。 使用这种响应很容易testing哪些用户名存在,然后只关注密码。