Skip to content

Instantly share code, notes, and snippets.

@tbhaxor
Created March 9, 2025 07:28
Show Gist options
  • Save tbhaxor/86e0909c50d93cc174da5e201f4b717a to your computer and use it in GitHub Desktop.
Save tbhaxor/86e0909c50d93cc174da5e201f4b717a to your computer and use it in GitHub Desktop.
Nestjs swagger bearerAuth error
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { ValidationPipe } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
app.useGlobalPipes(new ValidationPipe());
const configService = app.get<ConfigService>(ConfigService);
const packageJsonPath = configService.get<string>('PACKAGE_JSON_PATH', joinPath(__dirname, '../package.json'));
const pkg = await readFile(packageJsonPath, { encoding: 'utf-8' }).then((content) => JSON.parse(content));
const appUrl = configService.getOrThrow<string>('APP_URL');
const document = new DocumentBuilder()
.setTitle('Backend Service')
.setVersion(pkg.version)
.addServer(appUrl)
.addTag('Authentication', 'Create and manage the authentication sessions')
.addBearerAuth({
name: 'firebaseIdToken',
type: 'http',
bearerFormat: 'jwt',
description: 'Firebase user ID token from the login session to authenticate the requests.',
scheme: 'bearer',
})
.build();
SwaggerModule.setup('/docs', app, () => SwaggerModule.createDocument(app, document));
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment