Skip to content

signInWithWebUI with SAML returns isSignedIn: true immediately but userPoolTokensResult is not yet available (race condition) #6829

@nguyenhuuloc99

Description

@nguyenhuuloc99

Description

When calling Amplify.Auth.signInWithWebUI with a SAML provider, the method completes and returns result.isSignedIn == true immediately after the Cognito Hosted UI redirects back to the app via deep link.
However, calling fetchAuthSession() (even with forceRefresh: true) right after that often fails to return tokens. userPoolTokensResult.hasValue is false or accessing .value throws an exception.
The tokens only become available after a short period because the authorization code exchange happens asynchronously in the background. This creates a race condition that makes it difficult to reliably retrieve the access token or id token right after SAML login.
The issue is much more noticeable with SAML compared to social providers (Google, Facebook, etc.).

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Notifications (Push)
  • Storage

Steps to Reproduce

  1. Configure a Cognito User Pool with a SAML Identity Provider.
  2. Set up correct OAuth callback URLs and deep link scheme (AndroidManifest.xml is already configured correctly).
  3. Perform login:
    final result = await Amplify.Auth.signInWithWebUI(
    provider: const AuthProvider.saml('SSO'), // replace with your exact provider name
    );

print('isSignedIn: ${result.isSignedIn}'); // → always returns true
5. Immediately attempt to fetch tokens:
final session = await Amplify.Auth.fetchAuthSession(
options: const FetchAuthSessionOptions(forceRefresh: true),
) as CognitoAuthSession;

final accessToken = session.userPoolTokensResult.value.accessToken.raw; // ← frequently fails or has no value on first attempt

Expected behavior
After signInWithWebUI returns isSignedIn: true, fetchAuthSession() should reliably return the user pool tokens immediately, or Amplify should provide a clear mechanism (callback, future, or event) to know when the token exchange has completed.
There should be no race condition between the redirect returning to the app and token availability.

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.32.8

Amplify Flutter Version

2.6.0

Deployment Method

Amplify Gen 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    authIssues related to the Auth CategorybugSomething is not working; the issue has reproducible steps and has been reproduced

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions