Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {
  2. hashPassword,
  3. verifyPassword,
  4. } from '../utils/helpers/hashPasswordHelpers';
  5. const mongoose = require('mongoose');
  6. const validator = require('validator');
  7. const UserSchema = new mongoose.Schema({
  8. fullName: {
  9. type: String,
  10. required: [true, 'Please provide a name.'],
  11. maxlength: [60, 'Name cannot be more than 60 characters'],
  12. trim: true,
  13. },
  14. username: {
  15. type: String,
  16. required: [true, 'Please provide an username.'],
  17. unique: [true, 'Username must be unique.'],
  18. maxlength: [60, 'Name cannot be more than 60 characters'],
  19. trim: true,
  20. },
  21. email: {
  22. type: String,
  23. unique: [true, 'Email must be unique.'],
  24. required: [true, 'Please provide an email.'],
  25. trim: true,
  26. lowercase: true,
  27. validate(value) {
  28. if (!validator.isEmail(value)) {
  29. throw new Error('Email is invalid');
  30. }
  31. },
  32. },
  33. password: {
  34. type: String,
  35. required: [true, 'Please provide a password.'],
  36. minlength: 7,
  37. trim: true,
  38. validate(value) {
  39. if (value.toLowerCase().includes('password')) {
  40. throw new Error('Password cannot contain "password"');
  41. }
  42. },
  43. },
  44. country: {
  45. type: String,
  46. required: [true, 'Please provide a country.'],
  47. trim: true,
  48. },
  49. city: {
  50. type: String,
  51. required: [true, 'Please provide a city.'],
  52. trim: true,
  53. },
  54. address: {
  55. type: String,
  56. required: [true, 'Please provide an address.'],
  57. trim: true,
  58. },
  59. phone: {
  60. type: String,
  61. unique: [true, 'Phone number must be unique.'],
  62. required: [true, 'Please provide a phone number.'],
  63. trim: true,
  64. lowercase: true,
  65. validate(value) {
  66. if (!validator.isMobilePhone(value)) {
  67. throw new Error('Not a valid phone number');
  68. }
  69. },
  70. },
  71. postcode: {
  72. type: String,
  73. required: [true, 'Please provide a postal code.'],
  74. validate(value) {
  75. if (!validator.isPostalCode(value)) {
  76. throw new Error('Not a valid postal code');
  77. }
  78. },
  79. },
  80. });
  81. UserSchema.virtual('orders', {
  82. ref: 'Order',
  83. localField: '_id',
  84. foreignField: 'owner',
  85. });
  86. UserSchema.statics.findByCredentials = async (username, password) => {
  87. const user = await User.findOne({ username });
  88. if (!user) {
  89. throw new Error('Unable to login');
  90. }
  91. const isMatch = await verifyPassword(password, user.password);
  92. if (!isMatch) {
  93. throw new Error('Unable to login');
  94. }
  95. return user;
  96. };
  97. UserSchema.pre('save', async function (next) {
  98. const user = this;
  99. if (user.isModified('password')) {
  100. user.password = await hashPassword(user.password);
  101. }
  102. next();
  103. });
  104. const User = mongoose.models.User || mongoose.model('User', UserSchema);
  105. module.exports = User;