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, {