Guest User

Untitled

a guest
Jul 1st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. var mongoose = require('mongoose');
  2. var Schema = mongoose.Schema;
  3.  
  4. var bcrypt = require('bcryptjs');
  5.  
  6. var validateEmail = function(email) {
  7. var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  8. return re.test(email)
  9. };
  10.  
  11. var UserSchema = new Schema({
  12. name: {
  13. type: String,
  14. required: true
  15. },
  16. username: {
  17. type: String,
  18. required: true,
  19. index: {
  20. unique: true
  21. }
  22. },
  23. email: {
  24. type: String,
  25. required: true,
  26. unique: true,
  27. validate: {
  28. validator: validateEmail,
  29. message: 'Invalid email'
  30. }
  31. },
  32. password: {
  33. type: String,
  34. required: true
  35. },
  36. new_password: {
  37. type: String,
  38. default: null
  39. },
  40. birthday: {
  41. type: Date,
  42. required: true
  43. },
  44. gender : {
  45. type: String,
  46. required: true,
  47. enum: ['male', 'female', 'other']
  48. },
  49. description: {
  50. type: String,
  51. maxlength: 240,
  52. default: "No description."
  53. },
  54. _history: {
  55. type: [
  56. {
  57. type: mongoose.Schema.Types.ObjectId,
  58. ref: 'Action'
  59. }],
  60. default: [],
  61. required: true
  62. },
  63.  
  64. _following: {
  65. type: [
  66. {
  67. type: mongoose.Schema.Types.ObjectId,
  68. ref: 'Follows'
  69. }
  70. ],
  71. default: []
  72. },
  73. _following_pages: {
  74. type: [
  75. {
  76. type: mongoose.Schema.Types.ObjectId,
  77. ref: 'FollowsPage'
  78. }
  79. ],
  80. default: []
  81. },
  82. _followers: {
  83. type: [
  84. {
  85. type: mongoose.Schema.Types.ObjectId,
  86. ref: 'Follows'
  87. }
  88. ],
  89. default: []
  90. },
  91. vip: {
  92. type: Boolean,
  93. default: false
  94. },
  95. _watchlist: {
  96. type: mongoose.Schema.Types.ObjectId,
  97. ref: 'UserList'
  98. },
  99. _lists: {
  100. type: [
  101. {
  102. type: mongoose.Schema.Types.ObjectId,
  103. ref: 'UserList'
  104. }
  105. ],
  106. default: []
  107. },
  108. _ratings: {
  109. type: [
  110. {
  111. type: mongoose.Schema.Types.ObjectId,
  112. ref: 'Rating'
  113. }
  114. ],
  115. default: []
  116. },
  117. image: Buffer,
  118. settings: {
  119. autowatch: {
  120. type: Boolean,
  121. default: false
  122. }
  123. },
  124. _following_lists: {
  125. type: [
  126. {
  127. userListId: {
  128. type: mongoose.Schema.Types.ObjectId,
  129. ref: 'UserList'
  130. },
  131. notifications_enabled: {
  132. type: Boolean
  133. }
  134. },
  135. ],
  136.  
  137. default: []
  138. }
  139. });
  140.  
  141. UserSchema.methods.generateHash = function(password) {
  142. return bcrypt.hashSync(password, bcrypt.genSaltSync(10), null);
  143. };
  144.  
  145. UserSchema.methods.validPassword = function(password) {
  146. return bcrypt.compareSync(password, this.password);
  147. };
  148.  
  149. var User = mongoose.model('User', UserSchema);
  150.  
  151. module.exports = User;
Add Comment
Please, Sign In to add comment