Advertisement
Guest User

Untitled

a guest
Apr 11th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. const bcrypt = require('bcrypt-nodejs');
  2. const crypto = require('crypto');
  3. const mongoose = require('mongoose');
  4.  
  5. const userSchema = new mongoose.Schema({
  6. email: { type: String, unique: true },
  7. password: String,
  8. passwordResetToken: String,
  9. passwordResetExpires: Date,
  10.  
  11. facebook: String,
  12. twitter: String,
  13. google: String,
  14. github: String,
  15. instagram: String,
  16. linkedin: String,
  17. steam: String,
  18. tokens: Array,
  19.  
  20. // Added my MGR
  21. devices: Array,
  22. classes: Array,
  23. photo: Buffer,
  24. firstName: String,
  25. lastName: String,
  26. ///////////////
  27.  
  28. profile: {
  29. name: String,
  30. gender: String,
  31. location: String,
  32. website: String,
  33. picture: String
  34. }
  35.  
  36. }, { timestamps: true });
  37.  
  38. /**
  39. * Password hash middleware.
  40. */
  41. userSchema.pre('save', function save(next) {
  42. const user = this;
  43. if (!user.isModified('password')) { return next(); }
  44. bcrypt.genSalt(10, (err, salt) => {
  45. if (err) { return next(err); }
  46. bcrypt.hash(user.password, salt, null, (err, hash) => {
  47. if (err) { return next(err); }
  48. user.password = hash;
  49. next();
  50. });
  51. });
  52. });
  53.  
  54. /**
  55. * Helper method for validating user's password.
  56. */
  57. userSchema.methods.comparePassword = function comparePassword(candidatePassword, cb) {
  58. bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
  59. cb(err, isMatch);
  60. });
  61. };
  62.  
  63. /**
  64. * Helper method for getting user's gravatar.
  65. */
  66. userSchema.methods.gravatar = function gravatar(size) {
  67. if (!size) {
  68. size = 200;
  69. }
  70. if (!this.email) {
  71. return `https://gravatar.com/avatar/?s=${size}&d=retro`;
  72. }
  73. const md5 = crypto.createHash('md5').update(this.email).digest('hex');
  74. return `https://gravatar.com/avatar/${md5}?s=${size}&d=retro`;
  75. };
  76.  
  77. const User = mongoose.model('User', userSchema);
  78.  
  79. module.exports = User;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement