import express, { Request, Response, NextFunction } from 'express'; import jwt, { JwtPayload } from 'jsonwebtoken'; import cookieParser from 'cookie-parser'; 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' }); } const user_id = jwt.verify(token, process.env.JWT_SECRET as string, (err, decoded) => { if (err) { return res.status(401).json({ error: 'In Valid Token' }); } req.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'); }