Answer the question
In order to leave comments, you need to log in
Why do passwords no longer match after changing the status and saving it to the database?
After HOURS of digging through the app, I finally found the reason why my passwords don't match - it happens when the user verifies the mail and the status is saved active: true
to the database!!!
But now I'm at a loss how to fix it? And why is there such a config that the hashes do not match?
Here is my user status update code:
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 ()
})
})
})
Answer the question
In order to leave comments, you need to log in
Do you happen to overwrite your password? Prolog the data that arrives to the hashing function.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question