После ЧАСОВ рытья в приложении я наконец нашел причину, почему мои пароли перестают совпадать - это происходит когда юзер верифицирует почту и сохраняется статус
active: true
в базу!!!
Но теперь я в недоумении как это исправить? И почему такой конфлиг, что хеши не совпадают?
ВОт мой код апдейта статуса юзера:
User.findOne({email: decoded.sub}, function (err, userFound) {
if (err) { console.log('error') }
if (!userFound) {
res.status(401).send({
message: 'Unable to verify email. User not found.'
})
console.log('User not found')
return
}
if (!userFound.active) {
userFound.active = true
console.log('Changed to active')
}
userFound.save(function(err) {
if (err) { return next(err) }
console.log(userFound.active)
console.log('User saved')
res.status(201).send('Status changed')
// res.status(201).redirect('http://localhost:8080/secretquote')
})
})
Очень прошу помочь, мозг уже закипает.
АПД: заметил, что после этой операции меняется сохраненный в базе ХЭШ!!! Но почему???
Вот, судя по всему проблема в моем хуке
userSchema.pre('save', function (next) {
const user = this
bcrypt.genSalt(10, function (err, salt) {
if (err) { return next (err) }
bcrypt.hash(user.password, salt, function (err, hash) {
if (err) { return next (err) }
// ovewrite plain text password with hash
user.password = hash
next ()
})
})
})
Что можно придумать?