Expo AuthSession 立即解析为 "dismiss"ed

kac*_*tel 6 auth0 react-native react-native-android expo

我正在使用 Expo 的AuthSession模块登录 Auth0:

    let result = await AuthSession.startAsync({
      authUrl: `${auth0Domain}/authorize?` + qs.stringify({
        client_id: auth0ClientId,
        response_type: 'code',
        scope: 'openid profile email offline_access',
        redirect_uri: redirectUrl,
        code_challenge_method: 'S256',
        code_verifier: codeVerifier,
        state: oAuthState,
        audience: auth0Audience
      })
    })

    if (result.type === 'success') {
      ...
    } else if (
      result.type === 'dismiss' ||
      (result.type === 'error' && result.errorCode === 'login-declined')
    ) {
      // FIXME: alert never pops without delay here, despite the if correctly evaluating true
      // Any way to check if the window has closed, if that's the issue?
      await new Promise((resolve) => setTimeout(resolve, 5000))

      let retry = await alertAsync(
        'Authentication dismissed',
        'Cancel signing in?',
        'Try again',
        'Exit'
      )
      return retry ? this.loginWithAuth0() : false
    }
    logger.error('Login failed', result)
    throw new Error('Error signing in')
  }
Run Code Online (Sandbox Code Playgroud)

在开发环境中,我从来没有遇到过问题,但在已发布的应用程序中,promise 通常会在浏览器关闭之前立即通过{ type: dismiss },解决。甚至在 AuthSession 窗口打开之前,我瞥见了一个警报(我尝试弹出,以防会话实际上被解除)

如果我在弹出错误之前添加 5 秒延迟,它会在开始AuthSession而不是完成后显示 5 秒。

在安卓上测试。

我研究了迁移到 WebBrowser,但它似乎是相同的实现。

是否有一种一致的方式来实际await完成 Auth 过程,还是我应该放弃这整个事情并编写自己的登录屏幕?

提前感谢您的任何输入/方向!

Luc*_*llo 5

我已经详细解释了问题和可能的解决方案https://github.com/expo/expo/issues/6679#issuecomment-570963717

作为临时解决方法,您可以在应用程序中的某个位置调用 AppState.currentState,也许只是为了在用户单击并启用 AuthSession 之前添加正确的侦听器而记录它(在链接的 GitHub 问题中了解更多信息)。

请看一下并发表评论:)