dev-resources.site
for different kinds of informations.
Seamlessly Integrate Swagger with JWT Authentication in NestJS
Published at
11/3/2024
Categories
nestjs
jwt
swagger
webdev
Author
Mofajjal Rasul
Enhancing your NestJS API with robust documentation and JWT authentication is essential for a smooth developer experience. In this article, we'll walk you through integrating Swagger UI into your NestJS application, utilizing dummy data, and customizing the UI with external assets.
Step 1: Install Required Packages
Start by installing the necessary packages for Swagger support:
npm install @nestjs/swagger
Step 2: Create Swagger Configuration
Create a file (swagger.config.ts
), configure Swagger using the following code snippet with dummy data for demonstration:
import { INestApplication } from "@nestjs/common";
import {
DocumentBuilder,
SwaggerCustomOptions,
SwaggerModule,
} from "@nestjs/swagger";
const documentConfig = new DocumentBuilder()
.setTitle("Dummy API") // API Title
.setVersion("1.0.0") // API Version
.addBearerAuth( // Adds JWT Bearer authentication
{
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
name: "Authorization",
description: "Enter JWT token",
in: "header",
},
"Bearer",
)
.addSecurityRequirements("Bearer") // Security requirement
.build();
const swaggerUiOptions: SwaggerCustomOptions = {
swaggerOptions: {
persistAuthorization: true, // Retain authorization
},
customSiteTitle: "Dummy API Documentation", // Swagger UI title
customJs: [ // External JS files
"https://unpkg.com/[email protected]/swagger-ui-bundle.js",
"https://unpkg.com/[email protected]/swagger-ui-standalone-preset.js",
],
customCssUrl: [ // External CSS files
"https://unpkg.com/[email protected]/swagger-ui.css",
],
};
// Function to configure Swagger UI
export const configureSwaggerUI = (app: INestApplication<any>) => {
const document = SwaggerModule.createDocument(app, documentConfig); // Create Swagger document
SwaggerModule.setup("swagger", app, document, swaggerUiOptions); // Setup Swagger UI
};
Explanation
- Document Configuration: Set up a Swagger document with a title, version, and authentication.
- Swagger UI Options: Configure options, including a custom title and external assets.
Step 3: Integrate Swagger in Your NestJS Application
Add the Swagger configuration in your main.ts
file:
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { configureSwaggerUI } from "./swagger.config"; // Import Swagger configuration
async function bootstrap() {
const app = await NestFactory.create(AppModule);
configureSwaggerUI(app); // Configure Swagger
await app.listen(4000); // Start the application
}
bootstrap();
Step 4: Run Your Application
Now, run your NestJS application:
npm run start
Step 5: Access Swagger UI
Open your browser and navigate to:
http://localhost:4000/swagger
You should see the Swagger UI with your Dummy API documentation!
Articles
12 articles in total
Simplify Email Testing with a Local Papercut SMTP Server Using Docker
read article
Setting Up a Home Media Server with Docker: A Beginner's Guide
read article
Master React API Management with TanStack React Query: Best Practices & Examples
read article
Building a Linux Based Minimal and Efficient .NET 8 Application Docker Image
read article
Setting Up a VPS Server with Docker, Nginx Proxy Manager, and Portainer
read article
Integrating Stripe Payment Intent in NestJS with Webhook Handling
read article
Comprehensive Encryption and Security Service in NestJS: Argon2 Hashing, Token Generation, and AES Encryption
read article
Custom Role-Based Access Control in NestJS Using Custom Guards
read article
Implementing HTTP Request and Response Encryption in ASP.NET Core with Custom Attributes
read article
A Generic Repository Pattern for NestJS with Mongoose for MongoDB
read article
Integrating Cloudinary with NestJS for Image Management
read article
Seamlessly Integrate Swagger with JWT Authentication in NestJS
currently reading
Featured ones: