0
0
NestJSframework~20 mins

Combining multiple guards in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Guard Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when multiple guards are applied to a NestJS route?

Consider a NestJS controller method protected by two guards: AuthGuard and RolesGuard. What is the behavior when both guards are applied?

NestJS
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';
  }
}
ABoth guards run in parallel; the request is allowed if either guard returns true.
BBoth guards run in the order declared; if any guard returns false, the request is denied.
COnly the first guard runs; the second guard is ignored.
DGuards run randomly; the request is allowed only if the last guard returns true.
Attempts:
2 left
💡 Hint

Think about how NestJS processes multiple guards in sequence.

📝 Syntax
intermediate
1:30remaining
Which code correctly applies multiple guards to a NestJS route?

Choose the correct syntax to apply AuthGuard and RolesGuard to a controller method.

A
@UseGuards(AuthGuard || RolesGuard)
getData() { return 'data'; }
B
@UseGuards([AuthGuard, RolesGuard])
getData() { return 'data'; }
C
@UseGuards(AuthGuard && RolesGuard)
getData() { return 'data'; }
D
@UseGuards(AuthGuard, RolesGuard)
getData() { return 'data'; }
Attempts:
2 left
💡 Hint

Remember the decorator syntax for multiple guards.

state_output
advanced
2:00remaining
What is the output when the first guard denies access?

Given two guards AuthGuard and RolesGuard applied to a route, if AuthGuard returns false, what happens?

NestJS
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)
ARequest is denied immediately; RolesGuard is not executed.
BRequest is allowed because RolesGuard returns true.
CRequest is denied after both guards run.
DRequest causes a runtime error because of conflicting guards.
Attempts:
2 left
💡 Hint

Consider the guard execution order and short-circuit behavior.

🔧 Debug
advanced
1:30remaining
Why does this combined guard setup apply only one guard?

Examine the following code snippet. Why does it apply only RolesGuard instead of both?

NestJS
import { UseGuards } from '@nestjs/common';

@UseGuards(AuthGuard && RolesGuard)
getData() {
  return 'data';
}
AUsing logical AND (&&) between guard classes evaluates to the last truthy class instead of both.
BGuards must be instantiated before use; classes alone cause errors.
CMissing parentheses around guards causes a syntax error.
DThe guards are applied correctly; error is unrelated.
Attempts:
2 left
💡 Hint

Check how decorators expect arguments.

🧠 Conceptual
expert
2:30remaining
How does NestJS handle asynchronous guards when combined?

When multiple guards return Promise<boolean> (are async), how does NestJS process them?

ANestJS does not support async guards; it throws an error.
BNestJS runs all guards in parallel and allows access if any resolves to true.
CNestJS awaits each guard in order; if any resolves to false, it stops and denies access.
DNestJS runs guards sequentially but ignores their resolved values.
Attempts:
2 left
💡 Hint

Think about how async functions and promises work in sequence.