NestJS JwtStrategy use configService to pass secret key

爱⌒轻易说出口 提交于 2021-02-04 16:08:45

问题


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.


回答1:


Just remove this., see here:

secretOrKey: configService.getSecretKey

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



来源:https://stackoverflow.com/questions/50977202/nestjs-jwtstrategy-use-configservice-to-pass-secret-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!