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.
This commit is contained in:
Chamika J
2025-08-06 11:33:14 +05:30
parent cc68a5e9cc
commit 0959f3f926

View File

@@ -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);
});
};