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

šŸ› [firebase_auth] Authentication does not persist in flutter web #9241

Closed cruftmonger closed 2 years ago

cruftmonger commented 2 years ago

Bug report

A flutter web app will lose its authentication state efter page reload.

Steps to reproduce

Run a simple app (see gist) that:

  1. Listens to the FirebaseAuth.instance.authStateChanges()
  2. Have a button invoke FirebaseAuth.instance.signInAnonymously() (or any other auth method)
  3. Render FirebaseAuth.instance.currentUser.uid to verify that the user is logged in.
  4. Reload the page and note that the next User on the stream is null.

Expected behavior

That the authentication state be persisted as described in the documentation.

Sample project

A minimal example


Additional context

Verified in Chrome as well as Safari


Flutter doctor

Click To Expand ``` āœ— flutter doctor Doctor summary (to see all details, run flutter doctor -v): [āœ“] Flutter (Channel beta, 3.3.0-0.0.pre, on macOS 13.0 22A5295i darwin-arm, locale en-SE) [!] Android toolchain - develop for Android devices (Android SDK version 32.0.0) ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses [āœ“] Xcode - develop for iOS and macOS (Xcode 13.4.1) [āœ“] Chrome - develop for the web [āœ“] Android Studio (version 2021.2) [āœ“] IntelliJ IDEA Ultimate Edition (version 2022.2) [āœ“] VS Code (version 1.68.0-insider) [āœ“] Connected device (2 available) [āœ“] HTTP Host Availability ! Doctor found issues in 1 category. ```

Flutter dependencies

