firebase / FirebaseUI-Flutter

Apache License 2.0
101 stars 91 forks source link

πŸ› flutterfire Few errors found when register a user using UI #44

Closed cupidchan closed 1 year ago

cupidchan commented 1 year ago

Bug report

Describe the bug Exception thrown when trying to register a user

Steps to reproduce

Steps to reproduce the behavior:

  1. Create a brand new Flutter project using Flutter CLI and then use the sample at https://github.com/firebase/flutterfire/tree/master/packages/firebase_ui_auth/example.
  2. Try to register a user: https://user-images.githubusercontent.com/12452100/214127630-b78d52cd-4b71-48c6-ba5a-e964a7d1f881.mov
  3. See the 2 errors below
  4. If I commented out line 36-38 and rerun the app, the 3rd error will show.
    if (FirebaseAuth.instance.currentUser == null) {
    await FirebaseAuth.instance.signInAnonymously();
    }

1st Error:

errors.dart:266 Uncaught Error: Assertion failed: file:///opt/homebrew/Caskroom/flutter/3.3.6/flutter/packages/flutter/lib/src/services/hardware_keyboard.dart:435:16
_pressedKeys[event.physicalKey] == event.logicalKey
"A KeyUpEvent is dispatched, but the state shows that the physical key is pressed on a different logical key. If this occurs in real application, please report this bug to Flutter. If this occurs in unit tests, please ensure that simulated events follow Flutter's event model as documented in `HardwareKeyboard`. This was the event: KeyUpEvent#c185a(physicalKey: PhysicalKeyboardKey#7000c(usbHidUsage: \"0x0007000c\", debugName: \"Key I\"), logicalKey: LogicalKeyboardKey#00069(keyId: \"0x00000069\", keyLabel: \"I\", debugName: \"Key I\"), character: null, timeStamp: 0:00:09.265000, synthesized) and the recorded logical key LogicalKeyboardKey#70b1f(keyId: \"0x17c007000c\", keyLabel: \"\", debugName: \"Key with ID 0x017c007000c\")"
    at Object.throw_ [as throw] (errors.dart:266:49)
    at Object.assertFailed (errors.dart:29:3)
    at hardware_keyboard.dart:435:56
    at [_assertEventIsRegular] (hardware_keyboard.dart:442:14)
    at hardware_keyboard.HardwareKeyboard.new.handleKeyEvent (hardware_keyboard.dart:535:5)
    at hardware_keyboard.KeyEventManager.new.handleKeyData (hardware_keyboard.dart:808:29)
    at platform_dispatcher.dart:273:33
    at Object.invoke (platform_dispatcher.dart:1168:13)
    at _engine.EnginePlatformDispatcher.new.invokeOnKeyData (platform_dispatcher.dart:272:7)
    at [_onKeyData] (keyboard_binding.dart:134:39)
    at keyboard_binding.dart:539:36
    at [_handleEvent] (keyboard_binding.dart:416:11)
    at _engine.KeyboardConverter.new.handleEvent (keyboard_binding.dart:542:7)
    at keyboard_binding.dart:141:25
    at Object._checkAndCall (operations.dart:334:14)
    at Object.dcall (operations.dart:339:39)
    at ret (js_patch.dart:413:11)
    at loggedHandler (keyboard_binding.dart:112:23)
    at Object._checkAndCall (operations.dart:334:14)
    at Object.dcall (operations.dart:339:39)
    at ret (js_patch.dart:413:11)

2nd Error:

index.ts:121          POST https://identitytoolkit.googleapis.com/v1/accounts:lookup?key=AIzaSyB7wZb2tO1-Fs6GbDADUSTs2Qs3w08Hovw 400

3rd Error: image

Expected behavior

No error should be thrown.

Sample project

https://github.com/cupidchan/firebase_ui_auth_issue10279


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` PASTE OUTPUT INSIDE HERE ``` Doctor summary (to see all details, run flutter doctor -v): [βœ“] Flutter (Channel stable, 3.3.10, on macOS 13.1 22C65 darwin-arm, locale en) [βœ“] Android toolchain - develop for Android devices (Android SDK version 31.0.0) [βœ“] Xcode - develop for iOS and macOS (Xcode 14.2) [βœ“] Chrome - develop for the web [βœ“] Android Studio (version 2021.3) [βœ“] VS Code (version 1.74.3) [βœ“] Connected device (2 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.18.6 Flutter SDK 3.3.10 carealth 1.0.0+1 dependencies: - cupertino_icons 1.0.5 - firebase_auth 4.2.4 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 2.4.1 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_dynamic_links 5.0.11 [firebase_core firebase_core_platform_interface firebase_dynamic_links_platform_interface flutter meta plugin_platform_interface] - firebase_ui_auth 1.1.6 [email_validator firebase_auth firebase_core firebase_dynamic_links firebase_ui_localizations firebase_ui_oauth flutter flutter_localizations flutter_svg] - firebase_ui_localizations 1.0.2 [flutter flutter_localizations intl path] - firebase_ui_oauth_apple 1.0.13 [firebase_auth firebase_ui_oauth flutter] - firebase_ui_oauth_google 1.0.13 [firebase_auth firebase_ui_oauth flutter google_sign_in] - flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine] - flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math] 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 collection matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph] - test 1.21.4 [analyzer async boolean_selector collection coverage http_multi_server io js node_preamble package_config path pool shelf shelf_packages_handler shelf_static shelf_web_socket source_span stack_trace stream_channel typed_data web_socket_channel webkit_inspection_protocol yaml test_api test_core] transitive dependencies: - _fe_analyzer_shared 47.0.0 [meta] - _flutterfire_internals 1.0.12 [collection firebase_core firebase_core_platform_interface flutter meta] - analyzer 4.7.0 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml] - args 2.3.1 - async 2.9.0 [collection meta] - boolean_selector 2.1.0 [source_span string_scanner] - characters 1.2.1 - clock 1.1.1 - collection 1.16.0 - convert 3.1.1 [typed_data] - coverage 1.6.2 [args logging package_config path source_maps stack_trace vm_service] - crypto 3.0.2 [typed_data] - desktop_webview_auth 0.0.11 [crypto flutter http flutter_web_plugins plugin_platform_interface] - email_validator 2.1.17 - fake_async 1.3.1 [clock collection] - file 6.1.4 [meta path] - firebase_auth_platform_interface 6.11.7 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 5.2.4 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta] - firebase_core_platform_interface 4.5.2 [collection flutter flutter_test meta plugin_platform_interface] - firebase_core_web 2.1.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_dynamic_links_platform_interface 0.2.3+26 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface] - firebase_ui_oauth 1.1.6 [desktop_webview_auth firebase_auth firebase_ui_auth flutter_svg flutter] - flutter_svg 1.1.6 [flutter meta path_drawing vector_math xml] - flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math] - frontend_server_client 2.1.3 [async path] - glob 2.1.1 [async collection file path string_scanner] - google_sign_in 5.4.2 [flutter google_sign_in_android google_sign_in_ios google_sign_in_platform_interface google_sign_in_web] - google_sign_in_android 6.1.4 [flutter google_sign_in_platform_interface] - google_sign_in_ios 5.5.1 [flutter google_sign_in_platform_interface] - google_sign_in_platform_interface 2.3.0 [flutter plugin_platform_interface quiver] - google_sign_in_web 0.10.2 [flutter flutter_web_plugins google_sign_in_platform_interface js] - http 0.13.5 [async http_parser meta path] - http_multi_server 3.2.1 [async] - http_parser 4.0.2 [collection source_span string_scanner typed_data] - intl 0.17.0 [clock path] - io 1.0.3 [meta path string_scanner] - js 0.6.4 - lints 2.0.1 - logging 1.1.0 - matcher 0.12.12 [stack_trace] - material_color_utilities 0.1.5 - meta 1.8.0 - mime 1.0.4 - node_preamble 2.0.1 - package_config 2.1.0 [path] - path 1.8.2 - path_drawing 1.0.1 [vector_math meta path_parsing flutter] - path_parsing 1.0.1 [vector_math meta] - petitparser 5.1.0 [meta] - plugin_platform_interface 2.1.3 [meta] - pool 1.5.1 [async stack_trace] - pub_semver 2.1.3 [collection meta] - quiver 3.2.1 [matcher] - shelf 1.4.0 [async collection http_parser path stack_trace stream_channel] - shelf_packages_handler 3.0.1 [path shelf shelf_static] - shelf_static 1.1.1 [convert http_parser mime path shelf] - shelf_web_socket 1.0.3 [shelf stream_channel web_socket_channel] - sky_engine 0.0.99 - source_map_stack_trace 2.1.1 [path source_maps stack_trace] - source_maps 0.10.11 [source_span] - source_span 1.9.0 [collection path term_glyph] - stack_trace 1.10.0 [path] - stream_channel 2.1.0 [async] - string_scanner 1.1.1 [source_span] - term_glyph 1.2.1 - test_api 0.4.12 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher] - test_core 0.4.16 [analyzer async args boolean_selector collection coverage frontend_server_client glob io meta package_config path pool source_map_stack_trace source_maps source_span stack_trace stream_channel vm_service yaml matcher test_api] - typed_data 1.3.1 [collection] - vector_math 2.1.2 - vm_service 9.4.0 - watcher 1.0.2 [async path] - web_socket_channel 2.3.0 [async crypto stream_channel] - webkit_inspection_protocol 1.2.0 [logging] - xml 6.1.0 [collection meta petitparser] - yaml 3.1.1 [collection source_span string_scanner] ```
---
cupidchan commented 1 year ago

Here is the video to reproduce the errors: https://user-images.githubusercontent.com/12452100/214127630-b78d52cd-4b71-48c6-ba5a-e964a7d1f881.mov

cupidchan commented 1 year ago

@darshankawar I saw that you remove the Needs Attention label. Do you have any idea how can I resolve those issues reported above? Thanks!

cupidchan commented 1 year ago

@darshankawar @lesnitsky I tried to debug this issue further and found an interesting thing: After a user is registered to the local emulator, the Auth instance will be reset and point to the live server. This explains where the 2nd error comes since my live Firebase server does NOT have that newly created user, whom is in the local emulator.

Now, the key question of this issue is: Why FlutterFire reset the useEmulator and connect to the live server after the first attempt? I appreciate if you can share some thoughts as I believe there may be just a simple setting I missed in my code.

cupidchan commented 1 year ago

@darshankawar @lesnitsky I debug further on this issue over the weekend and found this observation hopefully helpful. But there are still some mysteries I need your help to understand.

  1. After a user registers, the local Auth Emulator will have a link like this http://127.0.0.1:9099/emulator/action?mode=verifyEmail&lang=en&oobCode=ysngrHpCYPTqpFWUhchTR3quF3PTHk7YZmnutS7l9-W8EKaF-4-m9T&apiKey=fake-api-key&continueUrl=http%3A%2F%2Flocalhost%3A62062%2F%23%2Fprofile
  2. I copy and paste this link to the browser of my Flutter app, with the expectation that the app will redirect to the Profile page and show the user detail
  3. But instead, the main function of the main.dart is called again. This is really unexpected since there is no code change nor Hot Reload. (Question: why the main is called again?)
  4. For this 2nd time when the main function is called, I step into await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); and found that it initiate the Firebase Auth. This is another surprise because FirebaseAuth.instance is the next line, which should not be started yet.
  5. If I let it to continue, the app sends out a request to the Live Firebase server (NOT Emulator). This is kind of understandable because the next line has not been called yet await FirebaseAuth.instance.useAuthEmulator('localhost', 9099);
  6. From that point on, no more local emulator can be called and all the Auth is now route to the Live server.

The big question here is Why the main will be called again?

Your help is greatly apprecaited!

lesnitsky commented 1 year ago

1st error has nothing to do with the firebase_ui – it's a flutter issue.

I was able to reproduce 2nd error only when manually reloading web page – seems to be an issue with the firebase_auth on web, will follow up with the team.

3rd error – you're trying to open profile page (/#/profile in url forces the example app to render profile page first). By that time currentUser is still null and signInAnonymously haven't executed yet (could be fixed with await FirebaseAuth.instance.authStateChanges().first, so you're trying to open the profile page without an authenticated user. Ideally there should be a check in build method that verifies profile is never opened without an authenticated user.

By that time currentUser is still null

this seems to be an issue of the firebase_auth as well, communicating with the team on that.

cupidchan commented 1 year ago

Thanks a lot for your reply and assessment @lesnitsky I tried a few more combinations but still not be able to set up the sample code working with Emulator.

  1. My actual app doesn't need signInAnonymously but i found out that the firebase_ui doesn't work without it. So, I keep this as what the sample app has. I am good without it if there is a way to skip.
  2. As you can see that the Emulator link redirect a success verification to /#/profile, how can I set up the universal logic in main so that it can initiate Firebase properly to use the emulator in all routes, such as /#, /#/profile, /#/verify-email... etc. Right now, the sample code works only the very first time when the app starts in `/#' but fails in all other routes as it will point to the live server instead of local emulator.
  3. The link provided by the emulator is to verify the user email. And I can confirm that calling that link has successfully verified the email on the Firebase emulator. Therefore, by the time /#/profile is called, the currentUser should not be null but with the email-verified user.

Thanks!

lesnitsky commented 1 year ago

This issue is blocked by https://github.com/firebase/flutterfire/issues/10200

lesnitsky commented 1 year ago

Thanks a lot for your reply and assessment @lesnitsky I tried a few more combinations but still not be able to set up the sample code working with Emulator.

  1. My actual app doesn't need signInAnonymously but i found out that the firebase_ui doesn't work without it. So, I keep this as what the sample app has. I am good without it if there is a way to skip.

it does.

  1. As you can see that the Emulator link redirect a success verification to /#/profile, how can I set up the universal logic in main so that it can initiate Firebase properly to use the emulator in all routes, such as /#, /#/profile, /#/verify-email... etc. Right now, the sample code works only the very first time when the app starts in `/#' but fails in all other routes as it will point to the live server instead of local emulator.

This functionality is currently broken and tracked under firebase/flutterfire#10200

  1. The link provided by the emulator is to verify the user email. And I can confirm that calling that link has successfully verified the email on the Firebase emulator. Therefore, by the time /#/profile is called, the currentUser should not be null but with the email-verified user.

It shouldn't, a workaround would be to wait for FirebaseAuth.instance.authStateChanges().first, but this would call a real server, not emulator, again firebase/flutterfire#10200

cupidchan commented 1 year ago

Thanks @lesnitsky! Do you think fixing firebase/flutterfire#10200 will also fix this issue automagically so that the same will work as it should? Or there are still something on the UI side required to do?

just to clarify: do you mean the UI should still work even without signInAnonymously?

lesnitsky commented 1 year ago

nothing will be required from the UI side

On Mon, Jan 30, 2023, 4:06 PM Cupid Chan @.***> wrote:

Thanks @lesnitsky https://github.com/lesnitsky! Do you think fixing firebase/flutterfire#10200 https://github.com/firebase/flutterfire/issues/10200 will also fix this issue automagically so that the same will work as it should? Or there are still something on the UI side required to do?

β€” Reply to this email directly, view it on GitHub https://github.com/firebase/FirebaseUI-Flutter/issues/44, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABPYUNQN5QIIWY4BLJ5YBTDWU7KIPANCNFSM6AAAAAAUEGD3LY . You are receiving this because you were mentioned.Message ID: @.***>

lesnitsky commented 1 year ago

as of Flutter 3.13.1 β€’ channel stable and firebase_ui_auth: 1.7.0 this is no longer reproducible.