flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

🐛 [firebase_auth] Can't sign in with Apple from Android

Open gabrc52 opened this issue 4 years ago • 20 comments

Bug report

Describe the bug I can't sign in with Apple from Android, no matter what I pass in redirectUri.

Steps to reproduce

Steps to reproduce the behavior:

  1. Set up sign in with Apple on Apple's developer portal, set up Glitch as instructed in sign_in_with_apple's docs, and enable Apple sign-in in the Firebase console. Upload all needed constants and keys to Glitch and Firebase.
  2. Try to sign in with Apple on Android using the provided code based on https://firebase.google.com/docs/auth/flutter/federated-auth#ios+-and-android_2
  3. Set the redirectUri in getAppleIDCredential depending on one of three cases
  4. Get an error

Case a: just like the docs

2a. Don't pass a redirectUri (even though it's a required parameter) 3a. Get an error message from appleid.apple.com saying

⚠️ invalid_request Invalid web request url.

Screenshot of 3a

image

Case b: use the URL provided by Firebase

2b. Pass https://[FIREBASE_PROJECT].firebaseapp.com/__/auth/handler as the redirectUri 3b. Get an error message from firebaseapp.com saying:

Unable to process request due to missing initial state. This may happen if browser sessionStorage is inaccessible or accidentally cleared.

Screenshot of 3b

image

Case c: use the Glitch URL as illustrated in https://pub.dev/packages/sign_in_with_apple#server

2c. Pass https://[YOUR-PROJECT-NAME].glitch.me/callbacks/sign_in_with_apple as the redirectUri 3c. Get an exception that doesn't explain what went wrong - unknown

[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: [firebase_auth/unknown] null
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
<asynchronous suspension>
#2      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:367:43)
<asynchronous suspension>
#3      MethodChannelFirebaseAuth.signInWithCredential (package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart:433:12)
<asynchronous suspension>
#4      FirebaseAuth.signInWithCredential (package:firebase_auth/src/firebase_auth.dart:497:7)
<asynchronous suspension>
#5      SignInMethods.apple (package:miyotl/models/sign_in.dart:123:12)
<asynchronous suspension>
#6      SignInPage.doSignIn (package:miyotl/screens/sign_in.dart:37:24)
<asynchronous suspension>

I believe I followed all the instructions from the Firebase and sign_in_with_apple docs, but if you think the error is on my end, please let me know.

Expected behavior

Sign in succeeds

Sample project

Sign in code:

Excerpt from https://github.com/miyotl/miyotl/blob/master/lib/models/sign_in.dart#L73

/// Everything is just like on <https://firebase.google.com/docs/auth/flutter/federated-auth#ios+-and-android_2>
/// except the redirectUri and some additional lines to persist the name
static Future<UserCredential> apple() async {
  final rawNonce = generateNonce();
  final nonce = sha256ofString(rawNonce);

  final appleCredential = await SignInWithApple.getAppleIDCredential(
    scopes: [
      AppleIDAuthorizationScopes.email,
      AppleIDAuthorizationScopes.fullName,
    ],
    nonce: nonce,
    webAuthenticationOptions: WebAuthenticationOptions(
      clientId: 'miyotl',
      redirectUri: Uri.parse('https://radical-foil-hortensia.glitch.me/callbacks/sign_in_with_apple'),
    ),
  );

  // Store name for posterity
  if (appleCredential.givenName != null || appleCredential.familyName != null) {
    final displayName = '${appleCredential.givenName ?? ''} ${appleCredential.familyName ?? ''}';
    final prefs = await SharedPreferences.getInstance();
    prefs.setString('apple-name', displayName);
  }

  final oauthCredential = OAuthProvider("apple.com").credential(
    idToken: appleCredential.identityToken,
    rawNonce: rawNonce,
  );

  return await FirebaseAuth.instance.signInWithCredential(oauthCredential);
}

Additional context

  • The documentation https://firebase.google.com/docs/auth/flutter/federated-auth#ios+-and-android_2 mentions Android on the header, as well as iOS, so I'd expect it to work on Android too. It might need to be updated explaining the situation with the redirect URI
  • I tried both ^3.0.0 and ^4.0.0 versions of sign_in_with_apple. The former is in the documentation and the latter is the latest version. Both gave the same results.

image


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
[✓] Flutter (Channel stable, 3.0.1, on Arch Linux 5.18.1-arch1-1, locale
    en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version
    32.1.0-rc1)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2021.2)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

