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.

Validating request with middleware

Validating request with middleware

Currently, we are getting the token when the user logs in. Now this token will be carried along with every request made to the server. And to implement the authentication, we need to verify this token on each request. And for that, we will create a middleware that will verify the token and call the next method only if the token is valid, else the user will be sent back to the login page. So let's begin. I'll create a middleware by giving the command nest generate middleware. Will define the folder that is middleware slash auth hyphen hyphen no spec and the middleware is created. Let's open it. Now here inside the use method, I'll define the token that is const token is equal to request.session.token. Now this auth middleware will be used for validating the token which will be required in the edit product, add product, delete product, and the logout route. and the user should be able to still use the sign up and login. Let's first try to simply display the token inside the console. And…

Contents