Skip to content

Instantly share code, notes, and snippets.

@omarqureshi
Created September 26, 2011 20:51
Show Gist options
  • Save omarqureshi/1243377 to your computer and use it in GitHub Desktop.
Save omarqureshi/1243377 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var _ = require('underscore');
var models = {};
var Schema = mongoose.Schema, ObjectId = Schema.ObjectId;
var bcrypt = require('bcrypt');
var TimestampProperties = {
created_at : Date,
updated_at : Date
}
var UserProperties = {
email : { type: String, required: true, index: { unique: true } },
full_name : { type: String, required: true },
password_digest : { type: String, required: true }
}
var ContentProperties = {
title : String,
start_time : Date,
end_time : Date,
published : Boolean,
_author : { type: ObjectId, ref: 'User' }
}
generatePassword = function(password, _ret) {
return bcrypt.gen_salt(10, function(err, salt) {
return bcrypt.encrypt(password, salt, function(err, hash) {
_ret(err, hash);
});
});
}
_.extend(UserProperties, TimestampProperties);
_.extend(ContentProperties, TimestampProperties);
models.UserSchema = new Schema(UserProperties);
models.UserSchema.virtual('password').set(function(password) {
var hash = generatePassword(password, _ret);
console.log(hash);
});
models.UserSchema.virtual('password_confirmation').set(function(password) {
var hash = generatePassword(password, _ret);
});
models.ContentSchema = new Schema(ContentProperties);
models.User = mongoose.model('User', models.UserSchema);
models.Content = mongoose.model('Content', models.ContentSchema);
exports.User = models.User;
exports.Content = models.Content;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment