Skip to content

Commit

Permalink
Merge pull request #130 from conceptadev/feature/jwt-exceptions
Browse files Browse the repository at this point in the history
fix: auth jwt exception
  • Loading branch information
MrMaz authored Oct 24, 2023
2 parents eaca7d8 + 8ff863e commit eb0f5ea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 6 additions & 2 deletions packages/nestjs-auth-jwt/src/decorator/auth-jwt.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ export class JwtAuthGuard extends AuthGuard(AUTH_JWT_STRATEGY_NAME) {
}

// handleRequest(err, user, info) {
handleRequest<T = ReferenceIdInterface>(err: Error | undefined, user: T) {
handleRequest<T = ReferenceIdInterface>(
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;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/nestjs-jwt/src/jwt.strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down

0 comments on commit eb0f5ea

Please sign in to comment.