Implemented update password functionality

This commit is contained in:
Alex Holliday
2024-07-10 11:51:27 -07:00
parent 1ba37a7ec9
commit 5a6cbcd325
4 changed files with 51 additions and 1 deletions
+10
View File
@@ -55,6 +55,16 @@ UserSchema.pre("save", async function (next) {
next();
});
UserSchema.pre("findOneAndUpdate", async function (next) {
const update = this.getUpdate();
if ("password" in update) {
const salt = await bcrypt.genSalt(10); //genSalt is asynchronous, need to wait
update.password = await bcrypt.hash(update.password, salt); // hash is also async, need to eitehr await or use hashSync
}
next();
});
UserSchema.methods.comparePassword = async function (submittedPassword) {
res = await bcrypt.compare(submittedPassword, this.password);
return res;