From 2f0fb92e3e2352b3c4b260ec7a366ab8ddee8378 Mon Sep 17 00:00:00 2001 From: chamikaJ Date: Thu, 29 May 2025 16:54:27 +0530 Subject: [PATCH] feat(session): add session store debugging and pg_sessions table management - Implemented a test function for the session store to verify database connection and check the existence of the pg_sessions table. - Added logic to create the pg_sessions table if it does not exist, including defining its structure and constraints. - Enhanced session store methods with detailed logging for session set and get operations, improving traceability and debugging capabilities. --- .../src/middlewares/session-middleware.ts | 123 +++++++++++++++++- 1 file changed, 119 insertions(+), 4 deletions(-) diff --git a/worklenz-backend/src/middlewares/session-middleware.ts b/worklenz-backend/src/middlewares/session-middleware.ts index 263fd857..2c3eddc7 100644 --- a/worklenz-backend/src/middlewares/session-middleware.ts +++ b/worklenz-backend/src/middlewares/session-middleware.ts @@ -5,6 +5,124 @@ import { isProduction } from "../shared/utils"; // eslint-disable-next-line @typescript-eslint/no-var-requires const pgSession = require("connect-pg-simple")(session); +// Test database connection and pg_sessions table +async function testSessionStore() { + try { + console.log("=== SESSION STORE DEBUG ==="); + + // Test basic database connection + const testQuery = await db.query("SELECT NOW() as current_time"); + console.log("Database connection test:", testQuery.rows[0]); + + // Check if pg_sessions table exists + const tableCheck = await db.query(` + SELECT EXISTS ( + SELECT FROM information_schema.tables + WHERE table_name = 'pg_sessions' + ) as table_exists + `); + console.log("pg_sessions table exists:", tableCheck.rows[0].table_exists); + + if (tableCheck.rows[0].table_exists) { + // Check table structure + const structureQuery = await db.query(` + SELECT column_name, data_type + FROM information_schema.columns + WHERE table_name = 'pg_sessions' + ORDER BY ordinal_position + `); + console.log("pg_sessions table structure:", structureQuery.rows); + + // Check current sessions count + const countQuery = await db.query("SELECT COUNT(*) as session_count FROM pg_sessions"); + console.log("Current sessions in database:", countQuery.rows[0].session_count); + + // Check recent sessions + const recentQuery = await db.query(` + SELECT sid, expire, created_at + FROM pg_sessions + ORDER BY created_at DESC + LIMIT 3 + `); + console.log("Recent sessions:", recentQuery.rows); + } else { + console.log("ERROR: pg_sessions table does not exist!"); + + // Try to create the table + console.log("Attempting to create pg_sessions table..."); + await db.query(` + CREATE TABLE IF NOT EXISTS pg_sessions ( + sid VARCHAR NOT NULL COLLATE "default", + sess JSON NOT NULL, + expire TIMESTAMP(6) NOT NULL, + created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP + ) + WITH (OIDS=FALSE); + + ALTER TABLE pg_sessions ADD CONSTRAINT session_pkey PRIMARY KEY (sid) NOT DEFERRABLE INITIALLY IMMEDIATE; + CREATE INDEX IF NOT EXISTS IDX_session_expire ON pg_sessions (expire); + `); + console.log("pg_sessions table created successfully"); + } + + console.log("=== END SESSION STORE DEBUG ==="); + } catch (error) { + console.log("Session store test error:", error); + } +} + +// Run the test +testSessionStore(); + +const store = new pgSession({ + pool: db.pool, + tableName: "pg_sessions" +}); + +// Add store event listeners +store.on("connect", () => { + console.log("Session store connected to database"); +}); + +store.on("disconnect", () => { + console.log("Session store disconnected from database"); +}); + +// Override store methods to add debugging +const originalSet = store.set.bind(store); +const originalGet = store.get.bind(store); + +store.set = function(sid: string, session: any, callback: any) { + console.log(`=== SESSION SET ===`); + console.log(`Session ID: ${sid}`); + console.log(`Session data:`, JSON.stringify(session, null, 2)); + + return originalSet(sid, session, (err: any) => { + if (err) { + console.log(`Session SET ERROR for ${sid}:`, err); + } else { + console.log(`Session SET SUCCESS for ${sid}`); + } + callback && callback(err); + }); +}; + +store.get = function(sid: string, callback: any) { + console.log(`=== SESSION GET ===`); + console.log(`Requesting session ID: ${sid}`); + + return originalGet(sid, (err: any, session: any) => { + if (err) { + console.log(`Session GET ERROR for ${sid}:`, err); + } else if (session) { + console.log(`Session GET SUCCESS for ${sid}:`, JSON.stringify(session, null, 2)); + } else { + console.log(`Session GET: No session found for ${sid}`); + } + callback(err, session); + }); +}; + export default session({ name: process.env.SESSION_NAME, secret: process.env.SESSION_SECRET || "development-secret-key", @@ -12,10 +130,7 @@ export default session({ resave: true, saveUninitialized: false, rolling: true, - store: new pgSession({ - pool: db.pool, - tableName: "pg_sessions" - }), + store, cookie: { path: "/", httpOnly: true,