Concept Flow - Combining multiple guards
Request comes in
Guard 1 runs
Guard 2 runs
The request passes through Guard 1 first. If it passes, Guard 2 runs. If all guards pass, access is allowed; if any guard fails, the request is rejected.
import { UseGuards } from '@nestjs/common'; import { Controller, Get } from '@nestjs/common'; @Controller() export class AppController { @UseGuards(AuthGuard, RolesGuard) @Get('profile') getProfile() { return 'User Profile'; } }
| Step | Guard | Guard Result | Action | Request Outcome |
|---|---|---|---|---|
| 1 | AuthGuard | Pass | Continue to next guard | Pending |
| 2 | RolesGuard | Pass | Allow access | Access granted |
| 3 | End | - | - | Request handled successfully |
| Variable | Start | After AuthGuard | After RolesGuard | Final |
|---|---|---|---|---|
| requestAllowed | false | true | true | true |
Use @UseGuards(Guard1, Guard2) to combine guards. Guards run in order; if any guard returns false, request is denied. All guards must pass to allow access. Useful for layered security checks. Easy to add or remove guards as needed.