From b558d344e1d7c04ed18267dd1c6c80855a0ebbc7 Mon Sep 17 00:00:00 2001 From: Mathis Date: Wed, 24 Jul 2024 20:26:53 +0200 Subject: [PATCH] fix(credentials): handle token verification errors Added try-catch block in verifyAuthToken to handle and log JWT verification errors. This change ensures that invalid tokens are caught and a relevant BadRequestException is thrown. --- src/credentials/credentials.service.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/credentials/credentials.service.ts b/src/credentials/credentials.service.ts index 09a5ca8..d9acaff 100644 --- a/src/credentials/credentials.service.ts +++ b/src/credentials/credentials.service.ts @@ -22,15 +22,23 @@ export class CredentialsService { secret: Buffer.from(this.configService.get("APP_HASH_SECRET")), }); } + async verifyAuthToken(token: string) { - return await jose.jwtVerify( - token, - Uint8Array.from(this.configService.get("APP_TOKEN_SECRET")), - { - audience: "auth:user", - issuer: "ShouldStick", - }, - ); + try { + const result = await jose.jwtVerify( + token, + Uint8Array.from(this.configService.get("APP_TOKEN_SECRET")), + { + audience: "auth:user", + issuer: "ShouldStick", + }, + ); + console.log(result); + return result; + } catch (error) { + console.log(error) + throw new BadRequestException("Invalid token"); + } } async signAuthToken(payload: JWTPayload) {