Challenge - 5 Problems
Local Strategy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when the local strategy validates a user with correct credentials?
Consider a NestJS local strategy that checks username and password. What is the output when a user provides the correct username and password?
NestJS
async validate(username: string, password: string) { const user = await this.authService.validateUser(username, password); if (!user) { throw new UnauthorizedException(); } return user; }
Attempts:
2 left
💡 Hint
Think about what the validate method should do when credentials are correct.
✗ Incorrect
The validate method returns the user object when credentials are correct so that the authentication flow can continue. It throws an error only if validation fails.
📝 Syntax
intermediate2:00remaining
Which option correctly implements the Passport local strategy constructor in NestJS?
Choose the correct constructor code snippet for a NestJS local strategy extending PassportStrategy(LocalStrategy).
NestJS
import { Strategy } from 'passport-local'; import { PassportStrategy } from '@nestjs/passport'; export class LocalStrategy extends PassportStrategy(Strategy) { constructor(private authService: AuthService) { super({ usernameField: 'email' }); } async validate(username: string, password: string) { // validation logic } }
Attempts:
2 left
💡 Hint
Remember how to pass options to the PassportStrategy constructor and inject services.
✗ Incorrect
Option A correctly uses dependency injection and passes the usernameField option to the super constructor.
🔧 Debug
advanced2:00remaining
Why does this local strategy fail to throw UnauthorizedException even with incorrect credentials?
Review the validate method below. Why does it fail to throw UnauthorizedException even when the username and password are incorrect?
async validate(username: string, password: string) {
const user = await this.authService.validateUser(username, password);
if (user === null) {
throw new UnauthorizedException();
}
return user;
}
NestJS
async validate(username: string, password: string) { const user = await this.authService.validateUser(username, password); if (user === null) { throw new UnauthorizedException(); } return user; }
Attempts:
2 left
💡 Hint
Check what value validateUser returns when credentials are invalid.
✗ Incorrect
If validateUser returns undefined on failure, checking for null will not catch it, so the exception is never thrown correctly.
❓ state_output
advanced2:00remaining
What is the value of 'user' after validate returns successfully?
Given this local strategy validate method:
async validate(username: string, password: string) {
const user = await this.authService.validateUser(username, password);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
If validateUser returns { id: 5, username: 'alice' }, what is the value of user after validate returns?
NestJS
async validate(username: string, password: string) { const user = await this.authService.validateUser(username, password); if (!user) { throw new UnauthorizedException(); } return user; }
Attempts:
2 left
💡 Hint
What does the method return if the user is valid?
✗ Incorrect
The method returns the user object from validateUser if it exists, so user will be the object { id: 5, username: 'alice' }.
🧠 Conceptual
expert2:00remaining
Which option best explains why the local strategy uses 'usernameField' option?
In NestJS local strategy, why do we pass { usernameField: 'email' } to the PassportStrategy constructor?
Attempts:
2 left
💡 Hint
Think about how Passport extracts credentials from the login form.
✗ Incorrect
The usernameField option tells Passport which field name to look for in the login request. By default, it looks for 'username', but many apps use 'email' instead.