skynyrd
12/13/2016 - 3:39 PM

Salting a pass using bcrypt js and mongoose

Salting a pass using bcrypt js and mongoose

// JS, Mongoose, Mongo, ExpressJS, Authentication, Security. Bcrypt

// Before saving user object
userSchema.pre('save', function(next) {
  const user = this;
  
  //Generate a salt then run callback 10: number of rounds
  bcrypt.genSalt(10, function(err, salt){
    if (err) {return next(err)}
    
    //Hash the pass using salt
    bcrypt.hash(user.password, salt, null, function(err, hash){
      if (err) {return next(err)}
      
      user.password = hash;
      next();
    });
  });
});