AuthGuard and AdminGuardAppController with a route GET /protected/protected route using @UseGuards()CanActivate interface and return true or false accordinglyAuthGuard and AdminGuardAppController with a route GET /protected/protected route using @UseGuards()CanActivate interface and return true or false accordinglyAuthGuard and AdminGuard. Each should implement the CanActivate interface from @nestjs/common and have a canActivate() method that returns true.Remember to import CanActivate and ExecutionContext from @nestjs/common.
AppController with a method getProtected() decorated with @Get('protected') from @nestjs/common. The method should return the string 'Protected content'.Import Controller and Get from @nestjs/common and decorate the class and method properly.
@UseGuards() decorator from @nestjs/common on the getProtected() method. Pass both AuthGuard and AdminGuard inside @UseGuards() to combine them.Import UseGuards and add @UseGuards(AuthGuard, AdminGuard) above the getProtected() method.
export keywords to the AppController, AuthGuard, and AdminGuard classes so they can be used in other parts of the application.Make sure all classes have the export keyword before class.
What happens when you use @UseGuards(AuthGuard, RolesGuard) on a NestJS route handler?
Choose the best description.
@UseGuards(), NestJS runs them in sequence and requires all to approve access.AuthGuard and RolesGuard must return true for the request to proceed.Which of the following is the correct syntax to apply two guards AuthGuard and RolesGuard on a controller method?
?@UseGuards(), separated by commas.Given the following code snippet, what will be the output when a request is made?
@UseGuards(AuthGuard, RolesGuard)
@Get('profile')
getProfile() {
return 'User Profile';
}
// AuthGuard returns true
// RolesGuard returns false
AuthGuard returns true but RolesGuard returns false, so access is denied and an exception is thrown.Identify the error in this NestJS controller snippet using multiple guards:
@UseGuards(AuthGuard RolesGuard)
@Get('data')
getData() {
return 'Data';
}
@UseGuards() must be separated by commas.AuthGuard and RolesGuard, causing a syntax error.You want to protect a route so that only authenticated users with the role 'admin' can access it. You have AuthGuard and RolesGuard available. How should you combine them in your controller method?
@UseGuards(). Order matters: AuthGuard first authenticates and sets req.user, then RolesGuard checks the role.Pass the test and this pattern moves unknown