在子模块中注入configservice

发布于 2025-02-11 01:49:06 字数 1741 浏览 0 评论 0原文

我是Nestjs的新手,并试图理解模式。

我配置了配置模块以在特定文件夹中使用配置设置。我想在子模块中使用该configmodule,但它不起作用。

我有一个app.module

app.module.ts @模块({ 进口:[[ configmodule.forroot({ envfilepath:['.env.local','.env'], iSglobal:是的, ExpandVariables:是的, 负载:[dbConfiguration,jwtconfiguration], }), databasemodule, authmodule, 用户模块, ],, 控制器:[AppController], 提供者:[], 导出:[configmodule] })) 导出类AppModule { }

auth.module.ts

@Module({
    imports: [
        UsersModule,
        PassportModule,
        ConfigModule,
        JwtModule.registerAsync({
            imports: [ConfigModule],
            useFactory: async (configService: ConfigService) => ({
                secretOrPrivateKey: configService.get('jwt').secret,
                signOptions: {
                    expiresIn: 3600,
                },
            }),
            inject: [ConfigService],
        }),

    ],
    providers: [AuthService, JwtStrategy],
    exports: [AuthService]
})
export class AuthModule {}

jwt.strategy.ts

import { PassportStrategy } from "@nestjs/passport";
import { ExtractJwt, Strategy } from "passport-jwt";
import {ConfigService} from "@nestjs/config";

export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor(configService: ConfigService) {
        console.log(configService);
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            ignoreExpiration: false,
            secretOrKey: configService.get('jwt').secret,
        })
    }

    async validate(payload: any) {
        return {userId: payload.sub, email: payload.email}
    }
}

在构造函数中,configService不确定。

我在包含JWTSTRATEGY的Auth.Module中导入ConfigModule。

我错过了什么?

I'm new with NestJS and trying to understand the patterns.

I configure ConfigModule to use config set in a specific folder. I want to use that ConfigModule in a submodule but It doesn't work.

I have an app.module

app.module.ts
@Module({
imports: [
ConfigModule.forRoot({
envFilePath: ['.env.local', '.env'],
isGlobal: true,
expandVariables: true,
load: [dbConfiguration, jwtConfiguration],
}),
DatabaseModule,
AuthModule,
UsersModule,
],
controllers: [AppController],
providers: [],
exports: [ConfigModule]
})
export class AppModule {
}

auth.module.ts

@Module({
    imports: [
        UsersModule,
        PassportModule,
        ConfigModule,
        JwtModule.registerAsync({
            imports: [ConfigModule],
            useFactory: async (configService: ConfigService) => ({
                secretOrPrivateKey: configService.get('jwt').secret,
                signOptions: {
                    expiresIn: 3600,
                },
            }),
            inject: [ConfigService],
        }),

    ],
    providers: [AuthService, JwtStrategy],
    exports: [AuthService]
})
export class AuthModule {}

jwt.strategy.ts

import { PassportStrategy } from "@nestjs/passport";
import { ExtractJwt, Strategy } from "passport-jwt";
import {ConfigService} from "@nestjs/config";

export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor(configService: ConfigService) {
        console.log(configService);
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            ignoreExpiration: false,
            secretOrKey: configService.get('jwt').secret,
        })
    }

    async validate(payload: any) {
        return {userId: payload.sub, email: payload.email}
    }
}

In the constructor, configService is undefined.

I import ConfigModule in auth.module that contain JwtStrategy.

What did I missed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2025-02-18 01:49:06

错过了jwtstrategy中的@injectable()

Missed the @Injectable() in JwtStrategy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文