All checks were successful
Test CI / build (push) Successful in 1m29s
- Access 토큰 만료 시 Refresh 토큰으로 Access 토큰 갱신 로직 구현 중
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import { ExecutionContext, Injectable, UnauthorizedException } from "@nestjs/common";
|
|
import { Reflector } from "@nestjs/core";
|
|
import { TokenExpiredError } from "@nestjs/jwt";
|
|
import { AuthGuard } from "@nestjs/passport";
|
|
import { IS_PUBLIC_KEY } from "src/common/decorators/public.decorator";
|
|
|
|
@Injectable()
|
|
export class JwtRefreshAuthGuard extends AuthGuard('refresh-token') {
|
|
constructor(private reflector: Reflector) {
|
|
super();
|
|
}
|
|
|
|
canActivate(context: ExecutionContext) {
|
|
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
|
context.getHandler(),
|
|
context.getClass()
|
|
]);
|
|
|
|
if (isPublic) {
|
|
return true;
|
|
}
|
|
|
|
return super.canActivate(context);
|
|
}
|
|
|
|
handleRequest(err: any, user:any, info:any) {
|
|
if (info instanceof TokenExpiredError) {
|
|
throw new UnauthorizedException({
|
|
statusCode: 401,
|
|
message: 'Refresh Token Expired',
|
|
code: 'RefreshTokenExpired'
|
|
});
|
|
}
|
|
|
|
if (err || !user) {
|
|
throw new UnauthorizedException({
|
|
statusCode: 401,
|
|
message: 'Invalid Token',
|
|
code: 'InvalidToken'
|
|
});
|
|
}
|
|
|
|
return user;
|
|
}
|
|
} |