Bcrypt.compare acting super weird

So I store my passwords in a database using bcryp.hash when the user signs up.
And then when I try to login, it actually works… until it stops working! I can login maybe 3-4 times using the same login and then the compare method suddenly returns false and I have to create a new user who can log in until he can’t… SUPER WEIRD RIGHT?!

Here’s my code for the signIn route :

exports.signIn = async (req, res, next) => {
try {

if (!req.body.email || !req.body.password) res.redirect('/')

const user = await User.findOne( {email: req.body.email })
const match = await bcrypt.compare(req.body.password, user.password)

if (match) {
  req.session.email = user.email   
  req.session.userid = user._id   
  res.redirect('/weather')
} else {
  res.redirect('/')
}

} catch (err) {
console.log(err)
}
}

Ok I found out the big WHY.
I had a pre save middleware to hash the password whenever a new user document is saved, and the problem was on one of my other route when I update the document using the .save() method … and so the password was re-hashed !

Sorry for bothering you with this unsolvable question since I didn’t give you all the necessary informations.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.