typescriptnestjspassport-jwt

NestJS JwtStrategy use configService to pass secret key


I have the JwtStrategy class from docs example (https://docs.nestjs.com/techniques/authentication):

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor(
        private readonly authService: AuthService,
        private readonly configService: ConfigService,
    ) {
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            secretOrKey: this.configService.getSecretKey,
        });
    }
    // ...
}

When I am trying access this before calling super() I get an error. But I still want to use configService to get secret key.

I know that I can use env var to do that, but service approach is more clearer solution, in my opinion.

How can I use configService or maybe get value from it and pass to super() call? Thanks.


Solution

  • Just remove this., see here:

    secretOrKey: configService.getSecretKey
    

    It will work since configService has been passed as a parameter.