if (!user.isModified('password')) return next();
Schema.pre('save', function (cb) { var currentUser = this; if (!currentUser.isModified('password')) return cb(); bcrypt.genSalt(5, function (err, salt){ if (err) return cb(err); bcrypt.hash(currentUser.password, salt, null, function (err, hash) { if (err) return cb(err); currentUser.password = hash; return cb(); }); }); });
// Hash the plain text password before saving userSchema.pre('save', async function(next) { const user = this; if (user.isModified('password')) { user.password = await bcrypt.hash(user.password, 8); } next(); });
UserSchema.pre('save', function (next) { const user = this if (user.isModified('password')) { bcrypt.genSalt(10, (err, salt) => { bcrypt.hash(user.password, salt, (err, hash) => { user.password = hash next() }) }) } else { next() } })
userSchema.pre('save', function(next) { const user = this; if (!user.isModified('password')) return next(); bcrypt.genSalt((saltError, salt) => { if (saltError) return next(saltError); bcrypt.hash(user.password, salt, (hashError, hash) => { if (hashError) return next(hashError); user.password = hash; next(); }); }); });
userSchema.pre('save', async function(next) { const user = this; if (!user.isModified('password')) { return next(); } const hashedPassword = await bcrypt.hash(user.password, 10); user.password = hashedPassword; next(); });
userSchema.pre('save',async function(next){ const user=this if(user.isModified('password')){ const salt=await bcryptjs.genSalt(10) const hsh=await bcryptjs.hash(user.password,salt) user.password=hsh } next() })
//validation and hashing UserSchema.pre('save', function (next) { var user = this; if (!user.isModified('password'))return next(); // hashing the password if no error and save it to database bcrypt.hash(user.password, null, null, function (err, hash) { if (err) { return next(err); } user.password = hash; next(); }); });
UserSchema.pre('save', function (next) { var user = this; if (user.isModified('password')) { bcrypt.genSalt(10, (err, salt) => { bcrypt.hash(user.password, salt, (err, hash) => { user.password = hash; next(); }); }); } else { next(); } });
//hash the plain text password before saving UserSchema.pre('save', async function(next){ const user = this if(user.isModified('password')){ user.password = await bcrypt.hash(user.password, 8) } next() })
//Hash password prior to saving userSchema.pre("save", async function(next) { const user = this; if (user.isModified("password")) { user.password = await bcrypt.hash(user.password, 8); } next(); });
UserSchema.pre('save', function(next){ var user = this; if(!user.isModified('password')) return next(); bcrypt.hash(user.password, null, null, function(err, hash){ if(err) return next(err); user.password = hash; next(); }); });
//hash the password before the user is saved UserSchema.pre('save', function(next) { var user = this; //hash the password only if the password has been changed or user is new if(!user.isModified('password')) return next(); bcrypt.hash(user.password, null,null,function(err,hash) { if (err) return next(err); //change the password to the hashed version user.password = hash; next(); }); });
userSchema.pre('save', function(next) { let user = this; if(user.isModified('password')) return next(); bcrypt.genSalt(10, (err, salt)=>{ if (err) return next(); bcrypt.hash(user.password, salt, null, (err, hash)=>{ if (err) return next(err); user.password = hash; next(); }) }); });
//hash the password method userSchema.pre('save' , function(next){ var user = this; if(!user.isModified('password')) return next(); bcrypt.hash(user.password, null , null , function(err , hash){ if(err) return next(err); //change password to hash user.password = hash; next(); }) })