I am using express-session and express-mysql-session to manage sessions in my Node.js application with a MySQL database. While the session store initializes successfully and a sessions table is created in the database, the session data is not being stored. When I log in, the session ID is generated, but the session store remains undefined, and no data is written to the sessions table.
Code:
sessionStore.js
import expressMySQLSession from "express-mysql-session";
import session from "express-session";
import { pool } from "../../app.mjs";
// Configure session store using express-mysql-session
//const MySQLStore = expressMySQLSession(session); // dont know if needed
let sessionStore; // Define sessionStore variable outside the asynchronous context so it can be exported
async () => {
try {
const poolInstance = await pool; // Await the pool promise to resolve
sessionStore = new expressMySQLSession(
{
createDatabaseTable: true,
expiration: 1000 * 60 * 60 * 1, // Session expiration time in milliseconds (1 hour)
},
poolInstance
);
console.log("Session store initialized successfully");
} catch (err) {
console.error("Error initializing session store:", err);
}
};
export default sessionStore;
app.mjs
import express, { response } from "express";
import dotenv from "dotenv";
dotenv.config();
import mysql from "mysql2";
import cookieParser from "cookie-parser";
import session from "express-session";
import passport from "passport";
import "./src/strategies/local.js";
import sessionStore from "./src/utils/sessionStore.js";
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
const pool = mysql
.createPool({
connectionLimit: 10,
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
})
.promise();
export { pool };
app.use(
session({
secret: "KTSJSRIJTHRSTIOJHSR",
resave: false,
saveUninitialized: false,
store: sessionStore,
rolling: true, // Refresh the session cookie on every request to extend session lifetime
expires: new Date(Date.now() + 1000 * 60 * 60 * 3), // Expires session after 3 hours of total duration
cookie: {
maxAge: 1000 * 60 * 60 * 1, // expire session after 1 hour in milliseconds (for client-side)
sameSite: "strict",
},
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use((request, response, next) => {
console.log("Inside auth check");
console.log(request.user);
console.log("Session store:", JSON.stringify(sessionStore, null, 2));
console.log("Session ID:", request.sessionID); // Log the session ID
if (request.user) {
next();
} else {
response.sendStatus(401); // Unauthorized status if user is not authenticated
}
});
here are the versions if it is useful:
“express”: “^4.19.2”,
“express-mysql-session”: “^2.1.8”,
“mysql2”: “^3.10.1”,
“passport”: “^0.7.0”,
“passport-local”: “^1.0.0”
“express-session”: “^1.18.0”
What I Tried:
Verified that the sessions table is created in the database.
Checked the session ID is being generated.
Printed the sessionStore to console, which remains undefined.
Expected Outcome:
The session data should be persisted in the MySQL sessions table.
Please review my question and provide guidance on what might be causing the session data not to be stored in the MySQL database and how to resolve this issue.