Skip to content

Commit

Permalink
Merge pull request #69 from VentionCapstone/fix/158_fix-access-token-…
Browse files Browse the repository at this point in the history
…error

Fix/158 fix access token error
  • Loading branch information
Zach13131 committed Feb 9, 2024
2 parents 5a1edc7 + baa89d1 commit 032fd61
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/common/guards/admin.guard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService, TokenExpiredError } from '@nestjs/jwt';
import { JsonWebTokenError, JwtService, TokenExpiredError } from '@nestjs/jwt';
import ErrorsTypes from 'src/errors/errors.enum';
import { GlobalException } from 'src/exceptions/global.exception';
import { User } from '../../auth/entities/auth.entity';
Expand Down Expand Up @@ -46,8 +46,10 @@ export class AdminGuard implements CanActivate {
} catch (error) {
if (error instanceof TokenExpiredError)
throw new UnauthorizedException(ErrorsTypes.UNAUTHORIZED_AUTH_EXPIRED_ACCESS_TOKEN);
if (error instanceof JsonWebTokenError)
throw new UnauthorizedException(ErrorsTypes.AUTH_FAILED_TOKEN_VERIFY);
if (error instanceof UnauthorizedException) throw error;
throw new GlobalException(ErrorsTypes.AUTH_FAILED_TOKEN_VERIFY);
throw new GlobalException(ErrorsTypes.AUTH_FAILED_TOKEN_VERIFY, error.message);
}
}
}
6 changes: 4 additions & 2 deletions src/common/guards/user.guard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService, TokenExpiredError } from '@nestjs/jwt';
import { JsonWebTokenError, JwtService, TokenExpiredError } from '@nestjs/jwt';
import ErrorsTypes from 'src/errors/errors.enum';
import { GlobalException } from 'src/exceptions/global.exception';
import { User } from '../../auth/entities/auth.entity';
Expand Down Expand Up @@ -44,8 +44,10 @@ export class UserGuard implements CanActivate {
} catch (error) {
if (error instanceof TokenExpiredError)
throw new UnauthorizedException(ErrorsTypes.UNAUTHORIZED_AUTH_EXPIRED_ACCESS_TOKEN);
if (error instanceof JsonWebTokenError)
throw new UnauthorizedException(ErrorsTypes.AUTH_FAILED_TOKEN_VERIFY);
if (error instanceof UnauthorizedException) throw error;
throw new GlobalException(ErrorsTypes.AUTH_FAILED_TOKEN_VERIFY);
throw new GlobalException(ErrorsTypes.AUTH_FAILED_TOKEN_VERIFY, error.message);
}
}
}

0 comments on commit 032fd61

Please sign in to comment.