Consider a NestJS controller with a guard applied at both the controller and method levels. Which guard(s) will run when the method is called?
import { Controller, Get, UseGuards } from '@nestjs/common'; @UseGuards(ControllerGuard) @Controller('items') export class ItemsController { @UseGuards(MethodGuard) @Get() findAll() { return 'items'; } }
Think about how NestJS applies guards in a hierarchical order.
Guards applied at the controller level run before method-level guards. So both guards run, with the controller guard first, then the method guard.
If a global guard denies access, what happens to guards applied at controller or method levels?
import { Injectable, CanActivate, ExecutionContext, UseGuards, Controller, Get } from '@nestjs/common'; @Injectable() class GlobalGuard implements CanActivate { canActivate(context: ExecutionContext) { return false; // always deny } } @UseGuards(GlobalGuard) @Controller('users') @UseGuards(ControllerGuard) export class UsersController { @UseGuards(MethodGuard) @Get() findAll() { return 'users'; } }
Global guards run before any other guards.
Global guards run first and can short-circuit the request. If a global guard denies access, no other guards run.
Which option correctly applies two guards at the method level in NestJS?
import { UseGuards } from '@nestjs/common'; class GuardOne {} class GuardTwo {} class SampleController { @UseGuards(/* ??? */) someMethod() {} }
Check the official NestJS syntax for multiple guards.
The @UseGuards decorator accepts multiple guard classes as separate arguments.
Given this code, why does AuthGuard not run when calling GET /profile?
import { Controller, Get, UseGuards } from '@nestjs/common'; @Controller('profile') export class ProfileController { @Get() @UseGuards(AuthGuard) getProfile() { return 'profile data'; } }
Check if the guard class is correctly imported and provided.
If the guard class is not imported or registered, NestJS silently skips running it. The decorator placement is correct, and async is not required.
In NestJS, if you have a global guard, a controller-level guard, and a method-level guard all applied, in what order do they execute?
Think about the scope from broadest to narrowest.
Guards run from the broadest scope to the narrowest: global guards first, then controller guards, then method guards.