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

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

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

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?