mirror of
https://github.com/MaciejkaG/statki.git
synced 2024-11-30 05:22:55 +01:00
Session persistance fixed
This commit is contained in:
parent
ad81684431
commit
8732ff929b
1
.session.secret
Normal file
1
.session.secret
Normal file
@ -0,0 +1 @@
|
|||||||
|
e733201e-f39d-4d91-9e7f-4c2d2c4a81da
|
18
index.js
18
index.js
@ -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,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user