![](/img/trans.png)
[英]NestJS can't resolve dependencies of the JWT_MODULE_OPTIONS (Same problem, different solution)
[英]NestJS can't resolve dependencies of the JWT_MODULE_OPTIONS
我无法编译此错误:
Nest 无法解析 JWT_MODULE_OPTIONS (?) 的依赖关系。 请确保索引 [0] 处的参数在 JwtModule 上下文中可用。 +52ms
我看到了模块和服务的类似依赖问题,但它们对我不起作用。 在我的auth.module.ts 中使用JwtModule :
import { JwtModule } from '@nestjs/jwt';
@Module({
imports: [
TypeOrmModule.forFeature([User, Role]),
ConfigModule,
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.registerAsync({
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
secretOrPrivateKey: config.jwtSecret,
type: configService.dbType as any,
host: configService.dbHost,
port: configService.dbPort,
username: configService.dbUsername,
password: configService.dbPassword,
database: configService.dbName,
entities: ['./src/data/entities/*.ts'],
signOptions: {
expiresIn: config.expiresIn,
},
}),
}),
],
providers: [AuthService, JwtStrategy],
controllers: [AuthController],
})
export class AuthModule { }
我不知道如何修复这个错误......使用jwt 6.1.1
编辑:在我之前的项目中使用 jwt 6.0.0,所以我降级了它,但问题没有解决。
首先,您将 TypeORMModule 配置与 JWTModule 配置混合在一起。
根据@nestjs/jwt
源代码(和文档), secretOrPrivateKey
和signOptions
。 所有其他参数似乎都是 TypeORMModule 配置的一部分。
其次,ConfigService(它是 JWT 模块的依赖项 [0])似乎并不存在于您的代码中的任何地方。 因此,您缺少对内部存在 ConfigService 的模块的导入。
这就是依赖加载失败的原因(这就是错误抛出的意思)
请注意,在您的代码中,您缺少一个模块(以下示例中的ConfigModule
)的导入,该模块是保存 ConfigService 的模块。 否则就没有办法从任何地方注入这个 ConfigService!
JwtModule.registerAsync({
imports: [ConfigModule], // Missing this
useFactory: async (configService: ConfigService) => ({
signOptions: {
expiresIn: config.expiresIn,
},
secretOrPrivateKey: config.jwtSecret,
}),
inject: [ConfigService],
}),
我以某种方式通过添加使其工作
JwtModule.registerAsync({
imports: [ConfigModule], // Missing this
useFactory: async (configService: ConfigService) => ({
signOptions: {
expiresIn: config.expiresIn,
},
secretOrPrivateKey: config.jwtSecret,
}),
inject: [ConfigService],
}),
在app.module.ts和auth.module.ts
你可以为它制作一个单独的模块(SharedModule)
确保您安装了以下软件包
npm i --save @nestjs/jwt
npm i --save @nestjs/passport
(可选,如果您使用 MongoDB/Mongoose)
npm i --save @nestjs/mongoose
共享模块.ts
@NgModule({
imports: [
PassportModule.register({
defaultStrategy: 'jwt',
}),
JwtModule.register({
secret: process.env.JWT_SECRET_KEY,
signOptions: {
expiresIn: '2 days',
},
}),
],
providers: [JwtStrategy],
exports: [JwtStrategy, PassportModule]
})
jwt.strategy.ts
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(@InjectModel('User') private collection: Model<User>) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_SECRET_KEY,
});
}
async validate(payload: JwtPayload): Promise<User> {
const { username } = payload;
const user = await this.collection.findOne({ username });
if (!user) {
throw new UnauthorizedException('JwtStrategy unauthorized');
}
return user;
}
}
现在你想使用它,只需在你的模块 SharedModule 中导入。 在您的控制器中使用以下装饰器
@UseGuards(AuthGuard())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.