在一些帮助下,我得到了以下代码来承诺一个 passport.js 登录策略。
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var Promise = require('bluebird');
var bcrypt = require('bcrypt');
var db = require('./db').db; //users are stored in mongo
//I'm using bluebird.js for promises
var users = Promise.promisifyAll(db.users);
var compare = Promise.promisify(bcrypt.compare);
// This strategy is used by passport to handle logins
module.exports.localStrategy = new LocalStrategy(function(username, password, done) {
users.findOneAsync({username: username}).bind({})
.then(function(user) {
if (!user) {
throw new NoMatchedUserError('Incorrect username.');
//should be equivalent to:
// return done(null, false, {message:'something'});
}
this.user = user;
return compare(password, user.password);
})
.then(function(isMatch) {
if (isMatch) {
return this.user;
//is equivalent to:
// return done(null, this.user);
}
else {
throw { message: 'Incorrect password.' };
//should be equivalent to:
// return done(null, false, {message:'something else'};
}
})
.nodeify(done);
});
通过调用nodeify(done)我可以处理密码匹配的路径,但我不知道如何传递可选的第三个参数,以便 passport.js 可以使用它。
是否可以处理两个失败(不是错误)路径?
更新:
正如评论中所问的那样,我在 Github 上创建了一个问题,并且(非常迅速地)在 Bluebird v2.0 中添加了此功能