Created
January 29, 2013 19:37
-
-
Save pfried/4667001 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(mongoose) { | |
var hasher = require('pbkdf2-hasher'); | |
var toLowerCase = function(string) { | |
return string.toLowerCase(); | |
} | |
// Generate a new password hash | |
var hashPassword = function(password, callback) { | |
hasher.generate(password, function(err, hash) { | |
callback(err, hash); | |
}); | |
}; | |
var accountSchema = new mongoose.Schema({ | |
email : { type : String, unique : true, required : true, trim : true, set : toLowerCase }, | |
_password : { type : String }, | |
name : { | |
first : { type : String }, | |
last : { type : String } | |
} | |
}); | |
accountSchema.virtual('password').get( function () { | |
return this._password; | |
}); | |
accountSchema.pre('save', true, function hook (next, done) { | |
next(); | |
done(); | |
//this.setPassword(); | |
}); | |
accountSchema.methods.setPassword = function (newPassword, callback) { | |
var that = this; | |
hashPassword(newPassword, function(err, hash) { | |
that._password = hash; | |
callback(err, that); | |
}); | |
}; | |
var Account = mongoose.model('Account', accountSchema); | |
return Account; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment