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.

Understanding guard

Understanding guard

Now, let's see how to implement a guard and understand the code syntax. To create a new guard, I will give the nest CLI command, nest generate guard slash guards, which is the folder name, and slash auth, which is the guard name, hyphen hyphen, no hyphen spec. And the guard file is created. Let me open it and we get the default boilerplate code for a guard. Now there are a lot of things here which you may or may not be familiar with. So let's break down each line. First of all, every guard file is marked with an injectable decorator so that it can be used that is injected inside different components and modules within the application. Also if you look at the import statement, there are two important interfaces that is can activate and execution context, which are implemented by the guard. I'll explain them in a few moments. But these interfaces are essential for creating guards in NestJS. Then we have an observable class which is getting imported from the RxJS library. Now if you are…

Contents