0

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?

Sancho
  • 1,288
  • 2
  • 25
  • 50

1 Answers1

2

Missed the @Injectable() in JwtStrategy

Sancho
  • 1,288
  • 2
  • 25
  • 50