Bird
0
0

Which of the following is the correct syntax to create a custom RolesGuard in NestJS?

easy📝 Syntax Q3 of 15
NestJS - Authentication
Which of the following is the correct syntax to create a custom RolesGuard in NestJS?
Aexport const RolesGuard = () => { /* logic */ }
Bexport function RolesGuard() { return (req, res) => { /* logic */ } }
Cexport interface RolesGuard { canActivate(): boolean }
Dexport class RolesGuard implements CanActivate { canActivate(context: ExecutionContext) { /* logic */ } }
Step-by-Step Solution
Solution:
  1. Step 1: Recall NestJS guard syntax

    Guards are classes implementing CanActivate with canActivate method.
  2. Step 2: Check options for this pattern

    Only export class RolesGuard implements CanActivate { canActivate(context: ExecutionContext) { /* logic */ } } defines a class implementing CanActivate with canActivate method.
  3. Final Answer:

    Class implementing CanActivate with canActivate method -> Option D
  4. Quick Check:

    Custom guard syntax = class with canActivate [OK]
Quick Trick: Guards are classes implementing CanActivate interface [OK]
Common Mistakes:
  • Using functions instead of classes for guards
  • Defining interfaces instead of classes
  • Missing canActivate method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes