Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. var mongoose = require('mongoose');
  2. var crypto = require('crypto');
  3. var jwt = require('jsonwebtoken');
  4. var config = require('../config/auth');
  5.  
  6. var userSchema = new mongoose.Schema({
  7. email: {
  8. type: String,
  9. unique: true,
  10. required: true
  11. },
  12. username: {
  13. type: String,
  14. required: true
  15. },
  16. salt: String,
  17. hash: String,
  18. premium: Boolean,
  19. admin: Boolean
  20. });
  21.  
  22. userSchema.methods.setPassword = function (password) {
  23. this.salt = crypto.randomBytes(16).toString('hex');
  24. this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
  25. };
  26.  
  27. userSchema.methods.checkPassword = function (password) {
  28. var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
  29. return this.hash === hash;
  30. };
  31.  
  32. userSchema.methods.generateJwt = function () {
  33. var expiry = new Date();
  34. expiry.setDate(expiry.getDate() + 7);
  35.  
  36. return jwt.sign({
  37. _id: this._id,
  38. exp: parseInt(expiry.getTime() / 1000)
  39. }, config.secretKey);
  40. };
  41.  
  42. module.exports = mongoose.model('User', userSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement