Setting up Mongo store for session cookies

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 308:- Setting up Mongo store for session cookies

To set up a MongoDB store for session cookies, we need to install the connect-mongo package, which is a MongoDB session store for Connect and Express. We can install it using npm:

arduinoCopy code

npm install connect-mongo

Once installed, we can require it in our app.js file and use it to configure our session store:

javascriptCopy code

const session = require('express-session'); const MongoStore = require('connect-mongo')(session); // ... app.use(session({  secret: 'mysecretkey',  resave: false,  saveUninitialized: false,  store: new MongoStore({ mongooseConnection: mongoose.connection }) }));

In the above code, we create a new MongoStore instance and pass it our Mongoose connection. Then we include it in our session configuration options using the store property. This will allow us to store our session data in MongoDB.

Now, all our session data will be stored in the sessions collection in our MongoDB database. We can verify this by connecting to the database and checking the collections:

javascriptCopy code

mongoose.connect('mongodb://localhost/myapp', { useNewUrlParser: true, useUnifiedTopology: true })  .then(() => {    console.log('Connected to database');    console.log('Collections:', mongoose.connection.db.listCollections());  })  .catch((error) => console.log(error));

This will print out a list of all the collections in our database, including the sessions collection.

35. Authentication Using Passport js

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support