Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1.  
  2. 'use strict';
  3.  
  4. const async = require('async');
  5. const crypto = require('crypto');
  6. const lib = require('../lib');
  7.  
  8.  
  9. module.exports = (mongoose) => {
  10. let Schema = mongoose.Schema;
  11.  
  12. let UserSchema = new Schema({
  13. id: {
  14. type: Number,
  15. unique: true,
  16. required: true
  17. },
  18. login: {
  19. type: String,
  20. unique: true,
  21. required: true
  22. },
  23. created: {
  24. type: Date,
  25. default: Date.now
  26. },
  27. hashedPassword: {
  28. type: String,
  29. required: true
  30. },
  31. salt: {
  32. type: String,
  33. required: true
  34. },
  35. imageId: {
  36. type: Number,
  37. default: 0
  38. }
  39. });
  40.  
  41. UserSchema.method('encryptPassword', encryptPassword);
  42. UserSchema.method('checkPassword', checkPassword);
  43.  
  44. UserSchema.static('authorize', authorize);
  45. UserSchema.static('create', create);
  46. UserSchema.static('signin', signin);
  47. UserSchema.static('safeFields', 'id login created');
  48.  
  49. UserSchema.virtual('password')
  50. .set(function(password) {
  51. this._plainPassword = password;
  52. this.salt = Math.random() + '';
  53. this.hashedPassword = this.encryptPassword(password);
  54. });
  55.  
  56. mongoose.model('User', UserSchema);
  57. }
  58.  
  59.  
  60. // statics
  61. function authorize(login, callback) {
  62. let User = this;
  63.  
  64. async.waterfall([
  65. (cb) => {
  66. User.findOne(
  67. {
  68. login: login
  69. },
  70. User.safeFields,
  71. cb
  72. );
  73. },
  74. (user, cb) => {
  75. return cb(null, user);
  76. }
  77. ], callback);
  78. }
  79.  
  80.  
  81. function create(params, cb) {
  82. let User = this;
  83.  
  84. lib.getDocumentId(User, (err, id) => {
  85. if (err) {
  86. return cb(err);
  87. }
  88.  
  89. let user = new User({
  90. id: id,
  91. login: params.login,
  92. password: params.password
  93. });
  94.  
  95. user.save(cb);
  96. });
  97. }
  98.  
  99.  
  100. function signin(params, cb) {
  101. let User = this;
  102.  
  103. User.findOne({ login: params.login }, (err, user) => {
  104. if (err) {
  105. return cb(err);
  106. }
  107.  
  108. // create if we dont find by login
  109. if (!user) {
  110. return User.create(params, cb);
  111. }
  112.  
  113. if (user && !user.checkPassword(params.password)) {
  114. return cb('Passwords not match');
  115. }
  116.  
  117. cb(null, user);
  118. });
  119. }
  120.  
  121.  
  122.  
  123. // methods
  124. function encryptPassword(password) {
  125. return crypto
  126. .createHmac('sha1', this.salt)
  127. .update(password)
  128. .digest('hex');
  129. }
  130.  
  131.  
  132. function checkPassword(password) {
  133. return this.encryptPassword(password) === this.hashedPassword;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement