// backend/middlewares/authMiddleware.js
import jwt from 'jsonwebtoken';

const authMiddleware = (req, res, next) => {
  const token = req.headers['authorization'];
  if (!token) {
    console.error('No token provided');
    return res.status(403).json({ message: 'Access denied' });
  }
  try {
    const decodedToken = jwt.verify(token.split(' ')[1], process.env.JWT_SECRET);
    req.userId = decodedToken.id;
    next();
  } catch (err) {
    console.error('Token verification failed:', err.message);
    return res.status(401).json({ message: 'Invalid token' });
  }
};

export default authMiddleware;

