import jwt from 'jsonwebtoken'; import { Request, Response, NextFunction } from 'express'; export interface AuthenticatedRequest extends Request { user?: { userId: number; tenantId: number; }; } export const authenticateToken = (req: AuthenticatedRequest, res: Response, next: NextFunction) => { const authHeader = req.headers['authorization']; const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN if (!token) { return res.status(401).json({ error: 'Access token required' }); } try { const decoded = jwt.verify(token, process.env.JWT_SECRET!) as any; req.user = { userId: decoded.userId, tenantId: decoded.tenantId }; next(); } catch (error) { return res.status(403).json({ error: 'Invalid or expired token' }); } };