• No issues found!

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand
Dart SDK 2.17.1
Flutter SDK 3.0.1
miyotl 1.0.6+4008

dependencies:
- auto_size_text 3.0.0 [flutter]
- cached_network_image 3.2.1 [flutter flutter_cache_manager octo_image cached_network_image_platform_interface cached_network_image_web]
- crypto 3.0.2 [typed_data]
- cupertino_icons 1.0.5
- firebase_analytics 9.1.9 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter]
- firebase_auth 3.3.19 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta]
- firebase_core 1.17.1 [firebase_core_platform_interface firebase_core_web flutter meta]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_facebook_auth 4.3.4+2 [flutter flutter_facebook_auth_platform_interface flutter_facebook_auth_web]
- flutter_inappwebview 5.4.3+7 [flutter]
- flutter_json_viewer 1.0.1 [flutter]
- flutter_platform_widgets 1.20.0 [flutter]
- flutter_signin_button 2.0.0 [flutter font_awesome_flutter]
- google_api_availability 3.0.1 [flutter meta]
- google_fonts 3.0.1 [flutter http path_provider crypto]
- google_sign_in 5.3.3 [flutter google_sign_in_android google_sign_in_ios google_sign_in_platform_interface google_sign_in_web]
- http 0.13.4 [async http_parser meta path]
- ionicons 0.1.2 [flutter]
- liquid_swipe 2.1.1 [flutter provider]
- package_info_plus 1.4.2 [flutter package_info_plus_platform_interface package_info_plus_linux package_info_plus_macos package_info_plus_windows package_info_plus_web]
- path_provider 2.0.11 [flutter path_provider_android path_provider_ios path_provider_linux path_provider_macos path_provider_platform_interface path_provider_windows]
- provider 6.0.3 [collection flutter nested]
- sentry_flutter 6.5.1 [flutter flutter_web_plugins sentry package_info_plus meta]
- settings_ui 2.0.2 [flutter]
- share_plus 4.0.7 [meta mime flutter share_plus_platform_interface share_plus_linux share_plus_macos share_plus_windows share_plus_web]
- shared_preferences 2.0.15 [flutter shared_preferences_android shared_preferences_ios shared_preferences_linux shared_preferences_macos shared_preferences_platform_interface shared_preferences_web shared_preferences_windows]
- sign_in_with_apple 3.3.0 [flutter meta sign_in_with_apple_platform_interface sign_in_with_apple_web]
- system_settings 2.1.0 [flutter]
- url_launcher 6.1.3 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows]
- uuid 3.0.6 [crypto]
- video_player 2.4.4 [flutter html video_player_android video_player_avfoundation video_player_platform_interface video_player_web]

dev dependencies:
- flutter_lints 2.0.1 [lints]
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph]

