text: /*global require */
  
  var mongoose = require('mongoose'),
      crypto = require('crypto'),
      jwt = require('jsonwebtoken'),
      userSchema;
  
  userSchema = new mongoose.Schema({
      email: {
          type: String,
          unique: true,
          required: true
      },
      name: {
          type: String,
          required: true
      },
      hash: String,
      salt: String
  });
  
  userSchema.methods.setPassword = function (password) {
      'use strict';
  
      this.salt = crypto.randomBytes(16).toString('hex');
      this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
  };
  
  userSchema.methods.validPassword = function (password) {
      'use strict';
  
      var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
  
      return this.hash === hash;
  };
  
  userSchema.methods.generateJwt = function () {
      'use strict';
  
      var expiry = new Date();
  
      expiry.setDate(expiry.getDate() + 7);
  
      return jwt.sign({
          _id: this._id,
          email: this.email,
          name: this.name,
          exp: parseInt(expiry.getTime() / 1000, 10)
      }, "MY_SECRET_KEY"); // DO NOT KEEP YOUR SECRET IN THE CODE!
  };
  
  mongoose.model('User', userSchema);