Advertisement
Guest User

Untitled

a guest
May 7th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. let mongoose = require('mongoose');
  2. let crypto = require('crypto');
  3. let jwt = require('jsonwebtoken');
  4.  
  5. let UserSchema = new mongoose.Schema({
  6. username: {
  7. type: String,
  8. lowercase: true,
  9. unique: true
  10. },
  11. hash: String,
  12. salt: String
  13. });
  14.  
  15. UserSchema.methods.setPassword = function(password) {
  16. this.salt = crypto.randomBytes(32).toString('hex');
  17. this.hash = crypto
  18. .pbkdf2Sync(password, this.salt, 10000, 64, 'sha512')
  19. .toString('hex');
  20. };
  21.  
  22. UserSchema.methods.validPassword = function(password) {
  23. let hash = crypto
  24. .pbkdf2Sync(password, this.salt, 10000, 64, 'sha512')
  25. .toString('hex');
  26. return this.hash === hash;
  27. };
  28.  
  29. UserSchema.methods.generateJWT = function() {
  30. var today = new Date();
  31. var exp = new Date(today);
  32. exp.setDate(today.getDate() + 60);
  33. return jwt.sign(
  34. {
  35. _id: this._id,
  36. username: this.username,
  37. exp: parseInt(exp.getTime() / 1000)
  38. },
  39. process.env.RECIPE_BACKEND_SECRET
  40. );
  41. };
  42.  
  43. mongoose.model('User', UserSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement