import { jwtAuth } from '../services/jwtAuth';


export function authenticateToken(req, res, next) {

    const authHeader = req.headers['authorization']
    const accessToken = authHeader && authHeader.split(' ')[1]

    if (accessToken == null) {
        return res.sendStatus(401)
    }

    jwtAuth.verifyAccessToken(accessToken, (err, user) => {
        
        if (err) {
            return res.sendStatus(401);
        }

        req.user = user
        next()
    })
}