Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. /*User Model found at ./models/User*/
  2.  
  3. var mongoose = require('mongoose');
  4. var crypto = require('crypto');
  5. var jwt = require('jsonwebtoken');
  6.  
  7. var UserSchema = new mongoose.Schema({
  8. username: {type:String, lowercase:true, unique:true},
  9. email: {type:String, unique:true},
  10. hash: String,
  11. salt: String,
  12. pollsCreated:[{type:mongoose.Schema.Types.ObjectId, ref:'Post'}],
  13. pollsVoted:[{type:mongoose.Schema.Types.ObjectId, ref:'Post'}]
  14. }, {collection:'Users'});
  15.  
  16.  
  17. UserSchema.methods.setPassword = function(password){
  18. this.salt = crypto.randomBytes(16).toString('hex');
  19. this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
  20. };
  21.  
  22. UserSchema.methods.validPassword = function(password){
  23. var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
  24. return this.hash === hash;
  25. };
  26.  
  27. UserSchema.methods.generateJWT = function(){
  28. var expiry = new Date();
  29. expiry.setDate(expiry.getDate() + 7);
  30.  
  31. return jwt.sign({
  32. _id:this._id,
  33. email:this.email,
  34. username:this.username,
  35. exp: parseInt(expiry.getTime() / 1000),
  36. }, "R3l3ntl3$$");
  37. };
  38.  
  39. mongoose.model('User', UserSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement