Hi everyone.
Let's go to with the.
NestJS fundamental and core concept of NestJS.
Okay this here will be guard.
This is very important concept inside NestJS.
And the guard is also the class.
And this will be a dependency injection class which implement the something called CanActive interface.
Now you can see the guard right here.
Okay.
It will protect the route handler.
Let me let me show you something like this, actually the guard.
We have a single responsibility okay.
This will determine the request will be handled by router or not okay.
This is the whole purpose of the guard.
And we usually use in the authorization.
Okay.
Now we if you already know the express.js, in the NestJS sorry, in the express.js, we normally
use the middleware to handle the authentication or authorization.
Right?
But NestJS we will use the guard because it will follow the single responsibility concept one class only.
Only responsible for one purpose, and the purpose of the guard
It will handle it will determine okay the route handler.
Now you can see, you can read more about the detail inside the document, it say the middleware, by
the nature, it's dump.
Okay.
Very fun.
Right?
And with middleware, it doesn't know the which.
It means let me summarize.
When we call the next function in the middleware, it doesn't know okay.
It doesn't know which handler will be executed
Okay.
After we go to next it doesn't know anything.
But with the guard it has an execution context.
Okay.
It means it's more powerful than the middleware a little bit.
Now that enough.
Let me show you how we can create a guard inside the NestJS.
Inside the guard, right
Here I will create one more folder call guards
And again, you don't need to remember that code
Okay.
This is just the common code from Nestjs.
And it's already provided by documentation.
You doesn't know.
Doesn't need to know what's in the code.
Okay, let me copy everything over here.
And I will create auth.guard.ts and paste everything right here.
And now it have a validate request.
Right now I will return true
Now you can see it will return a boolean or promise.
Boolean.
Uh.
Observable.
Boolean.
And the reason because promise Boolean right here, it can be used by async function.
Right.
For example if this is an async function.
Sorry for that.
Let me see if I use async function right here.
Oh, because if I use the async function, it clearly returns the promise.
Okay.
It doesn't have any idea to return another.
Okay.
But anyway you canActive right here this will be return a boolean or the promise or the observable boolean.
Right now we will not focus inside the observable boolean.
Now, why this here returns a boolean.
Let me read more about that.
Now you can see, if it returns a true, the request will be processed.
If return false, it will deny the request.
Let me show you how it can be used.
For example, I can use the @UseGuard inside the controller or we can use something like this.
Okay, we can mark the class or the instance of class, or we can use global.
Or something like here, I will discuss this syntax later.
refactor.
Okay.
Anyway let me scroll up a little bit.
So here, in order to use a "guard" we usually use the decorator will be "@UseGuard" right here.
If I go to the post controller and I will use the controller @UseGuard
I will pass the "AuthGuard" my guards
Insde here, let's see, because this returns true, it will process, right?
It means this will work.
But if I return false.
It doesn't work.
Right.
So this is the idea.
Later I will check if the current user is admin, I will allow it will be delete some
post or something like this.
Okay.
We will authorization using the "Guard" and we can authentication also inside the "Guard" right here the
okay I think this is the guard.
So hopefully you understand when we use the guard inside the NestJS application.