Passport & JWT Basics

Preview — 3 of 10 questions

What is the role of PassportModule in a NestJS application?

javascript
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';

@Module({
  imports: [PassportModule],
})
export class AuthModule {}
AIt encrypts the database connection
BIt integrates the Passport.js authentication library so strategies and guards work within Nest's DI
CIt generates JWT tokens directly
DIt configures CORS

What does @UseGuards(AuthGuard('local')) do on a login route?

javascript
import { Controller, Post, UseGuards, Request } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Controller('auth')
export class AuthController {
  @UseGuards(AuthGuard('local'))
  @Post('login')
  login(@Request() req): unknown {
    return req.user; // populated by the local strategy
  }
}
AIssues a JWT automatically
BEncrypts the request body
CSkips authentication entirely
DTriggers the registered local strategy to validate username/password before the handler runs

What does JwtModule.register({ secret, signOptions }) configure?

javascript
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';

@Module({
  imports: [
    JwtModule.register({
      secret: 'dev-secret',
      signOptions: { expiresIn: '15m' },
    }),
  ],
})
export class AuthModule {}
AThe signing secret and default options (like expiresIn) used by JwtService
BThe database schema for users
CThe CORS origins allowed
DThe Passport strategy name

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.