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 - Nest.js Tutorial
From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework
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…
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)
Introduction to guards4m 14s
-
(Locked)
Understanding guard5m 33s
-
(Locked)
Understanding ExecutionContext6m 50s
-
(Locked)
Accessing arguments with getArgs()3m 42s
-
(Locked)
Limiting controller access with guard4m 6s
-
(Locked)
Understanding switchToHttp() method4m 59s
-
(Locked)
Assignment: API key authorization6m 10s
-
(Locked)
Applying multiple guards3m 14s
-
(Locked)
Defining custom metadata5m 6s
-
(Locked)
Setting custom metadata: A better way3m 25s
-
(Locked)
Applying role for specific handlers10m 3s
-
(Locked)
Applying multiple roles1m 35s
-
(Locked)
Applying global guards4m 46s
-
(Locked)
-
-
-
-
-
-
-