firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.74k stars 3.98k forks source link

🐛 [firebase_auth] The getIdToken method of the User class is returning a null token string after a successful login #5434

Closed bernardo-cloudwalk closed 2 years ago

bernardo-cloudwalk commented 3 years ago

Bug report

Describe the bug The getIdToken method of the User class is returning a null value after a successful login using phone auth. We didn't achieve to reproduce this bug on a debug session. However, 20% of our login tentatives are failing due to that bug.

Steps to reproduce

Steps to reproduce the behavior:

  1. Create a FirebaseAuth instance
  2. Call the verifyPhoneNumber method of the FirebaseAuth instance with a valid phone number
  3. Instantiate a credential object calling the getCredential static method of the PhoneAuthProvider class with the SMS code and the verification ID received at the callback of verifyPhoneNumber method.
  4. Receive a result object calling the signInWithCredential method of the FirebaseAuth instance passing the credential object generated before.
  5. Get the user object from the result object returned by signInWithCredential method and call the method getIdToken of this user object.
  6. The token string of the object received on the response of the getIdToken method could be null.

Expected behavior

The token string must be a non-null and non-empty value that can be sent to our backend service and validated using Admin SDK.


Additional context

Add any other context about the problem here.


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` [✓] Flutter (Channel stable, 1.22.1, on Mac OS X 10.15.4 19E287, locale en-US) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) [✓] Xcode - develop for iOS and macOS (Xcode 12.2) [!] Android Studio (version 4.1) ✗ Flutter plugin not installed; this adds Flutter specific functionality. ✗ Dart plugin not installed; this adds Dart specific functionality. [✓] VS Code (version 1.54.3) [✓] Connected device (1 available) ! Doctor found issues in 1 category. ```

Flutter dependencies

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

