shrutiarora2906 / SendATask

Send a task app
0 stars 0 forks source link

securely store passwords #7

Open shrutiarora2906 opened 7 years ago

shrutiarora2906 commented 7 years ago

You can use bcrypt for the same:

// Execute before each user.save() call UserSchema.pre('save', function(callback) { var user = this;

// Break out if the password hasn't changed if (!user.isModified('password')) return callback();

// Password changed so we need to hash it bcrypt.genSalt(5, function(err, salt) { if (err) return callback(err);

bcrypt.hash(user.password, salt, null, function(err, hash) {
  if (err) return callback(err);
  user.password = hash;
  callback();
});

}); });