transitive dependencies:
- async 2.8.2 [collection meta]
- boolean_selector 2.1.0 [source_span string_scanner]
- cached_network_image_platform_interface 1.0.0 [flutter flutter_cache_manager]
- cached_network_image_web 1.0.1 [flutter flutter_cache_manager cached_network_image_platform_interface]
- characters 1.2.0
- charcode 1.3.1
- clock 1.1.0
- collection 1.16.0
- csslib 0.17.2 [source_span]
- fake_async 1.3.0 [clock collection]
- ffi 1.2.1
- file 6.1.2 [meta path]
- firebase_analytics_platform_interface 3.1.7 [firebase_core flutter meta plugin_platform_interface]
- firebase_analytics_web 0.4.0+14 [firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js]
- firebase_auth_platform_interface 6.2.7 [firebase_core flutter meta plugin_platform_interface]
- firebase_auth_web 3.3.16 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta]
- firebase_core_platform_interface 4.4.0 [collection flutter meta plugin_platform_interface]
- firebase_core_web 1.6.4 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- flutter_blurhash 0.7.0 [flutter]
- flutter_cache_manager 3.3.0 [clock collection file flutter http path path_provider pedantic rxdart sqflite uuid]
- flutter_facebook_auth_platform_interface 3.1.2 [flutter plugin_platform_interface]
- flutter_facebook_auth_web 3.1.2 [flutter flutter_web_plugins js flutter_facebook_auth_platform_interface]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- font_awesome_flutter 9.2.0 [flutter]
- google_sign_in_android 5.2.8 [flutter google_sign_in_platform_interface]
- google_sign_in_ios 5.3.1 [flutter google_sign_in_platform_interface]
- google_sign_in_platform_interface 2.1.3 [flutter quiver]
- google_sign_in_web 0.10.1+3 [flutter flutter_web_plugins google_sign_in_platform_interface js]
- html 0.15.0 [csslib source_span]
- http_parser 4.0.1 [collection source_span string_scanner typed_data]
- intl 0.17.0 [clock path]
- js 0.6.4
- lints 2.0.0
- matcher 0.12.11 [stack_trace]
- material_color_utilities 0.1.4
- meta 1.7.0
- mime 1.0.2
- nested 1.0.0 [flutter]
- octo_image 1.0.2 [flutter flutter_blurhash]
- package_info_plus_linux 1.0.5 [package_info_plus_platform_interface flutter path]
- package_info_plus_macos 1.3.0 [flutter]
- package_info_plus_platform_interface 1.0.2 [flutter meta plugin_platform_interface]
- package_info_plus_web 1.0.5 [flutter flutter_web_plugins http meta package_info_plus_platform_interface]
- package_info_plus_windows 1.0.5 [package_info_plus_platform_interface ffi flutter win32]
- path 1.8.1
- path_provider_android 2.0.14 [flutter path_provider_platform_interface]
- path_provider_ios 2.0.9 [flutter path_provider_platform_interface]
- path_provider_linux 2.1.7 [ffi flutter path path_provider_platform_interface xdg_directories]
- path_provider_macos 2.0.6 [flutter path_provider_platform_interface]
- path_provider_platform_interface 2.0.4 [flutter platform plugin_platform_interface]
- path_provider_windows 2.0.7 [ffi flutter path path_provider_platform_interface win32]
- pedantic 1.11.1
- platform 3.1.0
- plugin_platform_interface 2.1.2 [meta]
- process 4.2.4 [file path platform]
- quiver 3.1.0 [matcher]
- rxdart 0.27.4
- sentry 6.5.1 [http meta stack_trace uuid]
- share_plus_linux 3.0.0 [share_plus_platform_interface file flutter meta url_launcher]
- share_plus_macos 3.0.1 [share_plus_platform_interface flutter]
- share_plus_platform_interface 3.0.3 [flutter meta mime plugin_platform_interface]
- share_plus_web 3.0.1 [share_plus_platform_interface url_launcher flutter flutter_web_plugins meta]
- share_plus_windows 3.0.1 [share_plus_platform_interface flutter meta url_launcher]
- shared_preferences_android 2.0.12 [flutter shared_preferences_platform_interface]
- shared_preferences_ios 2.1.1 [flutter shared_preferences_platform_interface]
- shared_preferences_linux 2.1.1 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface]
- shared_preferences_macos 2.0.4 [flutter shared_preferences_platform_interface]
- shared_preferences_platform_interface 2.0.0 [flutter]
- shared_preferences_web 2.0.4 [flutter flutter_web_plugins shared_preferences_platform_interface]
- shared_preferences_windows 2.1.1 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface]
- sign_in_with_apple_platform_interface 1.0.0 [flutter plugin_platform_interface meta]
- sign_in_with_apple_web 1.0.1 [flutter flutter_web_plugins sign_in_with_apple_platform_interface js]
- sky_engine 0.0.99
- source_span 1.8.2 [collection path term_glyph]
- sqflite 2.0.2+1 [flutter sqflite_common path]
- sqflite_common 2.2.1+1 [synchronized path meta]
- stack_trace 1.10.0 [path]
- stream_channel 2.1.0 [async]
- string_scanner 1.1.0 [charcode source_span]
- synchronized 3.0.0+2
- term_glyph 1.2.0
- test_api 0.4.9 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher]
- typed_data 1.3.1 [collection]
- url_launcher_android 6.0.17 [flutter url_launcher_platform_interface]
- url_launcher_ios 6.0.17 [flutter url_launcher_platform_interface]
- url_launcher_linux 3.0.1 [flutter url_launcher_platform_interface]
- url_launcher_macos 3.0.1 [flutter url_launcher_platform_interface]
- url_launcher_platform_interface 2.0.5 [flutter plugin_platform_interface]
- url_launcher_web 2.0.11 [flutter flutter_web_plugins url_launcher_platform_interface]
- url_launcher_windows 3.0.1 [flutter url_launcher_platform_interface]
- vector_math 2.1.2
- video_player_android 2.3.6 [flutter video_player_platform_interface]
- video_player_avfoundation 2.3.5 [flutter video_player_platform_interface]
- video_player_platform_interface 5.1.3 [flutter plugin_platform_interface]
- video_player_web 2.0.10 [flutter flutter_web_plugins video_player_platform_interface]
- win32 2.6.1 [ffi]
- xdg_directories 0.2.0+1 [meta path process]

