firebase-ios-sdk icon indicating copy to clipboard operation
firebase-ios-sdk copied to clipboard

Unable to process request due to missing initial state. This may happen if browser sessionStorage is inaccessible or accidentally cleared. Some specific scenarios are - 1) Using IDP-Initiated SAML SSO. 2) Using signInWithRedirect in a storage-partitioned browser environment.

Open dennysoft opened this issue 1 year ago • 21 comments

Description

Hi, I'm using OpenId Connect with an external salesforce provider, on IOS (swift) after calling the getCredentialWith method, every now and then I get the following message when logging in: "Unable to process request due to missing initial state. This may happen if browser sessionStorage is inaccessible or accidentally cleared. Some specific scenarios are - 1) Using IDP-Initiated SAML SSO 2) Using signInWithRedirect in a storage-partitioned browser environment."

On the next login attempt everything works. Help, I don't know what to do anymore. Thank you photo_2024-06-03_15-20-48

Reproducing the issue

No response

Firebase SDK Version

10.25.0

Xcode Version

15.4

Installation Method

CocoaPods

Firebase Product(s)

Authentication

Targeted Platforms

iOS

Relevant Log Output

No response

If using Swift Package Manager, the project's Package.resolved

Expand Package.resolved snippet

Replace this line with the contents of your Package.resolved.

If using CocoaPods, the project's Podfile.lock

Expand Podfile.lock snippet

Replace this line with the contents of your Podfile.lock!

dennysoft avatar Jun 06 '24 13:06 dennysoft

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Jun 06 '24 13:06 google-oss-bot

Does anything at https://stackoverflow.com/q/67373448/556617 help?

paulb777 avatar Jun 06 '24 14:06 paulb777

no paulb777, i have try but nothing

dennysoft avatar Jun 06 '24 15:06 dennysoft

@dennysoft it looks like your issue is related here. Have you tried checking the doc to use signInWithRedirect?

rizafran avatar Jun 07 '24 12:06 rizafran

Rizafran, thanks, i have read this documentation, but the web side is a Salesforce authentication provider, that documentation refers to Firebase Web

dennysoft avatar Jun 13 '24 09:06 dennysoft

Same issue:

<!DOCTYPE html>
<html>
<head>
  <title>FirebaseUI with Compat</title>
  <!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
  <script src="https://www.gstatic.com/firebasejs/9.8.0/firebase-app-compat.js"></script>
  <script src="https://www.gstatic.com/firebasejs/9.8.0/firebase-auth-compat.js"></script>
  <script src="https://www.gstatic.com/firebasejs/9.8.0/firebase-firestore-compat.js"></script>

  <!-- Include FirebaseUI -->
  <link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/4.8.0/firebase-ui-auth.css" />
  <script src="https://www.gstatic.com/firebasejs/ui/4.8.0/firebase-ui-auth.js"></script>

  <style>
    /* Add some styling */
    #firebaseui-auth-container, #welcome-container, #loader {
      margin: 0 auto;
      width: 300px;
      text-align: center;
    }
    #sign-out-button {
      display: none;
    }
  </style>
</head>
<body>
  <h1>FirebaseUI with Compat</h1>
  <div id="firebaseui-auth-container"></div>
  <div id="welcome-container" style="display:none;">
    <h2>Welcome, <span id="user-name"></span>!</h2>
    <button id="sign-out-button">Sign Out</button>
  </div>
  <div id="loader">Loading...</div>

  <script>
    // Replace the following with your app's Firebase project configuration
    const firebaseConfig = {
      apiKey: "YOUR_API_KEY",
      authDomain: "localhost",
      projectId: "YOUR_PROJECT_ID",
      storageBucket: "YOUR_STORAGE_BUCKET",
      messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
      appId: "YOUR_APP_ID",
    };

    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);

    // Initialize the FirebaseUI Widget using Firebase.
    const ui = new firebaseui.auth.AuthUI(firebase.auth());

    // FirebaseUI config.
    const uiConfig = {
      callbacks: {
        signInSuccessWithAuthResult: function(authResult, redirectUrl) {
          // User successfully signed in.
          return false; // Do not redirect.
        },
        uiShown: function() {
          // The widget is rendered.
          // Hide the loader.
          document.getElementById('loader').style.display = 'none';
        }
      },
      signInFlow: 'redirect',
      signInOptions: [
        firebase.auth.GoogleAuthProvider.PROVIDER_ID,
        firebase.auth.FacebookAuthProvider.PROVIDER_ID,
        firebase.auth.TwitterAuthProvider.PROVIDER_ID,
        firebase.auth.GithubAuthProvider.PROVIDER_ID,
        firebase.auth.EmailAuthProvider.PROVIDER_ID,
        firebase.auth.PhoneAuthProvider.PROVIDER_ID
      ],
      tosUrl: '<your-tos-url>', // Terms of service url
      privacyPolicyUrl: '<your-privacy-policy-url>' // Privacy policy url
    };

    // Set persistence and then start FirebaseUI
    firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
      .then(() => {
        // Initialize the FirebaseUI Widget using Firebase.
        ui.start('#firebaseui-auth-container', uiConfig);
      })
      .catch((error) => {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        console.error('Persistence error', errorCode, errorMessage);
      });

    // Check the authentication state on page load and display the appropriate UI
    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        // User is signed in.
        document.getElementById('user-name').textContent = user.displayName || user.email;
        document.getElementById('firebaseui-auth-container').style.display = 'none';
        document.getElementById('welcome-container').style.display = 'block';
        document.getElementById('sign-out-button').style.display = 'block';
      } else {
        // No user is signed in.
        document.getElementById('firebaseui-auth-container').style.display = 'block';
        document.getElementById('welcome-container').style.display = 'none';
        document.getElementById('sign-out-button').style.display = 'none';
      }
      document.getElementById('loader').style.display = 'none';
    });

    // Sign out button functionality
    document.getElementById('sign-out-button').addEventListener('click', function() {
      firebase.auth().signOut().then(function() {
        // Sign-out successful.
        document.getElementById('firebaseui-auth-container').style.display = 'block';
        document.getElementById('welcome-container').style.display = 'none';
        document.getElementById('sign-out-button').style.display = 'none';
      }).catch(function(error) {
        // An error happened.
        console.error('Sign out error', error);
      });
    });
  </script>
