From 8ff863ef11a0bebbd4b15cb6c38b7d5a1b678b4a Mon Sep 17 00:00:00 2001 From: Marshall Sorenson Date: Tue, 24 Oct 2023 13:11:17 -0400 Subject: [PATCH] fix: auth jwt exception --- packages/nestjs-auth-jwt/src/decorator/auth-jwt.guard.ts | 8 ++++++-- packages/nestjs-jwt/src/jwt.strategy.spec.ts | 5 ++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/nestjs-auth-jwt/src/decorator/auth-jwt.guard.ts b/packages/nestjs-auth-jwt/src/decorator/auth-jwt.guard.ts index a8aa75606..c9f84b1d7 100644 --- a/packages/nestjs-auth-jwt/src/decorator/auth-jwt.guard.ts +++ b/packages/nestjs-auth-jwt/src/decorator/auth-jwt.guard.ts @@ -18,10 +18,14 @@ export class JwtAuthGuard extends AuthGuard(AUTH_JWT_STRATEGY_NAME) { } // handleRequest(err, user, info) { - handleRequest(err: Error | undefined, user: T) { + handleRequest( + err: Error | undefined, + user: T, + info?: Error, + ) { // You can throw an exception based on either "info" or "err" arguments if (err || !user) { - throw err || new UnauthorizedException(); + throw new UnauthorizedException(null, { cause: err ?? info }); } return user; } diff --git a/packages/nestjs-jwt/src/jwt.strategy.spec.ts b/packages/nestjs-jwt/src/jwt.strategy.spec.ts index 55fa923f4..ff4f7ec74 100644 --- a/packages/nestjs-jwt/src/jwt.strategy.spec.ts +++ b/packages/nestjs-jwt/src/jwt.strategy.spec.ts @@ -53,13 +53,12 @@ describe(JwtStrategy, () => { }); it('should throw exception', async () => { - const t = async () => await jwtStrategy['verifyTokenCallback'](); + const t = async () => jwtStrategy['verifyTokenCallback'](); expect(t).rejects.toThrow(); }); it('should throw exception', async () => { - const t = async () => - await jwtStrategy['verifyTokenCallback'](new Error()); + const t = async () => jwtStrategy['verifyTokenCallback'](new Error()); expect(t).rejects.toThrow(); });