3

我有来自文档示例(https://docs.nestjs.com/techniques/authentication)的 JwtStrategy 类:

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

当我this在调用 super() 之前尝试访问时,出现错误。但我仍然想使用 configService 来获取密钥。

我知道我可以使用 env var 来做到这一点,但在我看来,服务方法是更清晰的解决方案。

我如何使用 configService 或者从中获取价值并传递给 super() 调用?谢谢。

4

1 回答 1

11

只需删除this.,请参见此处:

secretOrKey: configService.getSecretKey

configService由于已作为参数传递,因此它将起作用。

于 2018-06-22T07:41:19.340 回答