Skip to content

Instantly share code, notes, and snippets.

@pfried
Created January 29, 2013 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfried/4667001 to your computer and use it in GitHub Desktop.
Save pfried/4667001 to your computer and use it in GitHub Desktop.
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