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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const validator = require('validator')
  2. const mongoose = require('mongoose')
  3. const bcrypt = require('bcryptjs')
  4. const jwt = require('jsonwebtoken')
  5. const User = require('../models/user')
  6. const tokenSchema = new mongoose.Schema({
  7. token: {
  8. type: String,
  9. required: true
  10. },
  11. userId: {
  12. type: String,
  13. required: true
  14. }
  15. })
  16. tokenSchema.statics.findByCredentials = async (email, password) => {
  17. const user = await User.findOne({email})
  18. if(!user) {
  19. return
  20. }
  21. const checkMatch = await bcrypt.compare(password, user.password)
  22. console.log(password)
  23. console.log(user.password)
  24. console.log(checkMatch)
  25. if(!checkMatch) {
  26. return user
  27. }
  28. return user
  29. }
  30. tokenSchema.statics.generateAuthToken = async function(userArg) {
  31. const user = userArg
  32. const token = jwt.sign({ _id: user._id.toString() }, 'ovoJeSecret')
  33. user.tokens = user.tokens.concat({ token })
  34. await user.save()
  35. return token
  36. }
  37. const Token = mongoose.model('Token', tokenSchema)
  38. module.exports = Token