const jwt = require("jsonwebtoken"); const User = require("../models/userModel"); exports.protect = async (req, res, next) => { try { let token; if (req.headers.authorization && req.headers.authorization.startsWith("Bearer")) { token = req.headers.authorization.split(" ")[1]; } if (!token) return res.status(401).json({ message: "Not authorized, token missing" }); const decoded = jwt.verify(token, process.env.JWT_SECRET); req.user = await User.findByPk(decoded.id); if (!req.user) return res.status(404).json({ message: "User not found" }); next(); } catch (error) { res.status(401).json({ message: "Invalid token", error: error.message }); } }; // Admin only access exports.adminOnly = (req, res, next) => { if (req.user.role !== "admin") { return res.status(403).json({ message: "Access denied, admin only" }); } next(); };