1
0
Fork 0

Setup session for sockets

This commit is contained in:
Ethanell 2020-10-12 20:54:32 +02:00
parent b9613f16a9
commit 5eaa4975f2
6 changed files with 54 additions and 1 deletions

9
app.js
View file

@ -4,14 +4,18 @@ let path = require("path");
let cookieParser = require("cookie-parser"); let cookieParser = require("cookie-parser");
let logger = require("morgan"); let logger = require("morgan");
let sassMiddleware = require("node-sass-middleware"); let sassMiddleware = require("node-sass-middleware");
const session = require("express-session");
const config = require("./config/config.json");
let indexRouter = require("./routes/index"); let indexRouter = require("./routes/index");
let app = express(); let app = express();
const sessionMiddleware = session({secret: config.secret});
// view engine setup // view engine setup
app.set("views", path.join(__dirname, "views")); app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug"); app.set("view engine", "pug");
app.set("sessionMiddleware", sessionMiddleware);
app.use(logger("dev")); app.use(logger("dev"));
app.use(express.json()); app.use(express.json());
@ -24,6 +28,11 @@ app.use(sassMiddleware({
sourceMap: true sourceMap: true
})); }));
app.use(express.static(path.join(__dirname, "public"))); app.use(express.static(path.join(__dirname, "public")));
app.use(sessionMiddleware);
app.use((req, res, next) => {
res.locals.session = req.session;
next();
});
app.use("/", indexRouter); app.use("/", indexRouter);

View file

@ -27,6 +27,7 @@ let server = http.createServer(app);
*/ */
const io = require("socket.io")(server); const io = require("socket.io")(server);
io.use((socket, next) => app.get("sessionMiddleware")(socket.request, socket.request.res || {}, next));
io.on("connection", require("../sockets")); io.on("connection", require("../sockets"));
/** /**

View file

@ -6,5 +6,6 @@
"host": "127.0.0.1", "host": "127.0.0.1",
"dialect": "postgres", "dialect": "postgres",
"operatorsAliases": false "operatorsAliases": false
} },
"secret": "keyboard cat"
} }

40
package-lock.json generated
View file

@ -1042,6 +1042,33 @@
} }
} }
}, },
"express-session": {
"version": "1.17.1",
"resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.1.tgz",
"integrity": "sha512-UbHwgqjxQZJiWRTMyhvWGvjBQduGCSBDhhZXYenziMFjxst5rMV+aJZ6hKPHZnPyHGsrqRICxtX8jtEbm/z36Q==",
"requires": {
"cookie": "0.4.0",
"cookie-signature": "1.0.6",
"debug": "2.6.9",
"depd": "~2.0.0",
"on-headers": "~1.0.2",
"parseurl": "~1.3.3",
"safe-buffer": "5.2.0",
"uid-safe": "~2.1.5"
},
"dependencies": {
"depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
"integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw=="
},
"safe-buffer": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.0.tgz",
"integrity": "sha512-fZEwUGbVl7kouZs1jCdMLdt95hdIv0ZeHg6L7qPeciMZhZ+/gdesW4wgTARkrFWEpspjEATAzUGPG8N2jJiwbg=="
}
}
},
"ext": { "ext": {
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/ext/-/ext-1.4.0.tgz", "resolved": "https://registry.npmjs.org/ext/-/ext-1.4.0.tgz",
@ -2738,6 +2765,11 @@
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz",
"integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA=="
}, },
"random-bytes": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz",
"integrity": "sha1-T2ih3Arli9P7lYSMMDJNt11kNgs="
},
"range-parser": { "range-parser": {
"version": "1.2.1", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
@ -3626,6 +3658,14 @@
"integrity": "sha1-bgkk1r2mta/jSeOabWMoUKD4grc=", "integrity": "sha1-bgkk1r2mta/jSeOabWMoUKD4grc=",
"optional": true "optional": true
}, },
"uid-safe": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz",
"integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==",
"requires": {
"random-bytes": "~1.0.0"
}
},
"umzug": { "umzug": {
"version": "2.3.0", "version": "2.3.0",
"resolved": "https://registry.npmjs.org/umzug/-/umzug-2.3.0.tgz", "resolved": "https://registry.npmjs.org/umzug/-/umzug-2.3.0.tgz",

View file

@ -10,6 +10,7 @@
"cookie-parser": "~1.4.4", "cookie-parser": "~1.4.4",
"debug": "~2.6.9", "debug": "~2.6.9",
"express": "~4.16.1", "express": "~4.16.1",
"express-session": "^1.17.1",
"http-errors": "~1.6.3", "http-errors": "~1.6.3",
"morgan": "~1.9.1", "morgan": "~1.9.1",
"node-sass-middleware": "0.11.0", "node-sass-middleware": "0.11.0",

View file

@ -5,3 +5,4 @@ html
link(rel="stylesheet", href="/stylesheets/style.css") link(rel="stylesheet", href="/stylesheets/style.css")
body body
block content block content
script(src="/socket.io/socket.io.js")