Flutter Sign in With Apple not working on Android

307 Views Asked by At

I am using 'sign_in_with_apple' package to sign in with Apple ID. As Backend I am Using FireBase. My Code works fine on IOS, but Android. I followed and looked this tutorial on Medium, this tutorial on firebase-page and this package-readme. Enabled Apple SignIn for the app in the Identifier. Created serviceID. Created Key. Filled all in the Firebase page (Apple-Team-ID, Key-ID, Private Key). Filled as Return URL '[FIREBASE_APPLE_SIGN_IN_RETURN_URL]' (which is provided from FireBase).

On IOS it is working fine but this happens when I try to sign in on android emulator:

  • I click on Sign in with Apple
  • I will be redirected to apple (appleid.apple.com)
  • I fill in my ID and Password and press 'next'
  • apple asks me if I am sure and want to continue, I click 'Continue'
  • I am getting the error:

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.

My SignInCode:

/// Sign In With Apple
  signInWithApple() async {
    try {
      String clientID = 'com.example-service'; /// same ID like in the service ID, com.example is a placeholder
      String redirectURL = '[FIREBASE_APPLE_SIGN_IN_RETURN_URL]'; /// Here I have the URL and this is just a placeholder

      /// Generates a Random String from 1-9 and A-Z characters.
      final rawNonce = generateNonce();

      /// We are convering that rawNonce into SHA256 for security purposes
      /// In our login.
      final nonce = sha256ofString(rawNonce);

      final appleCredential = await SignInWithApple.getAppleIDCredential(
        /// Scopes are the values that you are requiring from
        /// Apple Server.
        scopes: [
          AppleIDAuthorizationScopes.email,
          AppleIDAuthorizationScopes.fullName,
        ],
        nonce: Platform.isIOS ? nonce : null,

        /// We are providing Web Authentication for Android Login,
        /// Android uses web browser based login for Apple.
        webAuthenticationOptions: Platform.isIOS
            ? null
            : WebAuthenticationOptions(
                clientId: clientID,
                redirectUri: Uri.parse(redirectURL),
              ),
      );

      final AuthCredential appleAuthCredential = OAuthProvider('apple.com').credential(
        idToken: appleCredential.identityToken,
        rawNonce: Platform.isIOS ? rawNonce : null,
        accessToken: Platform.isIOS ? null : appleCredential.authorizationCode,
      );

      /// Once you are successful in generating Apple Credentials,
      /// We pass them into the Firebase function to finally sign in.
      UserCredential result = await _auth.signInWithCredential(appleAuthCredential);
      User? user = result.user;

      /// Check everything is ok and user is not null
      if (user != null) {
        if (result.additionalUserInfo!.isNewUser) {
          await user.delete();
          signOut();
          return Strings.youHaveToSignUpbeforeSignInWithApple;
        }
        return _userFromFireBaseUser(user);
      }
      return null;
    } catch (e) {
      rethrow;
    }
  }

Since they didn't mention to change anything in the AndroidManifest.xml, I didn't changed it.

0

There are 0 best solutions below