let {
    User, sequelize
} = require('../models')
const jwt = require("jsonwebtoken");
module.exports = async(req, res, next) => {
    try {
        const token = req.headers.authorization.replace("Bearer ", "");
        const decoded = jwt.verify(token, "iouydhtrfguyrthgftryhgidrhytgidhytiglriltnhgrhtiuygrthiugritghiyutrcginhrtijghurfcuhjgnioergjfuiehtiehtiehyritheithreifbhgehfbdxhbkvfdbhjkvgdkhnjUIYIRUiuiuYIYI3i42yiuyIUYIU4yiu$YUI#YUI$3mvsazr57;" + process.env.SESSION_SECRET);
        req.userData = decoded;
        let user = await User.findOne({ where: {
                id: req.userData.UserId
            }})
        if(user && user.jwtOffset == req.userData.offset) {
            req.userData = decoded;
            next()
        } else {
            res.status(200)
            res.json({loggedIn: false})
        }
    } catch {
        res.status(200)
        res.json({loggedIn: false})
    }
};
