Answer the question
In order to leave comments, you need to log in
Why does comparing password hashes in the controller with bcrypt fail?
I store password hashes in Mongu. During the LOGIN, I check the hashes and if ok, I return the status. At first everything worked, but now for some reason it stopped, and I did not change the code. Where is the error here?
exports.login = function (req, res, next) {
User.findOne({ email: req.body.email }, function (err, user) {
// DB error
if (err) {
res.send({
type: false,
data: "Error occured: " + err
})
} else {
if (user) {
user.comparePassword(req.body.password, function (err, isMatch) {
if (err) {
res.send('wierd error')
}
if (!isMatch) {
console.log(isMatch)
res.status(422).send({error: 'Wrong password'})
// res.send('wrong password')
} else {
res.status(201).send({
user: user, // test
// id_token: createIdToken(user),
access_token: createAccessToken(user),
// token: createIdToken(user),
data: req.body
})
}
})
} else {
res.status(422).send({error: 'The user is not registered'})
// res.send({
// type: false,
// data: "The user is not registered"
// })
}
}
})
}
userSchema.methods.comparePassword = function (candidatePassword, callback) {
bcrypt.compare(candidatePassword, this.password, function (err, isMatch) {
if (err) {
console.log('err')
return callback (err)
}
console.log('ok')
callback (null, isMatch)
})
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question