armsultan
5/20/2013 - 5:29 PM

Example of using bcrypt with mongoose middleware to enforce password hashing with bcrypt on save.

Example of using bcrypt with mongoose middleware to enforce password hashing with bcrypt on save.

var mongoose = require('mongoose'),                                                                                                                                            
    Schema = mongoose.Schema                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                               
var bcrypt = require('bcrypt')                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                                                              
var UserSchema = new Schema( {                                                                                                                                                 
    email: String,                                                                                                                                                             
    password: String                                                                                                                                                           
} )                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                               
// pre                                                                                                                                                                         
UserSchema.pre('save', function(next) {                                                                                                                                        
    if(this.password) {                                                                                                                                                        
        var salt = bcrypt.genSaltSync(10)                                                                                                                                     
        this.password  = bcrypt.hashSync(this.password, salt)                                                                                                                
    }                                                                                                                                                                          
    next()                                                                                                                                                                     
})                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                               
mongoose.model('User', UserSchema);