6

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.

Vladyslav Moisieienkov
  • 4,118
  • 4
  • 25
  • 32

1 Answers1

18

Just remove this., see here:

secretOrKey: configService.getSecretKey

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

Kamil Myśliwiec
  • 8,548
  • 2
  • 34
  • 33
  • 4
    To avoid "Property 'configService' is declared but its value is never read." I also had to drop the `private` from `private readonly configService: ConfigService`. – Steve Oct 06 '20 at 16:05
  • 1
    I can confirm, you cannot have `private` keyword and use that property in the `super()` call within `constructor` – andrewkiri Dec 15 '21 at 22:19
  • 2
    To add on the above answer and comment, replace `private` with `protected` – wongz Mar 31 '22 at 03:12
  • 1
    But nothing said here helped to me. I have the same code. – A.Anvarbekov Feb 03 '23 at 15:02