Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- angular modal
- 스크롤 이벤트 감지
- scroll
- angular button
- Angular Router
- 옵저버블
- 셀렉트박스 커스텀
- egov spring ajax 사용 예시
- ApexChart
- flex-1
- prisma
- route
- angular animation
- modal
- Router
- 스크롤 이벤트
- 호버
- 앵귤러 애니메이션
- mysql if
- 검색
- TAILWIND
- 모달
- Oracle LISTAGG 사용 예시
- formgroup
- summary
- ajax 사용 예시
- angular route
- 아이오닉 스크롤 이벤트
- Ionic modal
- 앵귤러 모달
Archives
- Today
- Total
깜놀하는 해므찌로
Angular NX Interceptor 활용 예시 / JWT Bearer 본문
반응형
SMALL
1. Angular 에서 일반적으로 interceptor 는 프론트엔드단에서 관리합니다.
2. pages 폴더와 같은 경로에 interceptor 폴더 생성 및 이름은 상황에 맞게 설정합니다.
import {
CanActivate,
ExecutionContext,
HttpException,
Injectable,
UnauthorizedException,
Logger,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { PrismaService } from '../../../prisma/prisma.service';
import { AuthUtil } from '../auth-util.service';
import { AccessTokenType } from '@namdorang/interface';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(
private readonly authUtil: AuthUtil,
private readonly prismaService: PrismaService
) {}
canActivate(
context: ExecutionContext
): boolean | Promise<boolean> | Observable<boolean> {
const request = context.switchToHttp().getRequest(); // Http 요청을 중간에 가로 채기
// 인증 정보 확인을 위해 변수에 할당
const header = request.headers;
const authorization = header.authorization; // authoriztion 이 없음!! 왜?
Logger.log(header);
// 국제 표준 형식 : Authorization: <type> <credentials>
if (!authorization) {
throw new UnauthorizedException('사용자 정보를 찾을 수 없습니다.');
}
// <type> 타입 이름 => Bearer
if (!authorization.startsWith('Bearer ')) {
throw new UnauthorizedException('사용자 정보를 찾을 수 없습니다.');
}
// <credentials> => random string
const token = authorization.split(' ')[1];
if (!token) {
throw new UnauthorizedException('사용자 정보를 찾을 수 없습니다.');
}
let id;
try {
const payload = this.authUtil.verifyToken<AccessTokenType>(token);
if (!payload.id) {
throw new UnauthorizedException('유효하지 않은 토큰입니다.');
}
id = payload.id;
} catch (e) {
throw new HttpException('사용할 수 없는 토큰입니다.', 498);
}
const admin = this.prismaService.admin.findUnique({ where: { id } });
if (!admin) {
throw new UnauthorizedException('유효하지 않은 토큰입니다.');
}
request.user = admin; // http header 에 인증 정보를 담기
return true;
}
}
3. interceptor 내부 코드 모습입니다
4. jwt bearer 토큰을 활용하였습니다.
5. 자세한 설명은 주석을 참고해주세요.
import { ApplicationConfig, importProvidersFrom } from '@angular/core';
import {
provideRouter,
withEnabledBlockingInitialNavigation,
} from '@angular/router';
import { appRoutes } from './app.routes';
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import { HttpInterceptorImpl } from './interceptors/http-interceptor'; // interceptor
import { ToastService } from '../../../common/src';
import { NgxsModule } from '@ngxs/store'; // store
import { AdminStore } from './store/auth.store'; // store
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(appRoutes, withEnabledBlockingInitialNavigation()),
importProvidersFrom(HttpClientModule, NgxsModule.forRoot([AdminStore])),
{ provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorImpl, multi: true },
ToastService,
],
};
6. injectable 을 사용하였으니 프로젝트 app.config 에 설정을 추가합니다.
반응형
LIST
'IT' 카테고리의 다른 글
Nestjs 비밀번호 / 비밀번호 확인과 같이 클래스 내 다른 property와 일치한 지 검증하고 싶을 때 (0) | 2023.08.13 |
---|---|
Nx nest Angular Guard 활용 예시 JWT Bearer (0) | 2023.08.11 |
@Prisma Client 활용 예시 / type (0) | 2023.08.10 |
Nx Angular Lib 생성 및 활용 (0) | 2023.08.09 |
StringMap 활용 간략 예시 (0) | 2023.08.08 |