NestJS JwtStrategy使用configService传递密钥

2023-11-26

我有文档示例中的 JwtStrategy 类(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,
        });
    }
    // ...
}

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

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

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


只需删除this., 看这里:

secretOrKey: configService.getSecretKey

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NestJS JwtStrategy使用configService传递密钥 的相关文章

随机推荐