text: var mongoose = require( 'mongoose' );
  var crypto = require('crypto');
  var jwt = require('jsonwebtoken');
  
  var personSchema = new mongoose.Schema({
    login: {
      type: String,
      unique: true,
      required: true
    },
    hash: String,
    salt: String
  });
  
  personSchema.methods.setPassword = function(password){
    this.salt = crypto.randomBytes(16).toString('hex');
    this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha512').toString('hex');
  };
  
  personSchema.methods.validPassword = function(password) {
    var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha512').toString('hex');
    return this.hash === hash;
  };
  
  personSchema.methods.generateJwt = function() {
    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),
    }, process.env.jwt_secret || "MY_SECRET");
  };
  
  mongoose.model('Person', personSchema);
  