From 2412d9f10f0aa658c23f7e389bd37a16b9eaa728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Fri, 6 Dec 2024 16:06:35 +0100 Subject: [PATCH] use off the shelf redis store --- package.json | 1 + src/HttpApi.js | 42 ++++-------------------------------------- 2 files changed, 5 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index ac05930e..c5e7c3bd 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "bcryptjs": "^2.4.3", "better-sqlite3": "^11.2.1", "body-parser": "^1.19.0", + "connect-redis": "^8.0.1", "cookie": "^1.0.1", "cookie-parser": "^1.4.4", "cors": "^2.8.5", diff --git a/src/HttpApi.js b/src/HttpApi.js index c8cdfa26..68dbed0f 100644 --- a/src/HttpApi.js +++ b/src/HttpApi.js @@ -7,6 +7,7 @@ import cookieParser from 'cookie-parser'; import cors from 'cors'; import helmet from 'helmet'; import session from 'express-session'; +import { RedisStore } from 'connect-redis'; import qs from 'qs'; import { pinoHttp } from 'pino-http'; @@ -122,44 +123,9 @@ async function httpApi(uw, options) { secure: uw.express.get('env') === 'production', httpOnly: true, }, - store: new class extends session.Store { - /** - * @param {string} sid - * @param {(err?: Error, data?: session.SessionData | null) => void} callback - */ - get(sid, callback) { - uw.redis.get(`session:${sid}`).then((data) => { - callback(undefined, data == null ? null : JSON.parse(data)); - }, (err) => { - callback(err); - }); - } - - /** - * @param {string} sid - * @param {session.SessionData} data - * @param {(err?: Error) => void} callback - */ - set(sid, data, callback) { - uw.redis.set(`session:${sid}`, JSON.stringify(data)).then(() => { - callback(); - }, (err) => { - callback(err); - }); - } - - /** - * @param {string} sid - * @param {(err?: Error) => void} callback - */ - destroy(sid, callback) { - uw.redis.del(`session:${sid}`).then(() => { - callback(); - }, (err) => { - callback(err); - }); - } - }(), + store: new RedisStore({ + client: uw.redis, + }), })) .use(uw.passport.initialize()) .use(addFullUrl())