K
K
komigor2021-04-26 19:47:24
Node.js
komigor, 2021-04-26 19:47:24

Why isMatch is always false?

Why isMatch is always false? If you print candidatePassword and this.password, then they are the same.

userSchema.methods.comparePassword = function comparePassword(candidatePassword, cb) {
  bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
    cb(err, isMatch);
  });
};

Here is a function with password encryption:
userScema.pre('save', function preSave(next) {
    const user = this;
    if (!user.isModified('password')) { return next();}
        bcrypt.genSalt(10, (err, salt) => {
            if (err) {return next(err); }
            bcrypt.hash(user.password, salt, (err, hash) => {
                if (err) {return next(err);}
                user.password = hash;
                next();
            });
        });
    })


What is the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question