Session persistance fixed

This commit is contained in:
MaciejkaG 2024-04-14 00:36:51 +02:00
parent ad81684431
commit 8732ff929b
2 changed files with 17 additions and 2 deletions

1
.session.secret Normal file
View File

@ -0,0 +1 @@
e733201e-f39d-4d91-9e7f-4c2d2c4a81da

View File

@ -6,6 +6,7 @@ import { createServer } from 'node:http';
import { Server } from 'socket.io'; import { Server } from 'socket.io';
import path from 'node:path'; import path from 'node:path';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import fs from 'node:fs';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import session from "express-session"; import session from "express-session";
import { engine } from 'express-handlebars'; import { engine } from 'express-handlebars';
@ -71,15 +72,28 @@ let sessionStore = new SessionRedisStore({
prefix: "statkiSession:", 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({ const sessionMiddleware = session({
store: sessionStore, store: sessionStore,
secret: uuidv4(), secret: sessionSecret,
resave: true, resave: true,
saveUninitialized: true, saveUninitialized: true,
rolling: true, rolling: true,
cookie: { cookie: {
secure: checkFlag("cookie_secure"), secure: checkFlag("cookie_secure"),
maxAge: 3 * 24 * 60 * 60 * 1000, maxAge: 7 * 24 * 60 * 60 * 1000,
}, },
}); });