<NestJS> TypeError: metatype is not a constructor 오류 해결

TaeWoo·2024년 11월 6일

nodejs

목록 보기
12/12
post-thumbnail

NestJS 프로젝트를 진행하다 보면 인증 관련 오류로 TypeError: metatype is not a constructor 오류를 만나는 경우가 있습니다.

이 오류는 대부분 @UseGuards(AuthGuard) 데코레이터 사용 시 발생하는데, 간단한 수정으로 해결할 수 있습니다.


  1. AuthGuard 데코레이터 수정하기
    @UseGuards(AuthGuard)를 사용할 때 오류가 발생하는 경우, AuthGuard를 함수로 호출하여 사용해야 합니다.
@UseGuards(AuthGuard()) 함수 형태로 수정.

이처럼 AuthGuard() 형태로 함수 호출을 추가해 주면 metatype is not a constructor 오류를 해결할 수 있습니다.


위 수정으로 metatype 오류가 해결된 후에도, 새로운 오류가 발생할 수 있습니다.

ERROR [AuthGuard] In order to use "defaultStrategy", please, ensure to import PassportModule in each place where AuthGuard() is being used. Otherwise, passport won't work correctly.

이는 PassportModule의 기본 전략이 설정되지 않았을 때 발생하는 오류입니다. 이 문제를 해결하기 위해 PassportModule을 불러오고, defaultStrategy를 jwt로 지정해주어야 합니다.


 PassportModule.register({ defaultStrategy: 'jwt' }),

이렇게 설정하면, 패스포트가 jwt 전략을 기본으로 사용하여 토큰 기반 인증을 처리할 수 있게 됩니다.

  1. @UseGuards(AuthGuard)를 @UseGuards(AuthGuard()) 형태로 수정하여 TypeError: metatype is not a constructor 오류를 해결합니다.
  2. PassportModule을 import하고 defaultStrategy를 설정하여, 인증 모듈이 JWT 전략을 기본으로 사용하도록 합니다.
profile
코드와 아이디어의 소통, 기록하는 개발자. 🚀✨

0개의 댓글