auth.js 559 Bytes
const jwt = require('jsonwebtoken')
const config = require(__dirname + '/../config/config')

const { User } = require('../models')

exports.currentUser = async (token) => {
    try{
        if (!token) {
            return false
        }
        let decoded = jwt.verify(token, config.JWT_KEY)
        if (!decoded) {
            return false
        }
        let userId = decoded.id
        let user = await User.findByPk(userId)
        if (!user) {
            return false
        }
        return user
    } catch (error) {
        throw error
    }
}