From eaffe0aaf413e8a5f8a1f38656e513e372bba87b Mon Sep 17 00:00:00 2001 From: Vighnesh Manjrekar Date: Thu, 30 Jun 2022 00:29:54 +0530 Subject: [PATCH] Fix: Hash password while updating it (admin) --- models/User.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/models/User.js b/models/User.js index 6c80f39..9fbafe7 100644 --- a/models/User.js +++ b/models/User.js @@ -58,6 +58,14 @@ UserSchema.pre('save', async function (next) { this.password = await bcrypt.hash(this.password, salt); }); +// Encrypt password using bcrypt while updating (admin) +UserSchema.pre("findOneAndUpdate", async function (next) { + if (this._update.password) { + this._update.password = await bcrypt.hash(this._update.password, 10); + } + next(); +}); + // Sign JWT and return UserSchema.methods.getSignedJwtToken = function () { return jwt.sign({ id: this._id }, process.env.JWT_SECRET, {