const CustomError = require('../errors')

const authorization = async (req, res, next) => {

    // next() 
    
    const authHeader = req.headers.authorization 

    if (!authHeader) {
        throw new CustomError.BadRequestError('Authorization not found into header!')
    }

    const [, token] = authHeader.split(" ")

    if (!token) {
        throw new CustomError.BadRequestError('Authorization token not found into header!')
    }

    if (token != process.env.TOKEN) {
        throw new CustomError.UnauthorizedError('Authorization token Invalid')
    }

    next()

}

module.exports = {
    authorization,
}