Guard — 인증/인가
@Injectable()
export class JwtGuard implements CanActivate {
canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
const token = request.headers.authorization?.split(" ")[1];
if (!token) throw new UnauthorizedException();
request.user = this.jwtService.verify(token);
return true;
}
}
Interceptor — 로깅/변환
@Injectable()
export class LoggingInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler) {
const now = Date.now();
return next.handle().pipe(
tap(() => console.log(`${Date.now() - now}ms`))
);
}
}
댓글 0