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.

Introduction to guards

Introduction to guards

Guards are one of the most important and core concepts in NestJS. Their sole purpose is to guard the application routes, that is, request endpoints, by examining the incoming requests and deciding whether to allow or deny access based on certain conditions. Guards are used for tasks like checking if a user is logged in, determining if a user has the right permissions, or validating incoming data before processing it. Now you may think that middleware can easily handle the authorization and authentication which it does, if the application is built on traditional ExpressJS. But in NestJS, we have guards and middleware by nature is a bit dumb. What I mean to say is, it does not know which handler will be executed after calling the next function. Like here in the code, middleware will not be able to process what will happen after it passes the request to the next handler. It only knows its own job, which is to examine the request and make a decision based on that. After that, it's up to…

Contents