【问题标题】:How to pass state during Nest.js Authentication flow如何在 Nest.js 身份验证流程中传递状态
【发布时间】:2020-07-06 12:18:23
【问题描述】:

在执行 Google OAuth 流程时,可以传递加密状态 (base64),该状态将作为参数传递给最终回调。例如,当您要将用户重定向到特定页面时,这很有用。 (https://developers.google.com/identity/protocols/oauth2/web-server)

是否可以将 OAuth 状态与 Nest.js 身份验证库一起使用?似乎 state 参数被忽略了,我在文档上找不到任何东西。

@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
  constructor(readonly configService: ConfigService) {
    super({
      clientID: configService.get('google.clientId'),
      clientSecret: configService.get('google.clientSecret'),
      callbackURL: `${configService.get('apiUri')}${configService.get('google.callbackUrl')}`,
      passReqToCallback: true,
      scope: ['profile', 'email'],
    });
  }
}

【问题讨论】:

    标签: oauth passport.js nestjs


    【解决方案1】:

    为了解决这个问题,我在设置状态值的类中添加了一个authenticate 函数。

    authenticate(req, options) {
      options.state = 'your state value here'
      super.authenticate(req, options)
    }
    

    免责声明:我试图实现与您描述的类似的东西,这种方法对我有用,但我不确定它是否是处理这个问题的“正确”方法。

    【讨论】:

    • 感谢您的回答!我会等待 NestJS 的真正解决方案,但这项工作是我们最终要做的。
    猜你喜欢
    • 2011-06-09
    • 2020-11-15
    • 1970-01-01
    • 2012-03-04
    • 2012-01-25
    • 2017-09-09
    • 1970-01-01
    • 2019-11-29
    • 2011-11-12
    相关资源
    最近更新 更多