{"size": 964, "ext": "js", "max_stars_repo_path": "server/models/user.js", "max_stars_repo_name": "scottjason/react-reflux-todos", "max_stars_repo_stars_event_min_datetime": "2015-12-27T00:21:26.000Z", "max_stars_repo_stars_event_max_datetime": "2015-12-27T00:21:26.000Z", "max_issues_repo_path": "server/models/user.js", "max_issues_repo_name": "scottjason/react-reflux-todos-with-authentication", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/models/user.js", "max_forks_repo_name": "scottjason/react-reflux-todos-with-authentication", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 19.6734693878, "max_line_length": 67, "alphanum_fraction": 0.5850622407}
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');

var toLower = function toLower(str) {
  return str.toLowerCase();
};

var userSchema = new mongoose.Schema({
  email: {
    type: String,
    set: toLower
  },
  password: {
    type: String
  },
  todos: {
    default: [],
    type: Array
  },
  createdAt: {
    type: Number
  },
  updatedAt: {
    type: Number
  }
});

userSchema.pre('save', function(cb) {
  
  this.createdAt = (this.createdAt) ? this.createdAt : Date.now();
  this.updatedAt = Date.now();

  if (this.password) {
    var _this = this;
    if (!_this.isModified('password')) return cb();
    bcrypt.genSalt(5, function(err, salt) {
      if (err) return cb(err);
      bcrypt.hash(_this.password, salt, null, function(err, hash) {
        if (err) return cb(err);
        _this.password = hash;
        return cb();
      });
    });
  } else {
    cb();
  }
});

module.exports = mongoose.model('User', userSchema);