본문 바로가기
Node.js/NestJs

[User] Authentication #5 AuthUser Decorator

by Ykie 2023. 4. 8.
728x90

login이 되어있지 않다면 request를 멈추게 함.

 

Decorator를 만들어보자~

// auth-user.decorator.ts
// custom decorator

export const AuthUser = createParamDecorator(
  (data: unknown, ctx: ExecutionContext) => {
    const gqlContext = GqlExecutionContext.create(ctx).getContext();
    return gqlContext['user'];
  },
);


// user.resolver.ts
...
  @Query((returns) => User)
  @UseGuards(AuthGuard)
  me(@AuthUser() authUser: User) {
    return authUser;
  }
 ...

Authentication은 향후 더 디벨롭 시킬 예정.

728x90

'Node.js > NestJs' 카테고리의 다른 글

Email Verification  (0) 2023.04.22
[User] update Profile  (0) 2023.04.14
[User] Authentication #4 Guard  (0) 2023.04.08
[User] Authentication #3 JWT Middleware  (0) 2023.04.08
[User] Authentication #2 JWT Module  (0) 2023.04.07

댓글