From 6fa0a7a3ac2c14ea5b5cffe3f53a49e7189b8eeb Mon Sep 17 00:00:00 2001 From: Tom Waller Date: Mon, 12 Aug 2024 21:54:59 +0000 Subject: [PATCH] feat: bearer authroization on swagger ui --- src/app.module.ts | 18 ++++++++++++++++++ src/auth/auth.service.ts | 6 +++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/app.module.ts b/src/app.module.ts index 38d5f44..cc00b7c 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -32,8 +32,26 @@ export const setupApp = () => { swagger({ documentation: { info: { title, version, description }, + components: { + securitySchemes: { + bearerAuth: { + type: 'http', + description: 'Bearer token to access these api endpoints', + scheme: 'bearer', + bearerFormat: 'JWT', + }, + }, + }, + security: [ + { + bearerAuth: [], + }, + ], }, exclude: ['/'], + swaggerOptions: { + persistAuthorization: true, + }, }), ) .group('/api', (app) => app.use(usersPlugin).use(profilesPlugin)); diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index fabef3e..367546b 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -1,6 +1,6 @@ import * as jose from 'jose'; import { env } from '@config'; -import { UserInDb } from '@users/users.schema'; +import type { UserInDb } from '@users/users.schema'; import { Type } from '@sinclair/typebox'; import { Value } from '@sinclair/typebox/value'; import { AuthenticationError } from '@errors'; @@ -69,9 +69,9 @@ export class AuthService { const tokenParts = rawHeader?.split(' '); const tokenType = tokenParts?.[0]; - if (tokenType !== 'Token') + if (tokenType !== 'Bearer') throw new AuthenticationError( - "Invalid token type. Expected header format: 'Token jwt'", + "Invalid token type. Expected header format: 'Bearer jwt'", ); const token = tokenParts?.[1];