Consider a NestJS controller method protected by two guards: AuthGuard and RolesGuard. What is the behavior when both guards are applied?
import { Controller, Get, UseGuards } from '@nestjs/common'; import { AuthGuard } from './auth.guard'; import { RolesGuard } from './roles.guard'; @Controller('items') export class ItemsController { @Get() @UseGuards(AuthGuard, RolesGuard) findAll() { return 'Items list'; } }
Think about how NestJS processes multiple guards in sequence.
In NestJS, multiple guards applied to a route run one after another in the order they are declared. If any guard denies access by returning false, the request is denied immediately.
Choose the correct syntax to apply AuthGuard and RolesGuard to a controller method.
Remember the decorator syntax for multiple guards.
The @UseGuards decorator accepts multiple guard classes either as separate arguments or as an array, not as logical expressions. Logical expressions like || or && evaluate to a single class.
Given two guards AuthGuard and RolesGuard applied to a route, if AuthGuard returns false, what happens?
import { CanActivate, ExecutionContext } from '@nestjs/common'; class AuthGuard implements CanActivate { canActivate(context: ExecutionContext): boolean { return false; // denies access } } class RolesGuard implements CanActivate { canActivate(context: ExecutionContext): boolean { console.log('RolesGuard checked'); return true; } } // Route uses @UseGuards(AuthGuard, RolesGuard)
Consider the guard execution order and short-circuit behavior.
Guards run in order and stop at the first guard that returns false, denying the request immediately without running subsequent guards.
Examine the following code snippet. Why does it apply only RolesGuard instead of both?
import { UseGuards } from '@nestjs/common'; @UseGuards(AuthGuard && RolesGuard) getData() { return 'data'; }
Check how decorators expect arguments.
The @UseGuards decorator expects guard classes as separate arguments or an array. Using AuthGuard && RolesGuard evaluates to RolesGuard (both classes truthy), applying only one guard.
When multiple guards return Promise<boolean> (are async), how does NestJS process them?
Think about how async functions and promises work in sequence.
NestJS supports async guards by awaiting each guard's canActivate method in order. If any guard resolves to false, the request is denied immediately without running further guards.