我正在努力宣传passport.js’s local strategy。我对承诺和护照都很陌生,而且我非常依赖this comment thread,它涉及使用蓝鸟的Promise库向护照的done()
回调传递额外的参数。这个评论产生了一个new bluebird implementation来处理额外的回调参数,但是我无法在我自己的代码中使用它:
const passport = require('passport');
const User = require('../models/user');
const LocalStrategy = require('passport-local');
const NoMatchedUserError = require('../helpers/error_helper').NoMatchedUserError;
const NotActivatedError = require('../helpers/error_helper').NotActivatedError;
const localOptions = { usernameField: 'email' };
const localLogin = new LocalStrategy(localOptions, function(email, password, done) {
let user;
User.findOne({ email: email }).exec()
.then((existingUser) => {
if (!existingUser) { throw new NoMatchedUserError('This is not a valid email address.'); }
user = existingUser;
return user.comparePassword(password, user.password);
})
.then((isMatch) => {
if (!isMatch) { throw new NoMatchedUserError('This is not a valid password.'); }
return user.isActivated();
})
.then((isActivated) => {
if (!isActivated) { throw new NotActivatedError('Your account has not been activated.'); }
return user;
})
.asCallback(done, { spread: true });
});
用户可以毫无问题地进行验证。这是我的身份验证失败:done(null, false, { message: ‘message’}
显然没有在.asCallback
方法中调用。我很确定它与抛出错误有关,所以我尝试使用它:
if (!existingUser) { return [ false, { message: 'This is not a valid email address.' } ]; }
但是返回一个数组也不起作用,因为它传递给了promise链并破坏了代码。
有什么想法吗?