From 8732ff929b3f8ea577a6a01c802e0b99617ecc71 Mon Sep 17 00:00:00 2001 From: MaciejkaG Date: Sun, 14 Apr 2024 00:36:51 +0200 Subject: [PATCH] Session persistance fixed --- .session.secret | 1 + index.js | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 .session.secret diff --git a/.session.secret b/.session.secret new file mode 100644 index 0000000..264981b --- /dev/null +++ b/.session.secret @@ -0,0 +1 @@ +e733201e-f39d-4d91-9e7f-4c2d2c4a81da \ No newline at end of file diff --git a/index.js b/index.js index 0a6ab3b..53afdea 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ import { createServer } from 'node:http'; import { Server } from 'socket.io'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; +import fs from 'node:fs'; import { v4 as uuidv4 } from 'uuid'; import session from "express-session"; import { engine } from 'express-handlebars'; @@ -71,15 +72,28 @@ let sessionStore = new SessionRedisStore({ prefix: "statkiSession:", }); +var sessionSecret = uuidv4(); +let secretPath = path.join(__dirname, '.session.secret'); + +if (fs.existsSync(secretPath)) { + sessionSecret = fs.readFileSync(secretPath); +} else { + fs.writeFile(secretPath, sessionSecret, function (err) { + if (err) { + console.log("An error occured while saving a freshly generated session secret.\nSessions may not persist after a restart of the server."); + } + }); +} + const sessionMiddleware = session({ store: sessionStore, - secret: uuidv4(), + secret: sessionSecret, resave: true, saveUninitialized: true, rolling: true, cookie: { secure: checkFlag("cookie_secure"), - maxAge: 3 * 24 * 60 * 60 * 1000, + maxAge: 7 * 24 * 60 * 60 * 1000, }, });