Click To Expand ``` - advertising_id 1.0.0 [flutter] - cached_network_image 2.5.0 [flutter flutter_cache_manager octo_image] - camera 0.5.8+17 [flutter] - contacts_service 0.4.6 [flutter collection quiver] - cybersource 0.0.1 [flutter] - dartz 0.9.2 - device_info 0.4.2+10 [flutter device_info_platform_interface] - equatable 1.2.6 [collection meta] - facebook_app_events 0.8.2 [flutter] - firebase_analytics 5.0.16 [meta flutter firebase_analytics_web firebase_analytics_platform_interface] - firebase_auth 0.16.1 [meta firebase_core firebase_auth_platform_interface firebase_auth_web flutter] - firebase_core 0.4.5 [firebase_core_platform_interface flutter meta firebase_core_web] - firebase_crashlytics 0.1.4+1 [flutter stack_trace] - firebase_messaging 6.0.16 [meta platform flutter] - firebase_remote_config 0.3.1+1 [flutter] - flare_flutter 2.0.6 [flutter flare_dart meta] - flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine] - flutter_cipher 0.0.3 [asn1lib collection pointycastle args flutter] - flutter_keyboard_visibility 3.3.0 [flutter] - flutter_localizations 0.0.0 [flutter intl characters collection meta path typed_data vector_math] - flutter_masked_text 0.8.0 [flutter] - flutter_mobx 1.1.0+2 [mobx flutter] - flutter_secure_storage 3.3.5 [meta flutter] - flutter_swiper 1.1.6 [flutter transformer_page_view flutter_page_indicator] - flutter_user_agent 1.2.2 [flutter] - geocoder 0.2.1 [meta flutter] - geolocator 5.3.2+2 [flutter meta equatable google_api_availability location_permissions vector_math] - get_it 5.0.6 [async meta] - google_maps_webservice 0.0.19 [http meta] - http 0.12.2 [http_parser path pedantic] - image_cropper 1.3.1 [flutter] - image_picker 0.6.7+22 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface] - image_sequence_animator 1.0.10 [flutter cached_network_image] - intercom_flutter 2.3.4 [flutter] - intl 0.16.1 [path] - ipd_info 0.0.1 [flutter package_info device_info dio wifi sensors battery contacts_service location disk_space trust_fall geolocator] - json_serializable 3.5.1 [analyzer build build_config json_annotation meta path source_gen] - keyboard_actions 3.3.1+1 [flutter] - launch_review 2.0.0 [flutter] - location 3.2.4 [flutter location_platform_interface location_web meta] - mask_text_input_formatter 1.2.1 [flutter] - mobx 1.2.1+4 [meta] - network_image_to_byte 0.0.1 [flutter] - package_info 0.4.3+4 [flutter] - path 1.8.0-nullsafety.1 - path_provider 1.6.27 [flutter path_provider_platform_interface path_provider_macos path_provider_linux path_provider_windows] - pdf 1.13.0 [archive barcode crypto image meta path_parsing vector_math xml] - pdf_flutter 1.1.4 [flutter flutter_cache_manager http path_provider] - permission_handler 5.1.0+2 [flutter meta permission_handler_platform_interface] - personaflutter 0.0.1 [flutter] - pull_to_refresh 1.6.0 [flutter] - retrofit 1.3.4+1 [meta dio] - rxdart 0.24.1 - scroll_to_index 1.0.6 [flutter] - sensors 0.4.2+6 [flutter] - sentry 3.0.1 [http meta stack_trace usage pedantic] - share_extend 1.1.9 [meta flutter] - shimmer 1.1.2 [flutter] - styled_text 1.0.3+4 [flutter xmlstream] - threading 0.0.10 - uni_links 0.4.0 [flutter] - url_launcher 5.7.10 [flutter url_launcher_platform_interface url_launcher_web url_launcher_linux url_launcher_macos url_launcher_windows] - uuid 2.2.2 [crypto convert] - webview_flutter 1.0.7 [flutter] - whatsapp_share 0.0.1 [flutter] dev dependencies: - build_runner 1.11.1 [args async build build_config build_daemon build_resolvers build_runner_core code_builder collection crypto dart_style glob graphs http_multi_server io js logging meta mime path pedantic pool pub_semver pubspec_parse shelf shelf_web_socket stack_trace stream_transform timing watcher web_socket_channel yaml] - effective_dart 1.3.0 - flutter_launcher_icons 0.8.1 [image args yaml path] - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data] - mobx_codegen 1.1.2 [analyzer build build_resolvers meta mobx path source_gen] - mockito 4.1.4 [analyzer build code_builder collection dart_style matcher meta source_gen test_api] - retrofit_generator 1.4.1+2 [dio source_gen built_collection code_builder tuple retrofit analyzer dart_style build] transitive dependencies: - _fe_analyzer_shared 14.0.0 [meta] - analyzer 0.41.2 [_fe_analyzer_shared args cli_util collection convert crypto glob meta package_config path pub_semver source_span watcher yaml] - archive 2.0.13 [crypto args path] - args 1.6.0 - asn1lib 0.5.15 - async 2.5.0-nullsafety.1 [collection] - barcode 1.17.1 [meta qr] - battery 1.0.11 [flutter meta battery_platform_interface] - battery_platform_interface 1.0.0 [flutter meta plugin_platform_interface] - boolean_selector 2.1.0-nullsafety.1 [source_span string_scanner] - build 1.6.2 [analyzer async convert crypto glob logging meta path] - build_config 0.4.5 [checked_yaml json_annotation meta path pubspec_parse yaml] - build_daemon 2.1.7 [built_collection built_value http_multi_server logging pedantic path pool shelf shelf_web_socket stream_transform watcher web_socket_channel] - build_resolvers 1.5.3 [analyzer build crypto graphs logging meta path package_config pool pub_semver] - build_runner_core 6.1.7 [async build build_config build_resolvers collection convert crypto glob graphs logging meta path package_config pedantic pool timing watcher yaml] - built_collection 4.3.2 [collection quiver] - built_value 7.1.0 [built_collection collection fixnum quiver] - characters 1.1.0-nullsafety.3 - charcode 1.2.0-nullsafety.1 - checked_yaml 1.0.4 [json_annotation source_span yaml] - cli_util 0.2.0 [path] - clock 1.1.0-nullsafety.1 - code_builder 3.6.0 [built_collection built_value collection matcher meta] - collection 1.15.0-nullsafety.3 - convert 2.1.1 [charcode typed_data] - crypto 2.1.5 [collection convert typed_data] - dart_style 1.3.12 [analyzer args path pub_semver source_span] - device_info_platform_interface 1.0.1 [flutter meta plugin_platform_interface] - dio 3.0.10 [http_parser path] - disk_space 0.0.3 [flutter] - fake_async 1.2.0-nullsafety.1 [clock collection] - ffi 0.1.3 - file 5.2.1 [intl meta path] - firebase 7.3.3 [http http_parser js] - firebase_analytics_platform_interface 1.0.3 [flutter meta] - firebase_analytics_web 0.1.1 [flutter flutter_web_plugins firebase firebase_analytics_platform_interface meta] - firebase_auth_platform_interface 1.1.8 [flutter meta plugin_platform_interface] - firebase_auth_web 0.1.3+1 [firebase_auth_platform_interface flutter flutter_web_plugins firebase http_parser meta js] - firebase_core_platform_interface 1.0.4 [flutter meta plugin_platform_interface quiver] - firebase_core_web 0.1.1+2 [firebase firebase_core_platform_interface flutter flutter_web_plugins meta js] - fixnum 0.10.11 - flare_dart 2.3.4 - flutter_blurhash 0.5.0 [flutter meta] - flutter_cache_manager 2.1.0 [flutter path_provider uuid http path sqflite pedantic clock file rxdart image] - flutter_page_indicator 0.0.3 [flutter] - flutter_plugin_android_lifecycle 1.0.11 [flutter] - flutter_web_plugins 0.0.0 [flutter characters collection meta typed_data vector_math] - glob 1.2.0 [async collection node_io path pedantic string_scanner] - google_api_availability 2.0.4 [flutter] - graphs 0.2.0 - http_multi_server 2.2.0 [async] - http_parser 3.1.4 [charcode collection source_span string_scanner typed_data] - image 2.1.19 [archive xml meta] - image_picker_platform_interface 1.1.6 [flutter meta http plugin_platform_interface] - io 0.3.5 [meta path string_scanner] - js 0.6.2 - json_annotation 3.1.1 - location_permissions 3.0.0+1 [flutter meta] - location_platform_interface 1.1.0 [flutter meta plugin_platform_interface] - location_web 1.0.1 [flutter flutter_web_plugins http_parser js location_platform_interface meta] - logging 0.11.4 - matcher 0.12.10-nullsafety.1 [stack_trace] - meta 1.3.0-nullsafety.3 - mime 0.9.7 - node_interop 1.2.1 [js] - node_io 1.2.0 [file node_interop path] - octo_image 0.3.0 [flutter flutter_blurhash] - package_config 1.9.3 [path charcode] - path_parsing 0.1.4 [vector_math meta] - path_provider_linux 0.0.1+2 [path xdg_directories path_provider_platform_interface flutter] - path_provider_macos 0.0.4+8 [flutter] - path_provider_platform_interface 1.0.4 [flutter meta platform plugin_platform_interface] - path_provider_windows 0.0.4+3 [path_provider_platform_interface meta path flutter ffi win32] - pedantic 1.9.2 [meta] - permission_handler_platform_interface 2.0.2 [flutter meta plugin_platform_interface] - petitparser 3.1.0 [meta] - platform 2.2.1 - plugin_platform_interface 1.0.3 [meta] - pointycastle 1.0.2 - pool 1.4.0 [async stack_trace] - process 3.0.13 [file intl meta path platform] - pub_semver 1.4.4 [collection] - pubspec_parse 0.1.8 [checked_yaml json_annotation pub_semver yaml] - qr 1.3.0 [meta] - quiver 2.1.5 [matcher meta] - shelf 0.7.9 [async collection http_parser path stack_trace stream_channel] - shelf_web_socket 0.2.4+1 [shelf stream_channel web_socket_channel] - sky_engine 0.0.99 - source_gen 0.9.10+3 [analyzer async build dart_style glob meta path pedantic source_span] - source_span 1.8.0-nullsafety.2 [charcode collection path term_glyph] - sqflite 1.3.1+2 [flutter sqflite_common path] - sqflite_common 1.0.3+1 [synchronized path meta] - stack_trace 1.10.0-nullsafety.1 [path] - stream_channel 2.1.0-nullsafety.1 [async] - stream_transform 1.2.0 - string_scanner 1.1.0-nullsafety.1 [charcode source_span] - synchronized 2.2.0+2 - term_glyph 1.2.0-nullsafety.1 - test_api 0.2.19-nullsafety.2 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher] - timing 0.1.1+3 [json_annotation] - transformer_page_view 0.1.6 [flutter] - trust_fall 1.0.4 [flutter] - tuple 1.0.3 [quiver] - typed_data 1.3.0-nullsafety.3 [collection] - url_launcher_linux 0.0.1+4 [flutter] - url_launcher_macos 0.0.1+9 [flutter] - url_launcher_platform_interface 1.0.9 [flutter meta plugin_platform_interface] - url_launcher_web 0.1.5+3 [url_launcher_platform_interface flutter flutter_web_plugins meta] - url_launcher_windows 0.0.1+3 [flutter] - usage 3.4.2 [path] - vector_math 2.1.0-nullsafety.3 - watcher 0.9.7+15 [async path pedantic] - web_socket_channel 1.2.0 [async crypto stream_channel] - wifi 0.1.5 [flutter] - win32 1.7.4+1 [ffi] - xdg_directories 0.1.2 [meta path process] - xml 4.5.1 [collection convert meta petitparser] - xmlstream 0.12.0 - yaml 2.2.1 [charcode collection string_scanner source_span] ```

