Skip to content

feat: add mongodb session store #1804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"bcrypt": "^5.1.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"connect-mongo": "^5.1.0",
"dotenv": "^16.0.3",
"express-session": "^1.17.3",
"handlebars": "^4.7.7",
Expand Down Expand Up @@ -93,7 +94,7 @@
"@types/bcrypt": "^5.0.0",
"@types/chai": "^5.0.0",
"@types/express": "^5.0.0",
"@types/express-session": "^1.17.4",
"@types/express-session": "^1.18.1",
"@types/jest": "^27.0.2",
"@types/js-yaml": "^4.0.9",
"@types/jsonpath-plus": "^5.0.5",
Expand Down
16 changes: 12 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import session from "express-session";
import MongoStore from "connect-mongo";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need to have both mongoose and connect-mongo?
Can we achieve the same functionality just with mongoose?

import { NestFactory } from "@nestjs/core";
import {
DocumentBuilder,
Expand Down Expand Up @@ -95,21 +96,28 @@ async function bootstrap() {
const expressSessionSecret = configService.get<string>(
"expressSessionSecret",
);
const mongoUrl = configService.get<string>("mongodbUri");

if (expressSessionSecret) {
app.use(
session({
secret: expressSessionSecret,
resave: false,
saveUninitialized: true,
store: MongoStore.create({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we leave space for choosing if to use mongoStore or in memory one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1
I would add a property in configuration to select the current behavior or session in mongodb.

mongoUrl, // MongoDB connection string
collectionName: "sessions", // Collection name for storing sessions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "sessions" is the default, so maybe we can avoid specifying it here?

ttl: 24 * 60 * 60, // Session TTL (24 hours)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this equal this?

}),
cookie: {
secure: true,
},
Comment on lines +107 to +114
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this post, it is possible to have only one connection to the database.
https://stackoverflow.com/questions/66731669/mongoose-and-connect-mongo

Can you look into it and see if we can reduce duplication?

Copy link
Collaborator

@martin-trajanovski martin-trajanovski Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is this mongoose example in the connect-mongo package (https://github.com/jdesboeufs/connect-mongo/blob/master/example/mongoose.js). But as we use the wrapper for mongoose @nestjs/mongoose then there might be some way to get the connection in a "nestjs" way. So I guess the idea is to reuse the existing mongoose connection if possible here instead of creating a new connection for the connect-mongo.

}),
);
}

const port = configService.get<number>("port") ?? 3000;
Logger.log(
"MongoDB URI : " + configService.get<string>("mongodbUri"),
"Main",
);
Logger.log("MongoDB URI : " + mongoUrl, "Main");
Logger.log("Scicat Backend listening on port: " + port, "Main");

await app.listen(port);
Expand Down