Bcrypt을 몽구스 내가 사용자 개체가 다시 빈 객체로오고, 따라서 this.password
이 정의되지 user.js
bcrypt 여권 오류 및
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 */
다시이 함수 추적 Incorrect arguments
오류를 던지고있다. bcrypt.compare의 this
매개 변수가 userSchema
인스턴스를 참조한다고 가정합니다. userSchema 내가 확실히 무엇으로 문제가 사용자 개체가 MongoDB를에서 암호화 된 암호 필드에 반환되고, user.comparepassword()
가 호출 것 같다 ...
나는 확실하지 않다 passport.js
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: '[email protected]',
password: '$2a$10$lAJ9hoGKt9ggfk1TISfkOedxDIs/waLB5e4PccHAKt286XCKCY0/q',
__v: 0 } */
에 선언 동일한 Schema 객체로 사용자를 서명했습니다.
도움/팁 감사드립니다!
당신은 또한 직렬화 누락 될 수도 있습니다이 부분을 역 직렬화 [체크 아웃이 explaination] (http://stackoverflow.com/questions/28691215/when-is-the -serialize-and-deserialize-passport-method-called-what-does-it-exact) – Clay
하이 클레이! passport.js에서 내 코드가 사용자 객체를 가져옵니다. console.log 문의 텍스트는 아래쪽에 주석으로 표시됩니다. 그 메소드에서 user.comparePassword를 호출하기 때문에, 변수로 'this'를 인수로 사용하여 bcrypt.compareSync에 의해 user.js에서 액세스 할 수 있다고 가정합니다. 성공적으로 가져온 개체를 전달할 수없는 이유가 있습니까? 고맙습니다 – Chris