How To Add Cookies And Session ?

 


Step 1: Set up your server and database using the given code


Step2: Install the following packages:
4)express-session
using command npm install <packge name>

Step 3: Require the above packages:
const keyword = require(“package name”);



Step 4: Use express-session in your app as follows:


You can add secret of your choice.

Step 5: Initialize passport and use it to deal with express-session


Step 6: Add passport-local-mongoose into your schema as a plugin



Step 7: Create a local strategy using a passport and then serialize and deserialize the user


Step 8: create register post, get and login post, get route


Step 9: Register and authenticate the user using passport-local-mongoose. And then redirect to the page you want. here I have mentioned the “secrets” route.



Step 10: Login and Authenticate users using passport-local-mongoose


This will automatically add Cookies and sessions to your browser. And you can view it by going to the settings, then search cookies.


Note : I haven't mentioned about frontend part, since this blog focused on only cookies. You can refer this Repository:

More resources: 



Comments

Popular Posts