Challenge - 5 Problems
Passport.js Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this NestJS Passport.js local strategy validation?
Given the following local strategy validate method, what will be the result when a user with username 'john' and password 'pass123' is validated?
NestJS
async validate(username: string, password: string): Promise<any> { if (username === 'john' && password === 'pass123') { return { userId: 1, username: 'john' }; } return null; }
Attempts:
2 left
💡 Hint
Think about what the method returns when username and password match.
✗ Incorrect
The validate method returns a user object when credentials match, otherwise null. Here, username and password match, so it returns the user object.
❓ state_output
intermediate2:00remaining
What is the value of req.user after successful JWT authentication?
In a NestJS controller protected by Passport JWT strategy, what will be the value of req.user if the JWT token is valid and contains { sub: 42, username: 'alice' }?
NestJS
import { Controller, Get, Req, UseGuards } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; @Controller('profile') export class ProfileController { @UseGuards(AuthGuard('jwt')) @Get() getProfile(@Req() req) { return req.user; } }
Attempts:
2 left
💡 Hint
What does Passport attach to req after successful JWT validation?
✗ Incorrect
Passport attaches the decoded JWT payload to req.user after successful authentication.
📝 Syntax
advanced2:00remaining
Which option correctly implements a Passport local strategy in NestJS?
Choose the correct implementation of a Passport local strategy validate method in NestJS that returns a user object if username is 'admin' and password is 'secret'.
Attempts:
2 left
💡 Hint
The validate method should throw an exception if credentials are invalid.
✗ Incorrect
In NestJS Passport strategies, the validate method should throw UnauthorizedException on failure, not return null.
🔧 Debug
advanced2:00remaining
Why does this Passport JWT strategy fail to authenticate?
Given this JWT strategy validate method, why does authentication always fail?
async validate(payload: any) {
if (payload.sub) {
return { userId: payload.sub };
}
return null;
}
Attempts:
2 left
💡 Hint
Check what happens when validate returns null in NestJS Passport.
✗ Incorrect
Returning null from validate causes Passport to reject authentication silently. It should throw UnauthorizedException instead.
🧠 Conceptual
expert2:00remaining
What is the main purpose of Passport.js in NestJS applications?
Choose the best description of Passport.js role in NestJS apps.
Attempts:
2 left
💡 Hint
Think about what Passport.js is known for in Node.js apps.
✗ Incorrect
Passport.js is a middleware for authentication supporting many strategies, not for database or routing.