Click To Expand ``` Dart SDK 2.18.0-271.4.beta Flutter SDK 3.3.0-0.1.pre bug_9241 1.0.0+1 dependencies: - beamer 1.5.0 [flutter flutter_web_plugins] - bloc 8.0.3 [meta] - cache 1.0.0 - cloud_firestore 3.4.1 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta] - cloud_firestore_web 2.8.1 [cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js] - cupertino_icons 1.0.5 - equatable 2.0.3 [collection meta] - exif 3.1.2 [args collection convert json_annotation sprintf] - firebase_auth 3.6.0 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 1.20.0 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_messaging 12.0.1 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta] - firebase_messaging_web 3.1.0 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta] - firebase_storage 10.3.4 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter] - firebase_storage_web 3.3.2 [async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http js meta] - flow_builder 0.0.9 [flutter] - flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine] - flutter_bloc 8.0.1 [flutter bloc provider] - flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math] - form_inputs 1.0.0+1 [formz] - formz 0.4.1 - google_fonts 3.0.1 [flutter http path_provider crypto] - google_maps_flutter 2.1.10 [flutter flutter_plugin_android_lifecycle google_maps_flutter_platform_interface] - heic_to_jpg 0.2.0 [flutter] - image 3.2.0 [archive meta xml] - image_cropper 2.0.3 [flutter image_cropper_platform_interface image_cropper_for_web] - image_picker 0.8.4+6 [flutter flutter_plugin_android_lifecycle image_picker_for_web image_picker_platform_interface] - layout 1.0.1 [gap flutter] - meta 1.8.0 - provider 6.0.3 [collection flutter nested] - 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] - sqflite 2.0.3 [flutter sqflite_common path] - user_repository 0.0.0 [equatable uuid] - uuid 3.0.6 [crypto] - vector_math 2.1.2 - yaml 3.1.1 [collection source_span string_scanner] dev dependencies: - bloc_test 9.0.3 [bloc diff_match_patch meta mocktail test] - fake_cloud_firestore 1.2.4 [flutter cloud_firestore cloud_firestore_platform_interface collection plugin_platform_interface quiver rxdart] - firebase_storage_mocks 0.5.1 [flutter firebase_storage] - flutter_driver 0.0.0 [file flutter flutter_test fuchsia_remote_debug_protocol path meta vm_service webdriver archive async boolean_selector characters clock collection crypto matcher material_color_utilities platform process source_span stack_trace stream_channel string_scanner sync_http term_glyph test_api typed_data vector_math] - 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] - integration_test 0.0.0 [flutter flutter_driver flutter_test path vm_service archive async boolean_selector characters clock collection crypto fake_async file matcher material_color_utilities meta source_span stack_trace stream_channel string_scanner sync_http term_glyph test_api typed_data vector_math webdriver] - mockito 5.2.0 [analyzer build code_builder collection dart_style matcher meta path source_gen test_api] - mutex 3.0.0 - 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] - testreport 2.0.1 transitive dependencies: - _fe_analyzer_shared 43.0.0 [meta] - analyzer 4.3.1 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml] - archive 3.3.0 [crypto path] - args 2.3.1 - async 2.9.0 [collection meta] - boolean_selector 2.1.0 [source_span string_scanner] - build 2.3.0 [analyzer async convert crypto glob logging meta path] - built_collection 5.1.1 - built_value 8.4.0 [built_collection collection fixnum meta] - characters 1.2.1 - clock 1.1.1 - cloud_firestore_platform_interface 5.7.0 [collection firebase_core flutter meta plugin_platform_interface] - code_builder 4.2.0 [built_collection built_value collection matcher meta] - collection 1.16.0 - convert 3.0.2 [typed_data] - coverage 1.5.0 [args logging package_config path source_maps stack_trace vm_service] - cross_file 0.3.3+1 [js meta] - crypto 3.0.2 [typed_data] - dart_style 2.2.3 [analyzer args path pub_semver source_span] - diff_match_patch 0.4.1 - fake_async 1.3.1 [clock collection] - ffi 2.0.1 - file 6.1.2 [meta path] - firebase_auth_platform_interface 6.5.0 [collection firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 4.2.0 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta] - firebase_core_platform_interface 4.5.0 [collection flutter flutter_test meta plugin_platform_interface] - firebase_core_web 1.7.1 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_messaging_platform_interface 4.1.0 [firebase_core flutter meta plugin_platform_interface] - firebase_storage_platform_interface 4.1.12 [collection firebase_core flutter meta plugin_platform_interface] - fixnum 1.0.1 - flutter_plugin_android_lifecycle 2.0.7 [flutter] - frontend_server_client 2.1.3 [async path] - fuchsia_remote_debug_protocol 0.0.0 [process vm_service file meta path platform] - gap 2.0.0 [flutter] - glob 2.1.0 [async collection file path string_scanner] - google_maps_flutter_platform_interface 2.2.1 [collection flutter plugin_platform_interface stream_transform] - http 0.13.4 [async http_parser meta path] - http_multi_server 3.2.1 [async] - http_parser 4.0.1 [collection source_span string_scanner typed_data] - image_cropper_for_web 0.0.4 [flutter flutter_web_plugins image_cropper_platform_interface js] - image_cropper_platform_interface 2.0.0 [flutter plugin_platform_interface http] - image_picker_for_web 2.1.8 [flutter flutter_web_plugins image_picker_platform_interface] - image_picker_platform_interface 2.6.0 [cross_file flutter http plugin_platform_interface] - intl 0.17.0 [clock path] - io 1.0.3 [meta path string_scanner] - js 0.6.4 - json_annotation 4.6.0 [meta] - logging 1.0.2 - matcher 0.12.12 [stack_trace] - material_color_utilities 0.1.5 - mime 1.0.2 - mocktail 0.3.0 [collection matcher test] - nested 1.0.0 [flutter] - node_preamble 2.0.1 - package_config 2.1.0 [path] - path 1.8.2 - 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] - path_provider_android 2.0.17 [flutter path_provider_platform_interface] - path_provider_ios 2.0.11 [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.1.0 [ffi flutter path path_provider_platform_interface win32] - petitparser 5.0.0 [meta] - platform 3.1.0 - plugin_platform_interface 2.1.2 [meta] - pool 1.5.1 [async stack_trace] - process 4.2.4 [file path platform] - pub_semver 2.1.1 [collection meta] - quiver 3.1.0 [matcher] - rxdart 0.27.5 - 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] - shelf 1.3.1 [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.2 [shelf stream_channel web_socket_channel] - sky_engine 0.0.99 - source_gen 1.2.2 [analyzer async build dart_style glob meta path source_span yaml] - source_map_stack_trace 2.1.0 [path stack_trace source_maps] - source_maps 0.10.10 [source_span] - source_span 1.9.0 [collection path term_glyph] - sprintf 6.0.0 - sqflite_common 2.2.1+1 [synchronized path meta] - stack_trace 1.10.0 [path] - stream_channel 2.1.0 [async] - stream_transform 2.0.0 - string_scanner 1.1.1 [source_span] - sync_http 0.3.1 - synchronized 3.0.0+2 - 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] - vm_service 9.0.0 - watcher 1.0.1 [async path] - web_socket_channel 2.2.0 [async crypto stream_channel] - webdriver 3.0.0 [archive matcher path stack_trace sync_http] - webkit_inspection_protocol 1.1.0 [logging] - win32 2.7.0 [ffi] - xdg_directories 0.2.0+1 [meta path process] - xml 6.1.0 [collection meta petitparser] ```

kaumudpa commented 2 years ago

duplicate of #9089 .

A New version is just released - Please check and if ok close this issue.

darshankawar commented 2 years ago

Closing this as duplicate of https://github.com/firebase/flutterfire/issues/9089. Please upgrade to latest firebase_auth version.

cruftmonger commented 2 years ago

Upgraded tofirebase_auth 3.6.0. The problem remains. Updated the dependency listing. Everything else the same.

cruftmonger commented 2 years ago

The eminent @OutdatedGuy reported in a comment to https://github.com/firebase/flutterfire/issues/9243 that that problem was fixed in 3.6.1. It turns out that this fix also fixes this issue. All is well that ends well. It would have been nice with a heads-up here and in https://github.com/firebase/flutterfire/issues/9089

Thanks!

kaumudpa commented 2 years ago

The problem is not resolved even in firebase_auth 3.6.1 !

cruftmonger commented 2 years ago

It works for me now. Note that there will be a null event emitted on the FirebaseAuth.instance.authStateChanges() stream before the subsequent event with the restored User.

kaumudpa commented 2 years ago

@charafau Can you please explain how you resolved this? cause i am unable to make this work!

cruftmonger commented 2 years ago

I didn't do anything. They fixed the bug. The example I provided in my bug report now works. https://gist.github.com/cruftmonger/80b44d82d78a622110be41d7d68d2dc4