const jwt = require('jsonwebtoken');
const env = require('../config/env');

module.exports = (req, res, next) => {
  const authHeader = req.headers.authorization || '';
  const token = authHeader.startsWith('Bearer ') ? authHeader.substring(7) : null;
  if (!token) {
    return res.status(401).json({ message: 'Kimlik dogrulama gerekiyor' });
  }
  try {
    const decoded = jwt.verify(token, env.jwt.secret);
    req.user = { id: decoded.sub, email: decoded.email };
    next();
  } catch (error) {
    res.status(401).json({ message: 'Token gecersiz veya suresi dolmus' });
  }
};
