Bcrypt正在抛出Incorrect arguments
错误,我在[{1}}
user.js
用户对象将作为空对象返回,因此userSchema.methods.comparePassword = (candidatePassword, callback) => {
bcrypt.compare(candidatePassword, this, (err, isMatch) => {
console.log('candidatePassword= ', candidatePassword, '& this= ', this);
if (err) { return callback(err); }
callback(null, isMatch);
});
};
/*
candidatePassword= bird
this= {}
this.password= undefined */
未定义。我假设bcrypt.compare中的this.password
参数引用this
实例。 userSchema在userSchema
passport.js
我不确定是什么问题,因为看起来用户对象是从mongodb返回的加密密码字段,并且const passport = require('passport');
const ExtractJwt = require('passport-jwt').ExtractJwt;
const JwtStrategy = require('passport-jwt').Strategy;
const LocalStrategy = require('passport-local').Strategy;
const User = require('../models/user');
const config = require('../config');
var localOptions = {
usernameField: 'email',
};
// Verifies user by checking if a password matches the specified email during signin
var localStrategy = new LocalStrategy(localOptions, function (email, password, done) {
User.findOne({ email:email.toLowerCase()}, function (err, user) {
console.log('/passport.js/localStrategy- user object: ', user)
if (err) { return done(err); }
if (!user) { return done(null, false); }
user.comparePassword(password, function (err, isMatch) {
console.log('/passport.js/localStrategy- password: ', password)
if (err) { return done(err); }
if (!isMatch) { return done(err, false); }
return done(null, user);
});
});
});
// ... jwt strategy ...
passport.use(localStrategy);
/*
user object: { _id: 58a1018dc3f89eb5955b8638,
email: 'bird@bird.com',
password: '$2a$10$lAJ9hoGKt9ggfk1TISfkOedxDIs/waLB5e4PccHAKt286XCKCY0/q',
__v: 0 } */
被调用...
我也使用相同的Schema对象为用户签名。
任何帮助/提示赞赏!
答案 0 :(得分:0)
您只是设置模型,以便拉入candidatePassword,但永远不会从数据库中找到存储的密码。由于这是返回一个空对象,因此电子邮件未匹配或密码未与存储的内容进行比较。尝试简化comparePassword函数并将'sync'添加到bcrypt.compare中,这样就无需回调。
在模特中:
userSchema.methods.comparePassword = (candidatePassword) => {
return bcrypt.compareSync(candidatePassword, this.password);
};