Challenge - 5 Problems
NestJS Authentication Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
What is the primary role of authentication in NestJS APIs?
Authentication in NestJS APIs is mainly used to:
Attempts:
2 left
💡 Hint
Think about what authentication means in everyday life, like showing an ID to prove who you are.
✗ Incorrect
Authentication confirms who the user is. In NestJS APIs, it ensures only verified users can access protected routes.
❓ component_behavior
intermediate2:00remaining
What happens when a NestJS API receives a request without valid authentication?
If a request to a protected NestJS API route lacks valid authentication, the API will:
Attempts:
2 left
💡 Hint
Think about what should happen if someone tries to enter a locked door without a key.
✗ Incorrect
Without valid authentication, NestJS APIs respond with a 401 error to prevent unauthorized access.
❓ state_output
advanced2:00remaining
What is the output of this NestJS guard when authentication fails?
Consider this simplified NestJS guard code snippet that checks authentication:
canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
if (!request.user) {
throw new UnauthorizedException('User not authenticated');
}
return true;
}
What will the API respond with if the request has no user?NestJS
canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
if (!request.user) {
throw new UnauthorizedException('User not authenticated');
}
return true;
}Attempts:
2 left
💡 Hint
UnauthorizedException in NestJS sends a 401 status code.
✗ Incorrect
The guard throws an UnauthorizedException which results in a 401 response with the given message.
📝 Syntax
advanced2:00remaining
Which code snippet correctly applies authentication guard to a NestJS controller route?
You want to protect a NestJS controller route so only authenticated users can access it. Which code correctly applies the AuthGuard?
Attempts:
2 left
💡 Hint
AuthGuard needs the strategy name as a string argument inside parentheses.
✗ Incorrect
Option A correctly uses AuthGuard with the 'jwt' strategy to protect the route.
Option A is missing the AuthGuard function call.
Option A lacks the strategy name.
Option A uses 'local' strategy which is not for JWT authentication.
🔧 Debug
expert3:00remaining
Why does this NestJS API allow access without authentication despite using AuthGuard?
Given this NestJS controller code:
@Controller('data')
export class DataController {
@Get()
@UseGuards(AuthGuard('jwt'))
getData() {
return 'Protected data';
}
@Get('public')
getPublicData() {
return 'Public data';
}
}
A user can access the 'getData' route without a token. What is the most likely reason?NestJS
@Controller('data') export class DataController { @Get() @UseGuards(AuthGuard('jwt')) getData() { return 'Protected data'; } @Get('public') getPublicData() { return 'Public data'; } }
Attempts:
2 left
💡 Hint
Guards must be properly registered and the strategy must be configured in the module.
✗ Incorrect
If the AuthGuard is not properly registered or the JWT strategy is not configured, the guard will not block unauthenticated requests even if used.