- Access 토큰 만료 시 Refresh 토큰으로 Access 토큰 갱신 로직 구현 중
This commit is contained in:
@@ -1,5 +1,45 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
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') {}
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user