markusaksli-nc commented 3 years ago

Was not able to reproduce this in debug either but labelling this based on the report

bmfreitas commented 3 years ago

@markusaksli-nc we are not sure why we are unable to reproduce this in debug.

It worth mentioning that this problem affects only Android devices, also, we previously had ~40% logging errors while using Firebase Auth plugin on version 0.14.0, which decreased to ~20% once we upgraded to version 0.16.0.

Ehesp commented 3 years ago

@bmfreitas Have you managed to discover any relationship between this issue and say specific devices/Android versions?

rrousselGit commented 3 years ago

I have tried on a one+6 both in debug and release mode, with the most recent versions of the packages, and am unable to reproduce the issue.

Do you think you could create a minimalist example to reproduce the issue?

bernardo-cloudwalk commented 3 years ago

@Ehesp there's no relationship between this issue and specific android versions or devices. I have cases of users that have two Samsung Galaxy S10 devices with the same Android version and in one of them the login is successful and the other not.

bernardo-cloudwalk commented 3 years ago

@rrousselGit Below we have the source code of the steps after step 3.

  Future<String> confirmFirebaseLogin(String sms) async {
    try {
      // assuming the _verificationId is a global attribute of the class
      final authCredential = await PhoneAuthProvider.getCredential(
          verificationId: _verificationId, smsCode: sms);
      final result =
          await FirebaseAuth.instance.signInWithCredential(authCredential);
      if ((result != null) && (result.user != null)) {
        final idToken = await result.user.getIdToken();
        return idToken.token;
      } else {
        throw Exception("SMSConfirmation_ErrorAuthResult");
      }
    } catch (e) {
      SentryManager.instance.report(e, null);
      return null;
    }
  }
rrousselGit commented 3 years ago

This code works for me, so I'm not quite sure what to do here.

Any thought on how to move forward @Ehesp / @mikehardy?

mikehardy commented 3 years ago

@rrousselGit I can only be vague - but there is a report on the react-native-firebase repo that current versions of the SDK are behaving, that is, actually returning tokens for users that were previously seeing null tokens coming back. So this may be version dependent. I am certain it was happening but I never saw a reproduction unfortunately so I have nothing concrete to offer

russellwheatley commented 2 years ago

Hi @bernardo-cloudwalk, are you still seeing this issue?

google-oss-bot commented 2 years ago

Hey @bernardo-cloudwalk. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot commented 2 years ago

Since there haven't been any recent updates here, I am going to close this issue.

@bernardo-cloudwalk if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.