NestJS · Middle · Question 5 of 75
How do you register a global guard that can inject other providers?
providers: [{ provide: APP_GUARD, useClass: JwtAuthGuard }]- 1Global guards can't use DI at all
- 2With
app.useGlobalGuards(new JwtAuthGuard()), which also gets DI - ✓Correct answer: With the
APP_GUARDtoken in a module's providers
Explanation
app.useGlobalGuards() takes an instance created outside the DI container, so its dependencies aren't injected. APP_PIPE, APP_INTERCEPTOR and APP_FILTER work the same way.