Guest User

Untitled

a guest
Aug 18th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. var bcrypt = require('bcrypt');
  2. var uuid = require('node-uuid');
  3. var SALT_WORK_FACTOR = 10;
  4.  
  5. module.exports = {
  6.  
  7. attributes: {
  8.  
  9. name: {
  10. type: 'string',
  11. required: true,
  12. minLength: 3,
  13. maxLength: 30
  14. },
  15.  
  16. username: {
  17. type: 'string',
  18. required: true,
  19. unique: true,
  20. minLength: 3,
  21. maxLength: 30
  22. },
  23.  
  24. email:{
  25. type: 'email',
  26. required: true,
  27. unique: true
  28. },
  29.  
  30. password:{
  31. type: 'string',
  32. required: true,
  33. minLength: 6,
  34. maxLength: 50
  35. },
  36.  
  37. keys: {
  38. collection: 'key',
  39. via: 'owner'
  40. },
  41.  
  42. verifyPassword: function (password) {
  43. return bcrypt.compareSync(password, this.password);
  44. },
  45.  
  46. changePassword: function(newPassword, cb){
  47. this.newPassword = newPassword;
  48. this.save(function(err, u) {
  49. return cb(err,u);
  50. });
  51. },
  52.  
  53. toJSON: function() {
  54. var obj = this.toObject();
  55. return obj;
  56. }
  57. },
  58.  
  59. beforeCreate: function (attrs, cb) {
  60. bcrypt.hash(attrs.password, SALT_WORK_FACTOR, function (err, hash) {
  61. attrs.password = hash;
  62. return cb();
  63. });
  64. },
  65.  
  66. beforeUpdate: function (attrs, cb) {
  67. if(attrs.newPassword){
  68. bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
  69. if (err) return cb(err);
  70.  
  71. bcrypt.hash(attrs.newPassword, salt, function(err, crypted) {
  72. if(err) return cb(err);
  73.  
  74. delete attrs.newPassword;
  75. attrs.password = crypted;
  76. return cb();
  77. });
  78. });
  79. }
  80. else {
  81. return cb();
  82. }
  83. }
  84. };
Add Comment
Please, Sign In to add comment