1. Nest can't resolve dependencies of the JwtStrategy (?, ConfigService).
에러 메시지를 보면 모듈간의 의존성 설정이 잘못돼서 에러가 생긴것 같았다. 의존성 주입은 제대로 된걸 몇번이나 확인했고 구글링을 해도 똑같은 말 밖에 안나와서 해결하는데 2시간은 쓴것 같은데, 의외의 곳에서 답을 찾았다.
JwtStrategy에 UserService가 필요한데, AuthModule의 import에 userModule을 넣어놓고, UserModule의 exports 부분에 UserService를 넣는걸 까먹은 것이었다...
채워넣기..
//user.module.ts
@Module({
imports: [
JwtModule.registerAsync({
useFactory: (config: ConfigService) => ({
secret: config.get<string>('JWT_SECRET_KEY'),
signOptions: { expiresIn: '12h' },
}),
inject: [ConfigService],
}),
TypeOrmModule.forFeature([User]),
],
exports: [TypeOrmModule, UserService], // 추가
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
//auth.module.ts
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt', session: false }),
JwtModule.registerAsync({
useFactory: (config: ConfigService) => ({
secret: config.get<string>('JWT_SECRET_KEY'),
}),
inject: [ConfigService],
}),
UserModule,
],
providers: [JwtStrategy],
})
export class AuthModule {}
2. In order to use "defaultStrategy", please, ensure to import PassportModule in each place where AuthGuard() is being used. Otherwise, passport won't work correctly.
서버는 열리는데, AuthGuard를 사용하는 API를 테스트할때마다 에러가 떴다. 터미널창을 다시 보니까, 서버를 여는 도중 한 구간에서 에러가 발생했는데, 모르고 지나쳤던것.. (api 실행 전이라 서버가 종료되진 않은듯)
AuthGuard를 사용하는 모듈에 PassportModule를 import해주니까 해결됐다.
//user.module.ts
@Module({
imports: [
JwtModule.registerAsync({
useFactory: (config: ConfigService) => ({
secret: config.get<string>('JWT_SECRET_KEY'),
signOptions: { expiresIn: '12h' },
}),
inject: [ConfigService],
}),
PassportModule.register({ defaultStrategy: 'jwt' }),
TypeOrmModule.forFeature([User]),
],
exports: [TypeOrmModule, UserService],
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
'TIL' 카테고리의 다른 글
TIL 240704 - UpdateValuesMissingError 에러해결 / Isolation Level 적용 (0) | 2024.07.04 |
---|---|
TIL 240703 - Guard, 커스텀 데코레이터 사용 (0) | 2024.07.03 |
TIL 240701 - javascript/typescript에서 날짜 한국표준시각으로 변경 (0) | 2024.07.01 |
TIL 240628 - Nest.js에서 Provider란? / useValue, useClass, useFactory 사용법 (0) | 2024.06.28 |
TIL 240627 - TypeORM 적용 (0) | 2024.06.27 |