feat(auth): enhance session middleware logging and error handling

- Added detailed debug logging to the session middleware for improved visibility into request processing, including URL, method, and header information.
- Updated error handling to log session middleware errors and session ID status after processing.
- Ensured compatibility with mobile applications by refining cookie handling based on header values.
This commit is contained in:
Chamika J
2025-08-06 12:40:11 +05:30
parent a1aaf9bd59
commit 097c281051

View File

@@ -42,8 +42,15 @@ export default (req: any, res: any, next: any) => {
const headerSessionId = req.headers["x-session-id"];
const headerSessionName = req.headers["x-session-name"];
console.log("DEBUG - Session middleware:");
console.log("- URL:", req.url);
console.log("- Method:", req.method);
console.log("- Has headers:", !!headerSessionId);
console.log("- Original cookie:", req.headers.cookie);
// Only process headers if they exist AND there's no existing valid session cookie
if (headerSessionId && headerSessionName) {
console.log("Processing mobile headers");
const secret = process.env.SESSION_SECRET || "development-secret-key";
try {
@@ -63,13 +70,24 @@ export default (req: any, res: any, next: any) => {
// Set the session cookie from header
req.headers.cookie = sessionCookie;
}
console.log("Updated cookie:", req.headers.cookie);
} catch (error) {
console.log("Error processing headers:", error);
// Fallback to the old method
const sessionCookie = `${headerSessionName}=s%3A${headerSessionId}`;
req.headers.cookie = sessionCookie;
}
} else {
console.log("Using normal cookie processing");
}
// Always call the original session middleware (handles both cookie and header-converted cases)
sessionMiddleware(req, res, next);
sessionMiddleware(req, res, (err: any) => {
if (err) {
console.log("Session middleware error:", err);
}
console.log("After session middleware - Session ID:", (req as any).sessionID);
console.log("After session middleware - Authenticated:", !!(req as any).isAuthenticated && (req as any).isAuthenticated());
next(err);
});
};