javascriptauthenticationswaggernestjs

How to guard nestjs swagger endpoint


How would you go about guarding swagger docs on nestjs as swagger module setup happens on the main.ts file.

 const options = new DocumentBuilder()
      .setTitle("API")
      .setDescription("NestJS api")
      .setVersion("1.0")
      .build();
    const document = SwaggerModule.createDocument(app, options);
    SwaggerModule.setup("swagger", app, document);

I am looking to use a guard like below to the swagger docs endpoint.

@UseGuards(RolesGuard)

Solution

  • I'd suggest you to use Interceptors if you want to stay in NestJS design

    Or you can use express or fastify authentication middleware in app bootstrap method, see this ticket for inspiration (old NestJS version)