diff --git a/Backend/Middelware/authMiddelware.js b/Backend/Middelware/authMiddelware.js index 36fa8fa6..a1fb543e 100644 --- a/Backend/Middelware/authMiddelware.js +++ b/Backend/Middelware/authMiddelware.js @@ -1,8 +1,17 @@ import jwt from 'jsonwebtoken'; export default (req, res, next) => { - const token = req.header('Authorization'); - if (!token) return res.status(401).json({ message: 'Access denied. No token provided.' }); + const authHeader = req.header('Authorization'); + + if (!authHeader) { + return res.status(401).json({ message: 'Access denied. No token provided.' }); + } + + const token = authHeader.split(' ')[1]; // Extract the token after "Bearer" + + if (!token) { + return res.status(401).json({ message: 'Access denied. No token provided.' }); + } try { const decoded = jwt.verify(token, process.env.SECRET); @@ -12,6 +21,6 @@ export default (req, res, next) => { if (ex.name === 'TokenExpiredError') { return res.status(401).json({ message: 'Token expired.' }); } - res.status(400).json({ message: 'Invalid token.' }); + return res.status(400).json({ message: 'Invalid token.' }); } -}; \ No newline at end of file +};