From 1215803a40400f54a908ee6c2747e244759197b2 Mon Sep 17 00:00:00 2001 From: andiz2 Date: Fri, 9 May 2025 11:48:43 +0300 Subject: [PATCH] Bugfix/fix ApiKeyStrategy error (#4682) * Fix ApiKeyStrategy error --- apps/api/src/app/auth/api-key.strategy.ts | 45 ++++++++++------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/apps/api/src/app/auth/api-key.strategy.ts b/apps/api/src/app/auth/api-key.strategy.ts index e1e067ab..e99d6aed 100644 --- a/apps/api/src/app/auth/api-key.strategy.ts +++ b/apps/api/src/app/auth/api-key.strategy.ts @@ -21,38 +21,31 @@ export class ApiKeyStrategy extends PassportStrategy( private readonly prismaService: PrismaService, private readonly userService: UserService ) { - super({ header: HEADER_KEY_TOKEN, prefix: 'Api-Key ' }, true); + super({ header: HEADER_KEY_TOKEN, prefix: 'Api-Key ' }, false); } - public async validate( - apiKey: string, - done: (error: any, user?: any) => void - ) { - try { - const user = await this.validateApiKey(apiKey); + public async validate(apiKey: string) { + const user = await this.validateApiKey(apiKey); - if (this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) { - if (hasRole(user, 'INACTIVE')) { - throw new HttpException( - getReasonPhrase(StatusCodes.TOO_MANY_REQUESTS), - StatusCodes.TOO_MANY_REQUESTS - ); - } - - await this.prismaService.analytics.upsert({ - create: { User: { connect: { id: user.id } } }, - update: { - activityCount: { increment: 1 }, - lastRequestAt: new Date() - }, - where: { userId: user.id } - }); + if (this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) { + if (hasRole(user, 'INACTIVE')) { + throw new HttpException( + getReasonPhrase(StatusCodes.TOO_MANY_REQUESTS), + StatusCodes.TOO_MANY_REQUESTS + ); } - done(null, user); - } catch (error) { - done(error, null); + await this.prismaService.analytics.upsert({ + create: { User: { connect: { id: user.id } } }, + update: { + activityCount: { increment: 1 }, + lastRequestAt: new Date() + }, + where: { userId: user.id } + }); } + + return user; } private async validateApiKey(apiKey: string) {