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.
Destroying the session: Logout - Nest.js Tutorial
From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework
Destroying the session: Logout
Now, let's destroy the session on logout. Currently, when the user is logged in, the session is created, as you can see. But when the user logs out, the session still remains in the database. So we need to fix this by destroying the session when the user logs out. So inside the user controllers logout route, we have set the is logged in to false. I'll remove it and instead I'll give request.session.destroy method. This method is used for destroying that is deleting the session. It takes a callback which catches the error. I'll catch the error by giving an if condition and throw a new internal server error exception with a message fail to destroy session. So this is going to destroy the session when the user logs out, but it won't clear the cookie. So to clear the cookie, I'll give response.clearCookie method and set the cookie that is connect.sid. Let's save this and try to log out. I'll re-login the user by giving the username and password and the session is created. Now let's log…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
-
-
(Locked)
Section introduction2m 33s
-
(Locked)
Setting up the sign-up route2m 24s
-
(Locked)
Designing the sign-up form2m 53s
-
(Locked)
Implementing show/hide password functionality4m 40s
-
(Locked)
Validating password2m 32s
-
(Locked)
Registering user to the database6m 50s
-
(Locked)
Configuring login page2m 12s
-
(Locked)
Validating user and sending cookie4m 41s
-
(Locked)
Reading a cookie2m 13s
-
(Locked)
Rendering DOM based on login status3m 48s
-
(Locked)
Implementing logout1m 20s
-
(Locked)
Configuring Express-Session2m 54s
-
(Locked)
Sending and reading session cookie3m 40s
-
(Locked)
Storing sessions in MySQL3m 44s
-
(Locked)
Destroying the session: Logout1m 42s
-
(Locked)
Optimizing session storage4m 15s
-
(Locked)
Implementing JWT token6m 51s
-
(Locked)
Validating request with middleware5m
-
(Locked)
Conditional rendering with token6m 14s
-
(Locked)
Hashing the password on sign-up3m 51s
-
(Locked)
Validating login credentials2m 15s
-
(Locked)
Finalizing MyStore application4m 20s
-
(Locked)
-
-