You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.js 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const validator = require('validator')
  2. const mongoose = require('mongoose')
  3. const bcrypt = require('bcryptjs')
  4. const jwt = require('jsonwebtoken')
  5. const ejwt = require('express-jwt')
  6. const Joi = require('joi')
  7. const userSchema = new mongoose.Schema({
  8. name: {
  9. type: String
  10. },
  11. email: {
  12. type: String,
  13. required: true
  14. },
  15. password: {
  16. type: String,
  17. required: true
  18. },
  19. tokens: [{
  20. token: {
  21. type: String,
  22. required: true
  23. }
  24. }]
  25. })
  26. userSchema.statics.joiValidate = async function(obj) {
  27. const schema = Joi.object({
  28. name: Joi.string().min(2).required(),
  29. password: Joi.string().min(8).regex(/[a-zA-Z0-9]{3,30}/).required(),
  30. email: Joi.string().email().required(),
  31. })
  32. const validation = schema.validate(obj);
  33. return validation.error
  34. }
  35. // userSchema.pre('save', async function(next) {
  36. // const user = this
  37. // console.log('pre hash: ' + user.password)
  38. // user.password = await bcrypt.hash(user.password, 8)
  39. // console.log('posle hash: ' + user.password)
  40. // console.log('Middleware before password hash')
  41. // next()
  42. // })
  43. const User = mongoose.model('User', userSchema)
  44. module.exports = User