const validator = require('validator') const mongoose = require('mongoose') const bcrypt = require('bcryptjs') const jwt = require('jsonwebtoken') const User = require('../models/user') const tokenSchema = new mongoose.Schema({ token: { type: String, required: true }, userId: { type: String, required: true } }) tokenSchema.statics.findByCredentials = async (email, password) => { const user = await User.findOne({email}) if(!user) { return } const checkMatch = await bcrypt.compare(password, user.password) console.log(password) console.log(user.password) console.log(checkMatch) if(!checkMatch) { return user } return user } tokenSchema.statics.generateAuthToken = async function(userArg) { const user = userArg const token = jwt.sign({ _id: user._id.toString() }, 'ovoJeSecret') user.tokens = user.tokens.concat({ token }) await user.save() return token } const Token = mongoose.model('Token', tokenSchema) module.exports = Token