</body>
</html>

wpitallo avatar Jul 04 '24 21:07 wpitallo

Any update on this?

penguib avatar Aug 14 '24 01:08 penguib

Just use the signInWithProvider method and give it an AppleAuthProvider object, which is bundled in the firebase_auth package.

v-sheludchenko avatar Sep 11 '24 15:09 v-sheludchenko

Any update on this? I have this problem with iOS and oidc connected with an external provider. Once authentication is complete, and I get to firebase callback I get that error message. This happens with two different providers that implement oidc connect. However, on Firebase Android this problem only happened a few times while on iOS it is systematic.

dennysoft avatar Sep 13 '24 09:09 dennysoft

A reproducible example would help

paulb777 avatar Sep 16 '24 20:09 paulb777

This solution works for me on Flutter (Android & iOS):

void _handleGoogleSignIn() async {
    try {
      final GoogleSignIn googleSignIn = GoogleSignIn();
      final GoogleSignInAccount? googleUser = await googleSignIn.signIn();

      if (googleUser != null) {
        final GoogleSignInAuthentication googleAuth =
            await googleUser.authentication;
        final AuthCredential credential = GoogleAuthProvider.credential(
          accessToken: googleAuth.accessToken,
          idToken: googleAuth.idToken,
        );

        UserCredential userCredential =
            await FirebaseAuth.instance.signInWithCredential(credential);
        User? user = userCredential.user;

        if (user != null) {
          print('User signed in: ${user.email}');
        }
      }
    } catch (e) {
      print(e.toString());
    }
  }

kitarvin23 avatar Oct 01 '24 09:10 kitarvin23

https://pub.dev/packages/sign_in_with_apple#server This solved this issue here

pedrohsampaioo avatar Oct 18 '24 13:10 pedrohsampaioo

The following patch makes it runnable without any errors for the case where the app transits to the Twitter/X app when logging in. The handling of Twitter/X for third-party apps is seemingly problematic. If it is not installed, the browser can process correctly. In the following code, the internal browser is used instead.

Intrinsically, this problem should be solved in the Twitter/X app.

FIRAuthURLPresenter.m

-    if ([SFSafariViewController class]) {
-      self->_safariViewController = [[SFSafariViewController alloc] initWithURL:URL];
-      self->_safariViewController.delegate = self;
-      [self->_UIDelegate presentViewController:self->_safariViewController
-                                      animated:YES
-                                    completion:nil];
-      return;
-    } else {
+// The SFSafariViewController code was temporarily commented out.
+// The Twitter/X app, which is opened automatically by the universal link, cannot handle the auth processing expectedly.
+// FIRAuthWebViewController and its WKWebView that has disabled the universal link internally must be used.
+//    if ([SFSafariViewController class]) {
+//      self->_safariViewController = [[SFSafariViewController alloc] initWithURL:URL];
+//      self->_safariViewController.delegate = self;
+//      [self->_UIDelegate presentViewController:self->_safariViewController
+//                                      animated:YES
+//                                    completion:nil];
+//      return;
+//    } else {
       self->_webViewController = [[FIRAuthWebViewController alloc] initWithURL:URL delegate:self];
       UINavigationController *navController =
           [[UINavigationController alloc] initWithRootViewController:self->_webViewController];
       [self->_UIDelegate presentViewController:navController animated:YES completion:nil];
-    }
+//    }

FIRAuthWebViewController.m

-  decisionHandler(WKNavigationActionPolicyAllow);
+  decisionHandler(WKNavigationActionPolicyAllow + 2);

justoneplanet avatar Oct 21 '24 15:10 justoneplanet

https://pub.dev/packages/sign_in_with_apple#server This solved this issue here

I was able to resolve the same error mentioned in this issue with Glitch. I would like to avoid dependence on another service, but am I stuck with Glitch as a workaround?

toddmueller avatar Oct 25 '24 17:10 toddmueller

I got this issue on a Web app

AnasKhan321 avatar Mar 10 '25 05:03 AnasKhan321

@toddmueller you can implement same of Glitch yourself in your server

pedrohsampaioo avatar Mar 10 '25 20:03 pedrohsampaioo

Just use the signInWithProvider method and give it an AppleAuthProvider object, which is bundled in the firebase_auth package.

@v-sheludchenko - I could be missing something but given that this issue was reported against the Firebase iOS SDK and the links you posted are to a Flutter library, I'm unsure how they're relevant.

javawizard avatar Mar 10 '25 22:03 javawizard

@dennysoft is it fix ?

sumanpandi avatar May 22 '25 11:05 sumanpandi

Facing same issue

RoyalBosS-Ayush avatar Jun 05 '25 08:06 RoyalBosS-Ayush