Implemented update password functionality
This commit is contained in:
1 parent
1ba37a7ec9
commit
5a6cbcd325
4 files changed
+51
-1
No files matched your search
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user