【发布时间】:2021-12-01 03:27:48
【问题描述】:
我在我的 NestJS 项目中使用 @nestjs/jwt。
我有两个模块,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]
这是为什么呢?我错过了哪里?
【问题讨论】:
标签: nestjs nestjs-passport nestjs-jwt