I'm currently working on incorporating bcrypt into my mongoose model using typescript. Referencing this link as a guide.
However, since my project is in typescript, I'm unable to directly use the provided code. I'm confused about how they're fetching the password for comparison with the user's input.
Upon comparing the passwords, one of them appears to be undefined. Here's the snippet of my code, any assistance would be greatly appreciated.
PersonTestSchema.pre<PersonTestModel>('save', function (next) {
const user = this;
if (this.password && this.password.length > 4) {
bcrypt.genSalt(10, function (err, salt) {
bcrypt.hash(user.password, salt, (err, hash) => {
user.password = hash;
next();
});
});
} else {
next();
}
});
PersonTestSchema.methods.verifyPassword = function (candidatePassword: string) {
const user = this;
return bcrypt.compareSync(candidatePassword, user.password);
While logging the candidate password shows its value, checking the user's password returns empty. This makes sense in my scenario, but I'm unsure where they're obtaining it from based on their example code.