Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expo AuthSession immediately resolves as "dismiss"ed

I'm using Expo's AuthSession module to sign into 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')
  }

In a dev environment I never have an issue, but in a published app the promise often resolves immediately with { type: dismiss }, before the browser closes. Once I caught a glimpse of an alert (that I try to popup in case the session is actually dismissed) even before the AuthSession window opened.

If I add a 5s delay before popping the error up, it does show up 5s after starting the AuthSession rather than it finishing.

Testing on Android.

I looked into migration over to WebBrowser but it seems to be the same implementation.

Is there a consistent way to actually await the Auth process finishing, or should I ditch this whole thing and write my own login screen?

Thanks in advance for any input / direction!

like image 625
kachnitel Avatar asked Jan 04 '20 08:01

kachnitel


1 Answers

I've put a detailed explanation of the problem and a possible solution https://github.com/expo/expo/issues/6679#issuecomment-570963717

As a temporary workaround, you can call AppState.currentState somewhere in your app, maybe logging it just to add the right listeners before the user clicks and enable the AuthSession (read more in the linked GitHub issue).

Have a look and comment please :)

like image 140
Luca Colonnello Avatar answered Nov 12 '22 23:11

Luca Colonnello