May 26, 2022

How to securing access to of Swagger with HTTP Basic Auth? With example if TypeScript NestJS.



- First set up NestJS TypeScrip basic project.

- Install express-basic-auth module using this command: npm i express-basic-auth

- import module on main.ts file

- Full example of main.ts file:


// import auth module

import * as basicAuth from 'express-basic-auth';

// Add HTTP Basic Auth

app.use(

    basicAuth({

        challenge: true,

        users: {

            defineUserName: 'passwrd here',

        },

    }),

);


// Main Code

const options = new DocumentBuilder()

    .setTitle('First App')

    .setSchemes('https')

    .setDescription('Firsy App API documentation')

    .setVersion('1.0')

    .build()


const document = SwaggerModule.createDocument(app, options)

SwaggerModule.setup('docs', app, document, {

    customSiteTitle: 'First App documentation',

});


Enjoy! 

5 comments:

  1. Web Development Course in Noida
    https://aptronsolutions.home.blog/2022/08/09/web-development-institute-in-noida-web-development-trainng-in-noida/

    ReplyDelete
  2. Great post..Rob Tech World is one of the leading and dependable Web Designer In Punjab. Website Designing Company In Zirakpur

    ReplyDelete
  3. Thanks for this wonderful information AWeb Developer is a professional who is responsible for the design and construction of websites.

    ReplyDelete

Integrating Google reCAPTCHA v3 in HTML Form with PHP

  What is Google reCAPTCHA v3? Google reCAPTCHA is a free service that helps protect websites from spam and abuse. reCAPTCHA v3 is the lates...