0
0
NestJSframework~20 mins

Guard binding levels in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Guard Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Guard Binding Level: Controller vs Method

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?

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

@UseGuards(ControllerGuard)
@Controller('items')
export class ItemsController {
  @UseGuards(MethodGuard)
  @Get()
  findAll() {
    return 'items';
  }
}
AOnly <code>ControllerGuard</code> runs before <code>findAll</code>.
BBoth <code>ControllerGuard</code> and <code>MethodGuard</code> run, with <code>ControllerGuard</code> first.
COnly <code>MethodGuard</code> runs before <code>findAll</code>.
DBoth <code>ControllerGuard</code> and <code>MethodGuard</code> run, with <code>MethodGuard</code> first.
Attempts:
2 left
💡 Hint

Think about how NestJS applies guards in a hierarchical order.

state_output
intermediate
2:00remaining
Effect of Global Guard on Controller Guards

If a global guard denies access, what happens to guards applied at controller or method levels?

NestJS
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';
  }
}
AOnly GlobalGuard runs and denies access; controller and method guards do not run.
BGlobalGuard runs and denies access; controller and method guards still run after.
CControllerGuard runs first, then GlobalGuard denies access; method guard does not run.
DMethodGuard runs first, then GlobalGuard denies access; controller guard does not run.
Attempts:
2 left
💡 Hint

Global guards run before any other guards.

📝 Syntax
advanced
2:00remaining
Correct Syntax for Applying Multiple Guards

Which option correctly applies two guards at the method level in NestJS?

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

class GuardOne {}
class GuardTwo {}

class SampleController {
  @UseGuards(/* ??? */)
  someMethod() {}
}
A@UseGuards('GuardOne', 'GuardTwo')
B@UseGuards([GuardOne, GuardTwo])
C@UseGuards({GuardOne, GuardTwo})
D@UseGuards(GuardOne, GuardTwo)
Attempts:
2 left
💡 Hint

Check the official NestJS syntax for multiple guards.

🔧 Debug
advanced
2:00remaining
Why Does a Guard Not Run on a Route?

Given this code, why does AuthGuard not run when calling GET /profile?

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

@Controller('profile')
export class ProfileController {
  @Get()
  @UseGuards(AuthGuard)
  getProfile() {
    return 'profile data';
  }
}
AThe <code>@UseGuards</code> decorator must be applied at the controller level, not method level.
BThe <code>@UseGuards</code> decorator is placed after <code>@Get()</code>, so it is ignored.
CThe <code>AuthGuard</code> class is not imported or registered properly.
DThe <code>getProfile</code> method is missing the <code>async</code> keyword.
Attempts:
2 left
💡 Hint

Check if the guard class is correctly imported and provided.

🧠 Conceptual
expert
3:00remaining
Order of Execution for Multiple Guards at Different Levels

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?

AGlobal guard → Controller guard → Method guard
BMethod guard → Controller guard → Global guard
CController guard → Global guard → Method guard
DMethod guard → Global guard → Controller guard
Attempts:
2 left
💡 Hint

Think about the scope from broadest to narrowest.