nest.js
swagger api 문서 페이지 접근시 아이디, 암호 인증
FaustK
2022. 11. 24. 13:09
api 문서가 있는 페이지에 접근 제어를 하고 싶다면 express-basic-auth 라이브러릴 활용 이름, 비밀번호로 로그인하게 할 수 있다.
로그인된 사용자만 해당 페이지 접속.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { winstonLogger } from './utils/winston.util';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import basicAuth from 'express-basic-auth';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: winstonLogger,
});
app.setGlobalPrefix('api');
app.use(
['/docs'],
basicAuth({
challenge: true,
users: {
admin: 'password', // key ==> id, value ==> password
},
}),
);
const config = new DocumentBuilder()
.setTitle('example api')
.setDescription('The example API description')
.setVersion('1.0')
.addTag('example')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('docs', app, document);
await app.listen(5001);
}
bootstrap();