@nestjs/jwt
NestJS プロジェクトで使用しています。
私は2つのモジュールを持っていAuthModule
ますAppModule
.
- は
AuthModule
、@nestjs/jwt
- からの
AppModule
呼び出し認証サービスAuthModule
。
AuthService
:
import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { ForbiddenException } from '@nestjs/common';
@Injectable()
export class AuthService {
constructor(private readonly jwt: JwtService) {}
async validate(token: string) {
return this.jwt.verify(token);
}
...
}
認証モジュール:
import { Module } from '@nestjs/common'
import { TokenService } from './auth.service'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { JwtModule } from '@nestjs/jwt'
@Module({
imports: [
JwtModule.register({
secret: "mysecret",
signOptions: { expiresIn: "60s" },
}),
],
// I provide AuthService & JwtService
providers: [AuthService, JwtService],
// I export AuthService and JwtService
exports: [AuthService, JwtService],
})
export class AuthModule {}
アプリモジュール:
@Module({
imports: [
AuthModule,
...
],
controllers: [AppController],
// I provide AuthService & JwtService also in AppModule
providers: [AppService, JwtService],
})
export class AppModule {}
(トークンを検証するためにインスタンスをAppController
呼び出します。)AuthService
私は常にエラーが発生します:
Nest can't resolve dependencies of the JwtService (?). Please make sure that the argument JWT_MODULE_OPTIONS at index [0]
何故ですか?どこが恋しいですか?