Wednesday, 7 November 2018

[HELP] Hashing Updated Password in MongoDB

How do I hash the password in MongoDB with bcryptjs when a user update?​This is how I hash the password when a user create. Ref: https://ift.tt/2QrLj82 // POST /users/ userSchema.pre("save", function(next) { const user = this; if (!user.isModified("password")) { return next(); } bcrypt.genSalt(10, function(err, salt) { if (err) { return next(err); } bcrypt.hash(user.password, salt, function(error, hash) { if (error) { return next(error); } user.password = hash; next(); }); }); });This is my attempt to hash the updated password.ts // PUT /users/:id userSchema.pre("update", function(next) { bcrypt.hash(this.password, 10, (err: mongoose.Error, hash) => { this.password = hash; next(); }); });But it doesn't hash the password. Please help.​Tools: Mongoose, MongoDB Atlas, TypeScript, Express, Bcryptjs​

Submitted November 07, 2018 at 03:47PM by xzenuu

No comments:

Post a Comment