// backend/src/middleware/authMiddleware.ts
import { Request, Response, NextFunction } from 'express';
import { SessionService } from '../services/SessionService';

export const authMiddleware = (req: Request, res: Response, next: NextFunction) => {
  const sessionToken = req.headers['x-session-token'] as string; // Assuming token is sent in a header

  if (!sessionToken) {
    return res.status(401).json({ message: 'No session token provided.' });
  }

  const session = SessionService.getSession(sessionToken);

  if (!session || !session.isAuthenticated) {
    return res.status(401).json({ message: 'Invalid or unauthenticated session.' });
  }

  // Optionally, attach session to request for further use
  (req as any).session = session;
  next();
};
