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:
@@ -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);
|
||||
};
|
||||
Reference in New Issue
Block a user