import { Request, Response, NextFunction } from 'express'; import jwt from 'jsonwebtoken'; interface AuthenticatedRequest extends Request { userId?: string; } // Middleware function to authenticate requests export function authenticateToken(req: AuthenticatedRequest, res: Response, next: NextFunction) { const token = req.cookies.access_token; if (!token) { return res.status(401).json({ error: 'Unauthorized' }); } jwt.verify(token, process.env.JWT_SECRET as string, (err: any, decoded: { userId: any; }) => { if (err) { return res.status(401).json({ error: 'In Valid Token' }); } req.body.userId = decoded.userId; next(); }); } // Set JWT as cookie in the response export function setJwtCookie(res: Response, token: string) { res.cookie('access_token', token, { httpOnly: true }); } // Clear JWT cookie in the response export function clearJwtCookie(res: Response) { res.clearCookie('access_token'); }