无法从 Web 视图中恢复控制以在 OAuth 流程中对本机应用程序做出反应

Rak*_*ngh 5 oauth-2.0 react-native appauth react-native-android

我正在尝试使用 FormidableLab 的react-native-app-auth库在我的 react native android 应用程序中实现 Google OAuth 2 登录,如下所示:

  googleLoginPressed = async () => {
    const config = {
      serviceConfiguration: {
        authorizationEndpoint: 'https://accounts.google.com/o/oauth2/v2/auth', tokenEndpoint: 'https://accounts.google.com/o/oauth2/v2/auth', 
      },
      clientId: app_params.GOOGLE_CLIENT_ID, redirectUrl: 'https://<my_domain>/oauth/google',
      scopes: ['openid', 'profile', 'email'], additionalParameters: { 'response-type': 'code' },
    };
    try {
      const result = await authorize(config);
    } catch (error) {
      console.log(error);
    }
  }
Run Code Online (Sandbox Code Playgroud)

这会使用 Google 的登录页面调用 Web 视图,我可以成功验证自己的身份。然后 Google 正确地重定向到我的 oauth 回调端点,并code像它应该的那样在重定向 url 中填充 oauth 。在这一点上,我希望react-native-app-auth将控制权从 webview 恢复到应用程序。相反,Web 视图在重定向 url 页面上保持打开状态。

我在下面添加了必要的网站关联配置AndroidManifest.xml和下面的代码,MainActivity.java以检查是否从重定向 url 将控制权返回给应用程序:

  @Override
  public void onNewIntent(Intent intent) { // this is not getting hit
    super.onNewIntent(intent);
    Uri appLinkData = intent.getData();

    if (appLinkData != null) {
      bar = appLinkData.getPath();
    }
  }
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试过的

  • 我确保我的应用程序可以打开Universal links。所以网站关联一定能正常工作。
  • 还尝试在 iOS 上复制整个设置。结果一样。webview 显示 Google 正确重定向到 oauth 端点,但应用程序无法重新获得控制权。

如何将控制权从 oauth Web 视图返回到我的 react-native 代码?

Gar*_*her 0

如果您使用声明的 HTTPS 方案(这是移动应用程序最推荐的安全选项),则您可能还需要登录后的插页式页面,以触发通用链接。

我的博客文章提供了更多信息和您可以运行的代码示例,尽管它使用普通的 Kotlin 而不是 React Native。