【问题标题】:How set more "jwt" AuthGuard in nestjs?如何在nestjs中设置更多“jwt”AuthGuard?
【发布时间】:2021-09-03 00:45:39
【问题描述】:

我是nestjs的新手,我已经用他的相对AuthGuard设置了一个“jwt” PassportStrategy,现在我必须创建一个完全不同的“jwt” PassportStrategy 和他的相对AuthGuard,我该怎么做?你有例子吗?

【问题讨论】:

    标签: jwt passport.js nestjs passport-jwt


    【解决方案1】:

    首先,您创建另一个策略类。 扩展基本 PassportStrategy 时的第二个参数将是您可以与 AuthGuard 一起使用的策略的名称,以指定哪个 它将使用的策略。

    export class OtherStrategy extends PassportStrategy(Strategy, 'other-strategy')
    { STRATEGY IMPLEMENTATION }
    

    然后你可以在你的控制器中使用它

    @UseGuards(AuthGuard('other-strategy')
    @Post('/my-endpoint')
    

    【讨论】:

    • 感谢您的回复!我正在测试这个解决方案,但它给了我这个错误:“未知身份验证策略“jwt-temporary”有什么建议吗?
    • 哦,忘了说您应该在必要时在模块中将策略注册为提供者,并再次确保在扩展 PassportStrategy 和将其与 AuthGuard 一起使用时使用相同的名称。
    猜你喜欢
    • 2019-01-26
    • 2019-09-28
    • 1970-01-01
    • 2022-08-12
    • 2020-03-26
    • 1970-01-01
    • 2019-06-12
    • 2021-10-22
    • 2019-03-27
    相关资源
    最近更新 更多