Guest User

Untitled

a guest
Aug 23rd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. (function() {
  2. var Schema, UserSchema, bcrypt, crypto, encryptPassword, mongoose, rbytes, shasum;
  3.  
  4. crypto = require('crypto');
  5.  
  6. shasum = crypto.createHash('sha1');
  7.  
  8. bcrypt = require('bcrypt');
  9.  
  10. mongoose = require('mongoose');
  11.  
  12. Schema = mongoose.Schema;
  13.  
  14. rbytes = require("rbytes");
  15.  
  16. encryptPassword = function(pw) {
  17. var salt;
  18. salt = bcrypt.gen_salt_sync(10);
  19. return [bcrypt.encrypt_sync(pw, salt), salt];
  20. };
  21.  
  22. UserSchema = new Schema({
  23. username: {
  24. type: String,
  25. required: true,
  26. unique: true
  27. },
  28. hashed_password: {
  29. type: String,
  30. required: true
  31. },
  32. password_salt: {
  33. type: String,
  34. required: true
  35. },
  36. created: {
  37. type: Date
  38. },
  39. updated: {
  40. type: Date,
  41. "default": Date.now
  42. },
  43. seats: [
  44. {
  45. type: Schema.ObjectId,
  46. ref: 'Seat'
  47. }
  48. ],
  49. payments: [
  50. {
  51. type: Schema.ObjectId,
  52. ref: 'Payment'
  53. }
  54. ],
  55. roles: [
  56. {
  57. type: Schema.ObjectId,
  58. ref: 'Role'
  59. }
  60. ]
  61. });
  62.  
  63. UserSchema.virtual('password').set(function(password) {
  64. var arr;
  65. this._password = password;
  66. arr = encryptPassword(password);
  67. this.password_salt = arr[1];
  68. console.log(arr[1]);
  69. console.log(arr[0]);
  70. this.hashed_password = arr[0];
  71. });
  72.  
  73. UserSchema.virtual('password').get(function() {
  74. return this._password;
  75. });
  76.  
  77. UserSchema.method("authenticate", function(plainText) {
  78. if (bcrypt.compare_sync(plainText, this.hashed_password)) {
  79. console.log('valid');
  80. this.generateToken;
  81. return true;
  82. } else {
  83. return false;
  84. }
  85. });
  86.  
  87. UserSchema.method("generateToken", function() {
  88. this.authentication_token = crypto.createHash("sha1").update(rbytes.randomBytes(2048)).digest("hex");
  89. return this.authentication_token;
  90. });
  91.  
  92. UserSchema.method("tokenAuthenticate", function(token) {
  93. return token === this.authentication_token;
  94. });
  95.  
  96. UserSchema.method("logout", function() {
  97. return this.authentication_token = void 0;
  98. });
  99.  
  100. mongoose.model('User', UserSchema);
  101.  
  102. module.exports = mongoose.model('User');
  103.  
  104. }).call(this);
Add Comment
Please, Sign In to add comment