diff --git a/app/models/user.js b/app/models/user.js index 4860387..dc7c764 100644 --- a/app/models/user.js +++ b/app/models/user.js @@ -31,24 +31,15 @@ var userSchema = mongoose.Schema({ }); -// checking if password is valid using bcrypt -userSchema.methods.validPassword = function(password) { - return bcrypt.compareSync(password, this.local.password); +// methods ====================== +// generating a hash +userSchema.methods.generateHash = function(password) { + return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); }; - -// this method hashes the password and sets the users password -userSchema.methods.hashPassword = function(password) { - var user = this; - - // hash the password - bcrypt.hash(user.local.password, null, null, function(err, hash) { - if (err) - return next(err); - - user.local.password = hash; - }); - +// checking if password is valid +userSchema.methods.validPassword = function(password) { + return bcrypt.compareSync(password, this.local.password); }; // create the model for users and expose it to our app diff --git a/config/passport.js b/config/passport.js index 14d7c10..852a331 100644 --- a/config/passport.js +++ b/config/passport.js @@ -59,7 +59,7 @@ module.exports = function(passport) { // set the user's local credentials newUser.local.email = email; - newUser.hashPassword(password); // use the generateHash function in our user model + newUser.local.password = newUser.generateHash(password); // use the generateHash function in our user model // save the user newUser.save(function(err) {