Guards

Server Enviroment
Client Enviroment

Overview

Guards can be used to control access to a handler. They are executed before the handler method and can allow or deny execution of the handler method. They may also change the execution context. Interceptors are executed after guards.

Creating a Guard

Guards are classes that implement the Guard interface. The Guard interface has a single method, canActivate, which returns a boolean or a promise that resolves to a boolean.

@Injectable()
export class OnlyRipeMangoGuard implements Guard {
    public canActivate(context: ExecutionContext) {
        const data = context.getMangoData();
        if (data.getParam('isRipe')) {
            return false;
        }
        return true;
    }
}
typescript

Using a Guard

Guards can be used at the module, controller or handler level. To use a guard, use the @UseGuards decorator on the module, controller or handler, and pass the guard class as an argument.

NOTE
  • When you specify a guard as a class reference, it is instantiated as a singleton by the dependency injection container. This means that the same instance is used for all requests. If you want to use a new instance for each request, you can specify an instance of the guard instead of a class reference.
  • If you're using class references, don't forget to add the @Injectable decorator and add the guard to the providers array in the module.
+
+
+
@Controller()
@UseGuards(OnlyRipeMangoGuard) 
export class DeliciousMangoController {
    @UseGuards(OnlyRipeMangoGuard) 
    @On('eat')
    public onEat() {
        console.log('I love mangoes 🥭');
    }
}

@UseGuards(OnlyRipeMangoGuard) 
@Module({
    controllers: [DeliciousMangoController],
    providers: [OnlyRipeMangoGuard],
})
export class DeliciousMangoModule {}
typescript

Also, instead of providing a class reference, you can provide an instance of the guard. However, dependency injection will not work for the guard instance.

+
@Controller()
@UseGuards(new OnlyRipeMangoGuard()) 
export class DeliciousMangoController {}
typescript
Last update at: 2024/04/29 10:15:50