-
Notifications
You must be signed in to change notification settings - Fork 29
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import session from "express-session"; | ||
import MongoStore from "connect-mongo"; | ||
import { NestFactory } from "@nestjs/core"; | ||
import { | ||
DocumentBuilder, | ||
|
@@ -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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
mongoUrl, // MongoDB connection string | ||
collectionName: "sessions", // Collection name for storing sessions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. Can you look into it and see if we can reduce duplication? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment.
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?