feat(auth): enhance session handling for mobile compatibility

- Modified session management to allow the use of existing sessions for mobile applications, improving session continuity.
- Added detailed logging for session ID usage, response headers, and session save operations to aid in debugging.
- Updated session middleware to support header-based session IDs, ensuring proper handling when cookies are not available.
- Included additional session information in the response for mobile app integration, facilitating better session management.
This commit is contained in:
Chamika J
2025-08-06 11:02:03 +05:30
parent 57c71357da
commit 3ebf262b8e
2 changed files with 82 additions and 55 deletions

View File

@@ -24,6 +24,10 @@ const sessionConfig = {
secure: isProduction(), // Required when sameSite is "none"
domain: isProduction() ? ".worklenz.com" : undefined,
maxAge: 30 * 24 * 60 * 60 * 1000 // 30 days
},
// Custom session ID handling for mobile apps
genid: () => {
return require('uid-safe').sync(24);
}
};
@@ -32,4 +36,19 @@ console.log("Session configuration:", {
secret: "[REDACTED]"
});
export default session(sessionConfig);
const sessionMiddleware = session(sessionConfig);
// Enhanced session middleware that supports both cookies and headers for mobile apps
export default (req: any, res: any, next: any) => {
// Check if mobile app is sending session ID via header (fallback for cookie issues)
const headerSessionId = req.headers['x-session-id'];
const headerSessionName = req.headers['x-session-name'];
if (headerSessionId && headerSessionName && !req.headers.cookie) {
console.log("Mobile app using header-based session:", headerSessionId);
// Inject the session cookie from header for session middleware to process
req.headers.cookie = `${headerSessionName}=s%3A${headerSessionId}`;
}
sessionMiddleware(req, res, next);
};