0
0
NestJSframework~10 mins

Passport.js integration in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import PassportModule in a NestJS module.

NestJS
import { Module } from '@nestjs/common';
import { PassportModule } from '[1]';

@Module({
  imports: [PassportModule.register({ defaultStrategy: 'jwt' })],
})
export class AuthModule {}
Drag options to blanks, or click blank then click option'
A@nestjs/passport
B@nestjs/jwt
Cpassport
Dpassport-jwt
Attempts:
3 left
💡 Hint
Common Mistakes
Importing PassportModule from 'passport' or 'passport-jwt' directly.
Confusing JWT package with Passport package.
2fill in blank
medium

Complete the code to create a JWT strategy class extending PassportStrategy.

NestJS
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy, ExtractJwt } from 'passport-jwt';

@Injectable()
export class JwtStrategy extends PassportStrategy([1]) {
  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: 'secretKey',
    });
  }

  async validate(payload: any) {
    return { userId: payload.sub, username: payload.username };
  }
}
Drag options to blanks, or click blank then click option'
AExtractJwt
BJwtStrategy
CStrategy
DPassportStrategy
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the strategy class name incorrectly.
Using JwtStrategy instead of Strategy.
3fill in blank
hard

Fix the error in the AuthGuard usage to protect a route with JWT strategy.

NestJS
import { Controller, Get, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Controller('profile')
export class ProfileController {
  @Get()
  @UseGuards([1]('jwt'))
  getProfile() {
    return { message: 'This is a protected route' };
  }
}
Drag options to blanks, or click blank then click option'
AAuthGuard
BPassportStrategy
CJwtStrategy
DUseGuards
Attempts:
3 left
💡 Hint
Common Mistakes
Using JwtStrategy instead of AuthGuard.
Calling UseGuards instead of AuthGuard inside the decorator.
4fill in blank
hard

Fill both blanks to register PassportModule and JwtStrategy in the AuthModule.

NestJS
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { JwtStrategy } from './jwt.strategy';

@Module({
  imports: [[1].register({ defaultStrategy: 'jwt' })],
  providers: [[2]],
  exports: [[1]],
})
export class AuthModule {}
Drag options to blanks, or click blank then click option'
APassportModule
BJwtModule
CJwtStrategy
DAuthGuard
Attempts:
3 left
💡 Hint
Common Mistakes
Using JwtModule instead of PassportModule.
Forgetting to add JwtStrategy to providers.
5fill in blank
hard

Fill all three blanks to create a login route that uses AuthService to validate user and issue JWT.

NestJS
import { Controller, Post, Body } from '@nestjs/common';
import { AuthService } from './auth.service';

@Controller('auth')
export class AuthController {
  constructor(private readonly authService: [1]) {}

  @Post('login')
  async login(@Body() [2]: any) {
    return this.authService.[3]([2].username, [2].password);
  }
}
Drag options to blanks, or click blank then click option'
AAuthService
BloginDto
CvalidateUser
DAuthController
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong service or controller names.
Calling a non-existent method on AuthService.