0
0
NestJSframework~20 mins

Passport.js integration in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Passport.js Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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;
}
AReturns { userId: 1, username: 'john' }
BReturns null
CThrows an error
DReturns undefined
Attempts:
2 left
💡 Hint
Think about what the method returns when username and password match.
state_output
intermediate
2: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;
  }
}
A{ sub: 42, username: 'alice' }
Bundefined
Cnull
DThrows an UnauthorizedException
Attempts:
2 left
💡 Hint
What does Passport attach to req after successful JWT validation?
📝 Syntax
advanced
2: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'.
A
async validate(username: string, password: string) {
  if (username === 'admin' &amp;&amp; password === 'secret') {
    return { id: 1, username: 'admin' };
  }
  return null;
}
B
async validate(username: string, password: string): Promise&lt;any&gt; {
  if (username === 'admin' &amp;&amp; password === 'secret') {
    return { id: 1, username: 'admin' };
  }
  throw new UnauthorizedException();
}
C
async validate(username: string, password: string): Promise&lt;void&gt; {
  if (username === 'admin' &amp;&amp; password === 'secret') {
    return { id: 1, username: 'admin' };
  }
}
D
validate(username, password) {
  if (username === 'admin' &amp;&amp; password === 'secret') {
    return { id: 1, username: 'admin' };
  }
  return null;
}
Attempts:
2 left
💡 Hint
The validate method should throw an exception if credentials are invalid.
🔧 Debug
advanced
2: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; }
AThe method should return payload directly, not a new object.
BThe validate method must be synchronous, async causes failure.
CReturning null instead of throwing UnauthorizedException causes failure.
DThe payload.sub property is undefined, so it always returns null.
Attempts:
2 left
💡 Hint
Check what happens when validate returns null in NestJS Passport.
🧠 Conceptual
expert
2:00remaining
What is the main purpose of Passport.js in NestJS applications?
Choose the best description of Passport.js role in NestJS apps.
ATo replace NestJS guards with a simpler authorization system.
BTo handle database connections and ORM integration automatically.
CTo serve static files and manage HTTP routing.
DTo provide a modular and flexible authentication middleware supporting many strategies.
Attempts:
2 left
💡 Hint
Think about what Passport.js is known for in Node.js apps.