From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Configuring Express-Session

Configuring Express-Session

So, now we have implemented the basic flow. But there is also a flaw in this application. And that is the cookie value can be easily changed from the browser, which alters the behavior of the application. So, to deal with this, we are going to create a unique session for the user and store it on the server. And to do so, we will install a middleware package called express-session. I'll give the command npm install express-session. Now inside the main TS file where the application is bootstrapped, I'll import the express-session module. Then we'll use the middleware by giving app dot use session. This session function takes a session configuration as an object. And the first configuration is the secret key. It holds the secret key for the session. Let me give the secret option and say it is a secret. Then I'll give resave option to false. If you set it to true, it will save the session on every request. And we don't want that. We want that the session should be saved only if there is a…

Contents