Skip to content

Commit

Permalink
use off the shelf redis store
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Dec 6, 2024
1 parent 9064e48 commit 2412d9f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 38 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
42 changes: 4 additions & 38 deletions src/HttpApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 2412d9f

Please sign in to comment.