firebase / firebase-ios-sdk

Firebase SDK for Apple App Development
https://firebase.google.com
Apache License 2.0
5.62k stars 1.47k forks source link

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. #13084

Open dennysoft opened 4 months ago

dennysoft commented 4 months ago

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
```json Replace this line with the contents of your Package.resolved. ```

If using CocoaPods, the project's Podfile.lock

Expand Podfile.lock snippet
```yml Replace this line with the contents of your Podfile.lock! ```
google-oss-bot commented 4 months ago

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

paulb777 commented 4 months ago

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

dennysoft commented 4 months ago

no paulb777, i have try but nothing

rizafran commented 4 months ago

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

dennysoft commented 4 months ago

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

wpitallo commented 3 months ago

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>
penguib commented 2 months ago

Any update on this?

v-sheludchenko commented 1 month ago

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

dennysoft commented 1 month ago

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.

paulb777 commented 1 month ago

A reproducible example would help

kitarvin23 commented 2 weeks ago

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());
    }
  }
pedrohsampaioo commented 1 day ago

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