bcrypt.compare() always comes back false with this code in the user model. This is with bcrypt-nodejs.
User.pre('save', function (callback) {
this.password = bcrypt.hashSync(this.password, bcrypt.genSaltSync(10))
this.token = jwt.sign(this.email, process.env.JWT_SECRET)
callback()
})
User.methods.verifyPassword = function ( password ) {
const self = this
return Q.Promise( (resolve, reject) => {
bcrypt.compare( password, self.password, (error, isMatch) => {
if (error) reject( new Error("Error checking user password."))
resolve(isMatch)
})
})
}
I can see that a hash appears in the database. I can see the that right password comes into the verifyPassword function.
EDIT: The issue seems to be that .pre('save', ... occurs twice in a row. So the newly hashed password gets hashed again.