gabrc52 avatar Jun 09 '22 03:06 gabrc52

Thanks for the detailed report @gabrc52. I have not yet configured apple sign in on a demo project, so I am unable to verify this behavior. Also, I wonder if this need to be reported in https://github.com/firebase/firebase-ios-sdk/issues or should be tracked here.

I'll keep this issue open for further insights from the team.

/cc @russellwheatley

darshankawar avatar Jun 09 '22 08:06 darshankawar

Seeing the same error when signing in with Apple on Android. The exception is thrown because this line expects the accessToken to be non null.

As a workaround, passing any string as the accessToken seems to be fixing the issue (no errors are thrown and signing in the user completes successfully).

final oauthCredential = OAuthProvider('apple.com').credential(
  idToken: appleCredential.identityToken,
  accessToken: '123',
  rawNonce: rawNonce,
);

jamesnocentini avatar Jun 18 '22 23:06 jamesnocentini

Seeing the same error when signing in with Apple on Android. The exception is thrown because this line expects the accessToken to be non null.

As a workaround, passing any string as the accessToken seems to be fixing the issue (no errors are thrown and signing in the user completes successfully).

final oauthCredential = OAuthProvider('apple.com').credential(
  idToken: appleCredential.identityToken,
  accessToken: '123',
  rawNonce: rawNonce,
);

Thanks for the input, @jamesnocentini 👍 .

Hey @gabrc52, are you able to confirm that the accessToken is causing the problem?

russellwheatley avatar Jun 20 '22 08:06 russellwheatley

Hello, for me the error Unable to process request due to missing initial state. This may happen if browser sessionStorage is inaccessible or accidentally cleared. is still there. It happens in the browser before using the idToken.

simkepal avatar Jun 21 '22 11:06 simkepal

@russellwheatley It works!

gabrc52 avatar Jun 22 '22 05:06 gabrc52

@gabrc52, which flow did you use? The one with the glitch.me redirectUri?

simkepal avatar Jun 22 '22 09:06 simkepal

Yes

gabrc52 avatar Jun 22 '22 16:06 gabrc52

@simkepal @gabrc52 @russellwheatley its just a waste of time ... this library did everything for you https://pub.dev/packages/eartho_one

fanswish avatar Jun 25 '22 08:06 fanswish

@fanswish Thanks for promoting your plug-in. I'll take it into consideration next time. /genuine

<off topic discussion>

However, it is a bit aggressive of you to promote a product this way, and not disclaim that there is both a free tier and a paid tier. GitHub issues are to report bugs in specific software, errors in documentation, and other similar things, and thanks to this I've found a workaround that fixes this in my existing code. I was already using firebase_auth with a manual UI, because it's what I knew and gives me more control without relying on more third-parties, and because I might want to use other Firebase services like its database. I don't think pinging a developer of your competitor is a good idea either.

