Advertisement
Guest User

Untitled

a guest
Sep 11th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. const mongoose = require('mongoose');
  2. const Schema = mongoose.Schema;
  3. // const bcrypt = require('bcrypt');
  4. mongoose.Promise = global.Promise; // removes deprecation warning
  5.  
  6.  
  7. const UserSchema = new Schema({
  8. username: {
  9. type: String,
  10. required: true,
  11. unique: true
  12. },
  13. email: {
  14. type: String,
  15. required: true,
  16. unique: true
  17. },
  18. image: {
  19. type: String,
  20. required: true
  21. },
  22. accessToken: String,
  23. refreshToken: String,
  24. created_at: Date,
  25. updated_at: Date
  26. });
  27.  
  28. UserSchema.plugin(require('mongoose-role'), {
  29. roles: ['public', 'user', 'manager', 'admin'],
  30. accessLevels: {
  31. 'public': ['public', 'user', 'admin'],
  32. 'anon': ['public'],
  33. 'user': ['user', 'admin'],
  34. 'admin': ['admin']
  35. }
  36. });
  37.  
  38. UserSchema.pre('save', function(next) {
  39. let user = this;
  40. let date = new Date();
  41. user.updated_at = date;
  42.  
  43. if ( !user.created_at ) {
  44. user.created_at = date;
  45. next();
  46. } else next();
  47.  
  48. // bcrypt.genSalt(10, (err, salt) => {
  49. // if ( err ) return next(err);
  50.  
  51. // bcrypt.hash(user.password, salt, (err, hash) => {
  52. // if ( err ) return next(err);
  53.  
  54. // user.password = hash;
  55. // next();
  56. // });
  57. // });
  58. });
  59.  
  60. UserSchema.methods.toJSON = function() {
  61. let user = this.toObject();
  62. delete user.__v;
  63. delete user.created_at;
  64. delete user.updated_at;
  65. return user;
  66. }
  67.  
  68. // UserSchema.methods.comparePassword = function(pass, cb) {
  69. // bcrypt.compare(pass, this.password, (err, isMatch) => {
  70. // if ( err ) return cb(err);
  71. // cb(null, isMatch);
  72. // });
  73. // }
  74.  
  75. module.exports = mongoose.model('User', UserSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement