Advertisement
Guest User

Untitled

a guest
Jan 14th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. 'use strict';
  2.  
  3. /**
  4. * Module dependencies.
  5. */
  6.  
  7. const mongoose = require('mongoose');
  8. const crypto = require('crypto');
  9.  
  10. const Schema = mongoose.Schema;
  11. const oAuthTypes = [
  12. 'github',
  13. 'twitter',
  14. 'facebook',
  15. 'google',
  16. 'linkedin'
  17. ];
  18.  
  19. /**
  20. * User Schema
  21. */
  22.  
  23. const UserSchema = new Schema({
  24. name: { type: String, default: '' },
  25. email: { type: String, default: '' },
  26. username: { type: String, default: '' },
  27. provider: { type: String, default: '' },
  28. hashed_password: { type: String, default: '' },
  29. salt: { type: String, default: '' },
  30. authToken: { type: String, default: '' },
  31. facebook: {},
  32. twitter: {},
  33. github: {},
  34. google: {},
  35. linkedin: {}
  36. });
  37.  
  38. const validatePresenceOf = value => value && value.length;
  39.  
  40. /**
  41. * Virtuals
  42. */
  43.  
  44. UserSchema
  45. .virtual('password')
  46. .set(function (password) {
  47. this._password = password;
  48. this.salt = this.makeSalt();
  49. this.hashed_password = this.encryptPassword(password);
  50. })
  51. .get(function () {
  52. return this._password;
  53. });
  54.  
  55. /**
  56. * Validations
  57. */
  58.  
  59. // the below 5 validations only apply if you are signing up traditionally
  60.  
  61. UserSchema.path('name').validate(function (name) {
  62. if (this.skipValidation()) return true;
  63. return name.length;
  64. }, 'Name cannot be blank');
  65.  
  66. UserSchema.path('email').validate(function (email) {
  67. if (this.skipValidation()) return true;
  68. return email.length;
  69. }, 'Email cannot be blank');
  70.  
  71. UserSchema.path('email').validate(function (email, fn) {
  72. const User = mongoose.model('User');
  73. if (this.skipValidation()) fn(true);
  74.  
  75. // Check only when it is a new user or when email field is modified
  76. if (this.isNew || this.isModified('email')) {
  77. User.find({ email: email }).exec(function (err, users) {
  78. fn(!err && users.length === 0);
  79. });
  80. } else fn(true);
  81. }, 'Email already exists');
  82.  
  83. UserSchema.path('username').validate(function (username) {
  84. if (this.skipValidation()) return true;
  85. return username.length;
  86. }, 'Username cannot be blank');
  87.  
  88. UserSchema.path('hashed_password').validate(function (hashed_password) {
  89. if (this.skipValidation()) return true;
  90. return hashed_password.length && this._password.length;
  91. }, 'Password cannot be blank');
  92.  
  93.  
  94. /**
  95. * Pre-save hook
  96. */
  97.  
  98. UserSchema.pre('save', function (next) {
  99. if (!this.isNew) return next();
  100.  
  101. if (!validatePresenceOf(this.password) && !this.skipValidation()) {
  102. next(new Error('Invalid password'));
  103. } else {
  104. next();
  105. }
  106. });
  107.  
  108. /**
  109. * Methods
  110. */
  111.  
  112. UserSchema.methods = {
  113.  
  114. /**
  115. * Authenticate - check if the passwords are the same
  116. *
  117. * @param {String} plainText
  118. * @return {Boolean}
  119. * @api public
  120. */
  121.  
  122. authenticate: function (plainText) {
  123. return this.encryptPassword(plainText) === this.hashed_password;
  124. },
  125.  
  126. /**
  127. * Make salt
  128. *
  129. * @return {String}
  130. * @api public
  131. */
  132.  
  133. makeSalt: function () {
  134. return Math.round((new Date().valueOf() * Math.random())) + '';
  135. },
  136.  
  137. /**
  138. * Encrypt password
  139. *
  140. * @param {String} password
  141. * @return {String}
  142. * @api public
  143. */
  144.  
  145. encryptPassword: function (password) {
  146. if (!password) return '';
  147. try {
  148. return crypto
  149. .createHmac('sha1', this.salt)
  150. .update(password)
  151. .digest('hex');
  152. } catch (err) {
  153. return '';
  154. }
  155. },
  156.  
  157. /**
  158. * Validation is not required if using OAuth
  159. */
  160.  
  161. skipValidation: function () {
  162. return ~oAuthTypes.indexOf(this.provider);
  163. }
  164. };
  165.  
  166. /**
  167. * Statics
  168. */
  169.  
  170. UserSchema.statics = {
  171.  
  172. /**
  173. * Load
  174. *
  175. * @param {Object} options
  176. * @param {Function} cb
  177. * @api private
  178. */
  179.  
  180. load: function (options, cb) {
  181. options.select = options.select || 'name username';
  182. return this.findOne(options.criteria)
  183. .select(options.select)
  184. .exec(cb);
  185. }
  186. };
  187.  
  188. mongoose.model('User', UserSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement