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.72k stars 3.97k forks source link

🐛 [firebase_auth] Invalid verification ID if PhoneAuthProvider.credential called inside a callback inside "codeSent" callback #8054

Closed momo-p closed 2 years ago

momo-p commented 2 years ago

Bug report

The below code will return "[firebase_auth/invalid-verification-id]"

await auth!.verifyPhoneNumber(
                  phoneNumber: currentCountryCode + " " + phoneNumber,
                  verificationCompleted: (credential) {
                    InputVerifyCode.close(context);
                    Callback.openLoading();
                    resolveCredential(credential);
                  },
                  verificationFailed: (e){
                    if(e.code == "invalid-phone-number"){
                      print("INVALID_PHONE_NUMBER");
                      Callback.closeLoading();
                      Alert.show(context, AppLocalizations.of(context)!.loginFailed, AppLocalizations.of(context)!.invalidPhoneNumber);
                      return;
                    }

                    print("FIREBASE_SMS_AUTH_FAILED");
                    print(e.message);
                    Callback.closeLoading();
                    Alert.show(context, AppLocalizations.of(context)!.loginFailed, AppLocalizations.of(context)!.anErrorOccur);
                  },
                  codeSent: (verificationID, resendToken) async {
                    Callback.closeLoading();
                    InputVerifyCode.show(context, AppLocalizations.of(context)!.inputVerifyCode, (code) async {
                      Callback.openLoading();
                      PhoneAuthCredential credential = PhoneAuthProvider.credential(verificationId: verificationID, smsCode: code);
                      resolveCredential(credential);
                    });
                  },
                  codeAutoRetrievalTimeout: (verificationID){

                  }
                )

Stacktrace

E/flutter (19377): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [firebase_auth/invalid-verification-id] The verification ID used to create the phone auth credential is invalid.
E/flutter (19377): #0      MethodChannelFirebaseAuth.signInWithCredential (package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart:445:7)
E/flutter (19377): <asynchronous suspension>
E/flutter (19377): #1      FirebaseAuth.signInWithCredential (package:firebase_auth/src/firebase_auth.dart:497:7)
E/flutter (19377): <asynchronous suspension>
E/flutter (19377): #2      _Login.resolveCredential (package:test_app/pages/login/login.dart:69:27)
E/flutter (19377): <asynchronous suspension>
E/flutter (19377): 

But if i remove the callback on "codeSent" and use a fixed smsCode it's work

await auth!.verifyPhoneNumber(
                  phoneNumber: currentCountryCode + " " + phoneNumber,
                  verificationCompleted: (credential) {
                    InputVerifyCode.close(context);
                    Callback.openLoading();
                    resolveCredential(credential);
                  },
                  verificationFailed: (e){
                    if(e.code == "invalid-phone-number"){
                      print("INVALID_PHONE_NUMBER");
                      Callback.closeLoading();
                      Alert.show(context, AppLocalizations.of(context)!.loginFailed, AppLocalizations.of(context)!.invalidPhoneNumber);
                      return;
                    }

                    print("FIREBASE_SMS_AUTH_FAILED");
                    print(e.message);
                    Callback.closeLoading();
                    Alert.show(context, AppLocalizations.of(context)!.loginFailed, AppLocalizations.of(context)!.anErrorOccur);
                  },
                  codeSent: (verificationID, resendToken) async {
                    Callback.closeLoading();
                    PhoneAuthCredential credential = PhoneAuthProvider.credential(verificationId: verificationID, smsCode: "xxxxxx");
                    resolveCredential(credential);
                  },
                  codeAutoRetrievalTimeout: (verificationID){

                  }
                )

Flutter doctor

Click To Expand ``` $ flutter doctor Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 2.10.0, on Artix Linux 5.16.7-artix1-1, locale en_US.UTF-8) [✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0) [✗] Chrome - develop for the web (Cannot find Chrome executable at google-chrome) ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable. [✓] Android Studio (version 2020.3) [✓] Connected device (1 available) [✓] HTTP Host Availability ! Doctor found issues in 1 category. ```

Flutter dependencies

Click To Expand ``` flutter pub deps -- --style=compact Dart SDK 2.16.0 Flutter SDK 2.10.0 test_app 1.0.0+1 dependencies: - adeptutils 3.1.7+51 [flutter json_annotation package_info_plus device_info_plus shared_preferences date_format http fluttertoast url_launcher intl validators collection basic_utils] - carousel_slider 4.0.0 [flutter] - cupertino_icons 1.0.4 - firebase_auth 3.3.6 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 1.12.0 [firebase_core_platform_interface firebase_core_web flutter meta] - flutter 0.0.0 [characters collection material_color_utilities meta typed_data vector_math sky_engine] - flutter_datetime_picker 1.5.1 [flutter] - flutter_image 4.1.0 [flutter] - flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path typed_data vector_math] - flutter_secure_storage 5.0.2 [meta flutter flutter_secure_storage_linux flutter_secure_storage_macos flutter_secure_storage_platform_interface flutter_secure_storage_web flutter_secure_storage_windows] - http 0.13.4 [async http_parser meta path] - image_cropper 1.4.1 [flutter] - image_picker 0.8.4+6 [flutter flutter_plugin_android_lifecycle image_picker_for_web image_picker_platform_interface] - intl 0.17.0 [clock path] - path_provider 2.0.8 [flutter path_provider_android path_provider_ios path_provider_linux path_provider_macos path_provider_platform_interface path_provider_windows] dev dependencies: - flutter_lints 1.0.4 [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 typed_data] transitive dependencies: - async 2.8.2 [collection meta] - basic_utils 3.9.4 [http logging json_annotation pointycastle] - boolean_selector 2.1.0 [source_span string_scanner] - characters 1.2.0 - charcode 1.3.1 - clock 1.1.0 - collection 1.15.0 - convert 3.0.1 [typed_data] - cross_file 0.3.2 [flutter js meta] - date_format 2.0.5 - device_info_plus 3.2.1 [flutter device_info_plus_platform_interface device_info_plus_macos device_info_plus_linux device_info_plus_web device_info_plus_windows] - device_info_plus_linux 2.1.1 [device_info_plus_platform_interface file flutter meta] - device_info_plus_macos 2.2.2 [device_info_plus_platform_interface flutter] - device_info_plus_platform_interface 2.3.0+1 [flutter meta plugin_platform_interface] - device_info_plus_web 2.1.0 [device_info_plus_platform_interface flutter_web_plugins flutter] - device_info_plus_windows 2.1.1 [device_info_plus_platform_interface ffi flutter win32] - fake_async 1.2.0 [clock collection] - ffi 1.1.2 - file 6.1.2 [meta path] - firebase_auth_platform_interface 6.1.11 [firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 3.3.7 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta] - firebase_core_platform_interface 4.2.4 [collection flutter meta plugin_platform_interface] - firebase_core_web 1.5.4 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - flutter_plugin_android_lifecycle 2.0.5 [flutter] - flutter_secure_storage_linux 1.1.0 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_macos 1.1.0 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_platform_interface 1.0.0 [flutter plugin_platform_interface] - flutter_secure_storage_web 1.0.2 [flutter flutter_web_plugins flutter_secure_storage_platform_interface js] - flutter_secure_storage_windows 1.1.2 [flutter flutter_secure_storage_platform_interface] - flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta typed_data vector_math] - fluttertoast 8.0.8 [flutter flutter_web_plugins] - http_parser 4.0.0 [charcode collection source_span string_scanner typed_data] - image_picker_for_web 2.1.5 [flutter flutter_web_plugins image_picker_platform_interface pedantic] - image_picker_platform_interface 2.4.3 [flutter http plugin_platform_interface cross_file] - js 0.6.3 - json_annotation 4.4.0 [meta] - lints 1.0.1 - logging 1.0.2 - matcher 0.12.11 [stack_trace] - material_color_utilities 0.1.3 - meta 1.7.0 - package_info_plus 1.3.0 [flutter package_info_plus_platform_interface package_info_plus_linux package_info_plus_macos package_info_plus_windows package_info_plus_web] - package_info_plus_linux 1.0.3 [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.4 [flutter flutter_web_plugins http meta package_info_plus_platform_interface] - package_info_plus_windows 1.0.4 [package_info_plus_platform_interface ffi flutter win32] - path 1.8.0 - path_provider_android 2.0.11 [flutter path_provider_platform_interface] - path_provider_ios 2.0.7 [flutter path_provider_platform_interface] - path_provider_linux 2.1.5 [ffi flutter path path_provider_platform_interface xdg_directories] - path_provider_macos 2.0.5 [flutter path_provider_platform_interface] - path_provider_platform_interface 2.0.3 [flutter platform plugin_platform_interface] - path_provider_windows 2.0.5 [ffi flutter path path_provider_platform_interface win32] - pedantic 1.11.1 - platform 3.1.0 - plugin_platform_interface 2.1.2 [meta] - pointycastle 3.5.0 [collection convert js] - process 4.2.4 [file path platform] - shared_preferences 2.0.12 [flutter shared_preferences_android shared_preferences_ios shared_preferences_linux shared_preferences_macos shared_preferences_platform_interface shared_preferences_web shared_preferences_windows] - shared_preferences_android 2.0.10 [flutter shared_preferences_platform_interface] - shared_preferences_ios 2.0.9 [flutter shared_preferences_platform_interface] - shared_preferences_linux 2.0.4 [file flutter path path_provider_linux shared_preferences_platform_interface] - shared_preferences_macos 2.0.2 [flutter shared_preferences_platform_interface] - shared_preferences_platform_interface 2.0.0 [flutter] - shared_preferences_web 2.0.3 [flutter flutter_web_plugins shared_preferences_platform_interface] - shared_preferences_windows 2.0.4 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface] - sky_engine 0.0.99 - source_span 1.8.1 [collection path term_glyph] - stack_trace 1.10.0 [path] - stream_channel 2.1.0 [async] - string_scanner 1.1.0 [charcode source_span] - term_glyph 1.2.0 - test_api 0.4.8 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher] - typed_data 1.3.0 [collection] - url_launcher 6.0.18 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] - url_launcher_android 6.0.14 [flutter url_launcher_platform_interface] - url_launcher_ios 6.0.14 [flutter url_launcher_platform_interface] - url_launcher_linux 2.0.3 [flutter] - url_launcher_macos 2.0.3 [flutter] - url_launcher_platform_interface 2.0.5 [flutter plugin_platform_interface] - url_launcher_web 2.0.6 [flutter flutter_web_plugins url_launcher_platform_interface] - url_launcher_windows 2.0.2 [flutter] - validators 3.0.0 - vector_math 2.1.1 - win32 2.3.10 [ffi] - xdg_directories 0.2.0 [meta path process] ```

darshankawar commented 2 years ago

@Quang19992 Can you try the same use case using plugin example and see if you still get same error / exception ?

momo-p commented 2 years ago

@Quang19992 Can you try the same use case using plugin example and see if you still get same error / exception ?

It's seem not throwing an exception anymore My InputVerifyCode#show is just a wrapper for a showCupertinoDialog

darshankawar commented 2 years ago

Closing based on your comment above. If you get the same exception again, write in comments and I'll reopen it. Also, try to look for a pattern which triggers this exception, so that it'll be helpful to debug further.

momo-p commented 2 years ago

Closing based on your comment above. If you get the same exception again, write in comments and I'll reopen it. Also, try to look for a pattern which triggers this exception, so that it'll be helpful to debug further.

I'm sorry for misunderstanding, I means if I try like the one on example (make a textfield in the same widget and show it on "codeSent") it work, but on my case i try to have user input on a CupertinoAlertDialog after user click "OK" which trigger my callback caused the exception above