text: import mongoose from 'mongoose';
  import bcrypt from 'bcrypt';
  
  const Schema = mongoose.Schema;
  
  // TODO: (http://mongoosejs.com/docs/guide.html) When your application starts up, Mongoose automatically calls createIndex for each defined index in your schema. Mongoose will call createIndex for each index sequentially, and emit an 'index' event on the model when all the createIndex calls succeeded or when there was an error. While nice for development, it is recommended this behavior be disabled in production since index creation can cause a significant performance impact. Disable the behavior by setting the autoIndex option of your schema to false, or globally on the connection by setting the option config.autoIndex to false.
  
  const userSchema = new Schema({
    email: { type: 'String', unique: true, required: true, trim: true },
    password: { type: 'String', required: true, trim: true },
    /* slug: { type    : 'String', required: true }, */
    cuid: { type: 'String', required: true },
    dateAdded: { type: 'Date', default: Date.now, required: true },
  });
  
  // hash/salt the password before saving
  userSchema.pre('save', function (next) {
    const user = this;
    bcrypt.hash(user.password, 10, function (err, hash) {
      if (err) {
        return next(err);
      }
      user.password = hash;
      next();
    })
  });
  
  // authenticate input against database
  userSchema.statics.authenticate = function (email, password, callback) {
    this.findOne({ email })
      .exec(function (err, user) {
        if (err) {
          return callback(err)
        } else if (!user) {
          var err = new Error('User not found.');
          err.status = 401;
          return callback(err);
        }
  
        bcrypt.compare(password, user.password, function (err, result) {
          if (result === true) {
            callback(null, user);
          }
          return callback(err);
        });
      });
  };
  
  userSchema.methods.checkPassword = function (candidatePassword, callback) {
    const user = this;
    bcrypt.compare(candidatePassword, this.password, function (err, isMatch) {
      if (isMatch) {
        return callback(null, user);
      }
      return callback(err);
    });
  };
  
  export default mongoose.model('User', userSchema);
  