From 0959f3f926cc281c2cd904547f86f7d905ecbb84 Mon Sep 17 00:00:00 2001 From: Chamika J <75464293+chamikaJ@users.noreply.github.com> Date: Wed, 6 Aug 2025 11:33:14 +0530 Subject: [PATCH] feat(auth): enhance session middleware logging and error handling - Improved logging within the session middleware to provide detailed insights into session ID, cookie headers, and authentication status. - Added error handling for the session middleware to capture and log any issues during session processing. - Ensured proper construction of session cookies for mobile applications, maintaining compatibility with existing cookies. --- .../src/middlewares/session-middleware.ts | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/worklenz-backend/src/middlewares/session-middleware.ts b/worklenz-backend/src/middlewares/session-middleware.ts index af666a2e..41c54a06 100644 --- a/worklenz-backend/src/middlewares/session-middleware.ts +++ b/worklenz-backend/src/middlewares/session-middleware.ts @@ -51,8 +51,11 @@ export default (req: any, res: any, next: any) => { if (headerSessionId && headerSessionName) { console.log("Mobile app using header-based session:", headerSessionId); - // Create or override the cookie header with the session from header + + // The session store expects a signed cookie, but we need to construct it properly + // For now, let's try the exact format from the successful login session const sessionCookie = `${headerSessionName}=s%3A${headerSessionId}`; + if (req.headers.cookie) { // Replace existing session cookie while keeping other cookies req.headers.cookie = req.headers.cookie @@ -65,7 +68,23 @@ export default (req: any, res: any, next: any) => { req.headers.cookie = sessionCookie; } console.log("Updated cookie header:", req.headers.cookie); + + // Also debug what the session middleware will see + console.log("Expected session lookup for ID:", headerSessionId); } - sessionMiddleware(req, res, next); + sessionMiddleware(req, res, (err: any) => { + if (err) { + console.log("Session middleware error:", err); + } + + // Debug what the session middleware produced + console.log("After session middleware:"); + console.log("- Session ID:", (req as any).sessionID); + console.log("- Session data exists:", !!(req as any).session); + console.log("- Session passport data:", (req as any).session?.passport); + console.log("- Is authenticated:", !!(req as any).isAuthenticated && (req as any).isAuthenticated()); + + next(err); + }); }; \ No newline at end of file