const jwt = require("jsonwebtoken")

exports.Auth = async (req, res, next) => {
    try{
        const authHeader = req.headers.authorization
        if(!authHeader) return res.status(403).json({ message: "Access Denied"})

        const token = authHeader.split(" ")[1]
        if(!token) return res.status(401).json({message: "No tokn found"})
        
        const decode = jwt.verify(token, process.env.JWT_SECRET)
        req.user = decode

        next()
    } catch(error) {
        return res.status(500).json({message: "Internal Server Error", error: error.message})
    }
}