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.

Applying global guards

Applying global guards

Applying guards globally in a NestJS application ensures that the guard is executed for every route handler in the application. Basically, there are two ways to apply the guards globally. First way is to configure the guard inside the app module file. So here, let me open the module file and inside the providers array, I'll give the curly braces, which is also known as the providers object in NestJS. This providers object is used for defining more advanced configuration settings, like the provide property. This property takes a token value. Now there are various predefined token values which can be passed to the provide property. As we are dealing with guards, I'll give the app guard token. This app guard is a special token that NestJS recognizes for global guards. So with this, we are telling NestJS that we want to apply a guard on the module level that is global level. Now to give the guard name, we will use another property called use class. This property specifies the guard class…

Contents