1
我试图使用Compoundjs,Passportjs(复合护照)和Bcryptjs实现本地身份验证。 这里是我的代码:NodeJS - 回调错误
定义新的战略
var Strategy = require('passport-local').Strategy;
passport.use(new Strategy({
usernameField: conf.usernameField || 'email'
}, exports.callback));
回调函数
exports.callback = function (email, password, done) {
exports.User.findOrCreate({
email: email,
password: password
}, function (err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(err, false);
}
var len = exports.User.verifyPassword.length;
if (len === 2) {
if (!exports.User.verifyPassword(password, user.password)) {
return done(err, false);
} else {
return done(err, user);
}
} else if (len === 3) {
exports.User.verifyPassword(password, user.password, function(err, isMatch) {
return done(err, !err && isMatch ? user : false);
});
}
return done(err, false);
});
};
User.verifyPassword
User.verifyPassword = function verifyPassword(password, hash, cb) {
bcrypt.compare(password, hash, function(err, isMatch) {
if(err) return cb(err);
return cb(null, isMatch);
});
};
在这种情况下,我得到以下错误:
Error: Can't set headers after they are sent.
它是指登录成功后重定向。 如果我使用verifyPassword没有回调(同步模式),它工作正常:
User.verifyPassword = function verifyPassword(password, hash) {
return bcrypt.compareSync(password, hash);
};
哪里是错误在我的代码?