From 5eaa4975f2069628d685d7686b37d6aafb8fd044 Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 12 Oct 2020 20:54:32 +0200 Subject: [PATCH] Setup session for sockets --- app.js | 9 +++++++++ bin/www | 1 + config/config_example.json | 3 ++- package-lock.json | 40 ++++++++++++++++++++++++++++++++++++++ package.json | 1 + views/layout.pug | 1 + 6 files changed, 54 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 03905da..5375955 100644 --- a/app.js +++ b/app.js @@ -4,14 +4,18 @@ let path = require("path"); let cookieParser = require("cookie-parser"); let logger = require("morgan"); let sassMiddleware = require("node-sass-middleware"); +const session = require("express-session"); +const config = require("./config/config.json"); let indexRouter = require("./routes/index"); let app = express(); +const sessionMiddleware = session({secret: config.secret}); // view engine setup app.set("views", path.join(__dirname, "views")); app.set("view engine", "pug"); +app.set("sessionMiddleware", sessionMiddleware); app.use(logger("dev")); app.use(express.json()); @@ -24,6 +28,11 @@ app.use(sassMiddleware({ sourceMap: true })); 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); diff --git a/bin/www b/bin/www index 58d685c..2c949c4 100755 --- a/bin/www +++ b/bin/www @@ -27,6 +27,7 @@ let server = http.createServer(app); */ const io = require("socket.io")(server); +io.use((socket, next) => app.get("sessionMiddleware")(socket.request, socket.request.res || {}, next)); io.on("connection", require("../sockets")); /** diff --git a/config/config_example.json b/config/config_example.json index d309ce1..e0a4d47 100644 --- a/config/config_example.json +++ b/config/config_example.json @@ -6,5 +6,6 @@ "host": "127.0.0.1", "dialect": "postgres", "operatorsAliases": false - } + }, + "secret": "keyboard cat" } diff --git a/package-lock.json b/package-lock.json index 3fa0516..441850e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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": { "version": "1.4.0", "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", "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": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", @@ -3626,6 +3658,14 @@ "integrity": "sha1-bgkk1r2mta/jSeOabWMoUKD4grc=", "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": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/umzug/-/umzug-2.3.0.tgz", diff --git a/package.json b/package.json index 04633eb..b3b2aea 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "cookie-parser": "~1.4.4", "debug": "~2.6.9", "express": "~4.16.1", + "express-session": "^1.17.1", "http-errors": "~1.6.3", "morgan": "~1.9.1", "node-sass-middleware": "0.11.0", diff --git a/views/layout.pug b/views/layout.pug index 1fe5414..f58a569 100644 --- a/views/layout.pug +++ b/views/layout.pug @@ -5,3 +5,4 @@ html link(rel="stylesheet", href="/stylesheets/style.css") body block content + script(src="/socket.io/socket.io.js")