Bird
0
0

Which combination correctly applies authentication and explains why it secures the API?

hard📝 Application Q15 of 15
NestJS - Authentication
You want to secure a NestJS API route so only logged-in users can access it. Which combination correctly applies authentication and explains why it secures the API? Options: A) Use @UseGuards(AuthGuard('jwt')) to check user identity before route access, preventing strangers. B) Use @UseInterceptors(AuthInterceptor) to log requests, which blocks unauthorized users. C) Use @UseFilters(AuthFilter) to catch errors, which stops unauthorized access. D) Use @UsePipes(AuthPipe) to validate data, which ensures only valid users access the route.
AUse @UsePipes(AuthPipe) to validate data, which ensures only valid users access the route.
BUse @UseInterceptors(AuthInterceptor) to log requests, which blocks unauthorized users.
CUse @UseFilters(AuthFilter) to catch errors, which stops unauthorized access.
DUse @UseGuards(AuthGuard('jwt')) to check user identity before route access, preventing strangers.
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct NestJS feature for authentication

    Guards are designed to control access by checking user identity before route execution.
  2. Step 2: Understand why guards secure APIs

    Using @UseGuards(AuthGuard('jwt')) ensures only authenticated users with valid JWT tokens can access the route, blocking strangers.
  3. Final Answer:

    Use @UseGuards(AuthGuard('jwt')) to check user identity before route access, preventing strangers. -> Option D
  4. Quick Check:

    Guards + AuthGuard('jwt') = Secure API access [OK]
Quick Trick: Use guards with AuthGuard('jwt') to secure routes [OK]
Common Mistakes:
  • Confusing interceptors, filters, or pipes with authentication guards
  • Thinking logging or error handling blocks unauthorized users
  • Assuming data validation equals authentication

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes