NestJS Set Password to Swagger Document
1. Generate new project
$ nest new [project name]
2. Install Swagger
$ npm i @nestjs/swagger swagger-ui-express
$ yarn add @nestjs/swagger swagger-ui-express
3. Install express-basic-auth
$ npm i express-basic-auth
$ yarn add express-basic-auth
// ./src/main.ts
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import * as basicAuth from 'express-basic-auth';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(
// set a user and password
['/api-docs', '/api-docs-json'],
basicAuth({
challenge: true,
users: {
admin: 'safepassword', // [user name] : [password : string]
},
}),
);
const options = new DocumentBuilder()
.setTitle('Set password to Swagger Doc')
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api-docs', app, document);
await app.listen(3000);
}
bootstrap();
code1) main.ts code
![]() |
picture1) init page |
![]() |
picture2) enter username and password |
![]() |
picture3) access to document |
gitHub Link : https://github.com/Alex-Choi0/NestJS_Swagger_Password.git
댓글
댓글 쓰기