如何在Nest.js身份验证流程中传递状态

时间:2020-03-25 21:19:19

标签: oauth passport.js nestjs

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

是否可以在Nest.js身份验证库中使用OAuth状态?似乎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'],
    });
  }
}

1 个答案:

答案 0 :(得分:2)

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

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

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