J
J
jeruthadam2017-08-11 01:26:12
MongoDB
jeruthadam, 2017-08-11 01:26:12

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: trueto 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')
        })
      })

I beg you to help, the brain is already boiling.
APD: I noticed that after this operation, the HASH stored in the database changes !!! But why???
Here, apparently, the problem is in my hook
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 ()
    })
  })
})

What can you think of?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Shashenkov, 2017-08-11
@jeruthadam

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 question

Ask a Question

731 491 924 answers to any question