JwtModule

Provides JwtService for signing and verifying JSON Web Tokens.

Since NestJS 10/11 Spec ↗

Syntax

JwtModule.register(options) | JwtModule.registerAsync(options)

Parameters

NameTypeRequiredDescription
options JwtModuleOptions No secret or keys plus signOptions such as expiresIn.

Returns

DynamicModule — A module exporting JwtService.

Examples

import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    JwtModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (c: ConfigService) => ({
        secret: c.getOrThrow('JWT_SECRET'),
        signOptions: { expiresIn: '15m' },
      }),
    }),
  ],
})
export class AuthModule {}
import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class AuthService {
  constructor(private jwt: JwtService) {}
  sign(userId: string) {
    return this.jwt.signAsync({ sub: userId });
  }
}

Notes

Install @nestjs/jwt. Prefer registerAsync() to pull secrets from ConfigService. Use JwtService.signAsync()/verifyAsync() and pair with a Passport JWT strategy for route protection.