For reference, flutterfire_ui seems like a first-party way to automate sign in without having to rewrite or copy, paste and adapt code from the Firebase documentation.

If I'd known about flutterfire_ui or eartho_one, I might have used them instead of writing a UI. Maybe this issue happens in those as well, and it'd have been reported anyway.

</off topic discussion>

gabrc52 avatar Jun 26 '22 03:06 gabrc52

Thank you @gabrc52. I don't like the approach with the glitch.me. I think there should be a solution to use just firebase link to process the response. Anyone else has an idea how to solve it? Thank you.

simkepal avatar Jun 27 '22 10:06 simkepal

Hello there,

For me the solution was simple.

Instead of using auth.signInWithCredential(credential) I replaced it with the new auth.signInWithProvider(AppleAuthProvider()) and now everything works fine.

It is part of the new federated auth documentation

qk7b avatar Oct 15 '22 07:10 qk7b

Hello there,

For me the solution was simple.

Instead of using auth.signInWithCredential(credential) I replaced it with the new auth.signInWithProvider(AppleAuthProvider()) and now everything works fine.

It is part of the new federated auth documentation

Not working! accessToken is wrong!

Ahmadre avatar Feb 14 '23 22:02 Ahmadre

Seeing the same error when signing in with Apple on Android. The exception is thrown because this line expects the accessToken to be non null.

As a workaround, passing any string as the accessToken seems to be fixing the issue (no errors are thrown and signing in the user completes successfully).

final oauthCredential = OAuthProvider('apple.com').credential(
  idToken: appleCredential.identityToken,
  accessToken: '123',
  rawNonce: rawNonce,
);

not working!

Ahmadre avatar Feb 14 '23 22:02 Ahmadre

I do have the same error :

Unable to process request due to missing initial state.
This may happen if browser sessionStorage is inaccessible or accidentally cleared.

Using :

 final Uri uri = Uri.parse("https://project-id.firebaseapp.com/__/auth/handler");
final appleCredential = await SignInWithApple.getAppleIDCredential(
        scopes: [
          AppleIDAuthorizationScopes.email,
          AppleIDAuthorizationScopes.fullName,
        ],
        webAuthenticationOptions: WebAuthenticationOptions(
            clientId: "myservice", redirectUri: uri),
        nonce: nonce,
      );

      // Create an `OAuthCredential` from the credential returned by Apple.
      final OAuthCredential oauthCredential =
          OAuthProvider("apple.com").credential(
        idToken: appleCredential.identityToken,
        accessToken: "fakeToken", // With or without the accessToken it doesn't work
        rawNonce: rawNonce,
      );

Tom3652 avatar Feb 27 '23 12:02 Tom3652

@Tom3652 did you get any resolution here? I have the same code and same error :/

ethernetz avatar Mar 09 '23 04:03 ethernetz

No unfortunately :/ i am hiding the Apple sign in button on my android app for now..

Tom3652 avatar Mar 09 '23 09:03 Tom3652

Facing the same issue. Working with glitch.me but using firebase redirection url, it throws the same error.

Screenshot

Screenshot_1678364408

slice-mohit avatar Mar 09 '23 12:03 slice-mohit

any updates. Found the same issue: Unable to process request due to missing initial state. This may happen if browser sessionStorage is inaccessible or accidentally cleared.

basemkhirat avatar Mar 17 '23 21:03 basemkhirat

Hello there,

For me the solution was simple.

Instead of using auth.signInWithCredential(credential) I replaced it with the new auth.signInWithProvider(AppleAuthProvider()) and now everything works fine.

It is part of the new federated auth documentation

Thanks a lot, fixed my issue by using signInWithProvider(AppleAuthProvider()).

nanistudio avatar May 02 '23 04:05 nanistudio

As mentioned in the last comment, you can now use the AppleAuthProvider. See documentation. I have tested this and it works on android.

russellwheatley avatar May 24 '23 10:05 russellwheatley