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 ] User auth state not persisted through sessions #9199

Closed OutdatedGuy closed 2 years ago

OutdatedGuy commented 2 years ago

Bug report

Describe the bug Since I upgraded to firebase_auth: ^3.5.0, the user auth state is not stored and is changing for each session and refresh. I'm using anonymous signin which was working perfectly before the upgrade.

Expected behavior

User auth state persists throughout sessions

Sample project

https://github.com/OutdatedGuy/Whats-That-Anime


Additional context

In sample project with firebase_auth: ^3.3.19, everything works fine, but for firebase_auth: ^3.5.0 the problem occurs


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` [√] Flutter (Channel stable, 3.0.5, on Microsoft Windows [Version 10.0.22622.436], locale en-IN) Checking Android licenses is taking an unexpectedly long time...[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1) [√] Chrome - develop for the web [√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.2.3) [√] Android Studio (version 2021.2) [√] VS Code (version 1.69.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.6 Flutter SDK 3.0.5 whats_that_anime 2.1.0+4 dependencies: - cached_network_image 3.2.1 [flutter flutter_cache_manager octo_image cached_network_image_platform_interface cached_network_image_web] - chewie 1.3.4 [cupertino_icons flutter provider video_player wakelock] - cloud_firestore 3.4.0 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta] - cupertino_icons 1.0.5 - desktop_drop 0.3.3 [flutter flutter_web_plugins cross_file] - dotted_border 2.0.0+2 [flutter path_drawing] - firebase_auth 3.5.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_storage 10.3.3 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter] - flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine] - flutter_easyloading 3.0.5 [flutter flutter_spinkit] - flutter_native_splash 2.2.5 [args flutter flutter_web_plugins image js lint meta path universal_io xml yaml] - get_storage 2.0.3 [flutter get path_provider] - http 0.13.4 [async http_parser meta path] - image_picker 0.8.5+3 [flutter image_picker_android image_picker_for_web image_picker_ios image_picker_platform_interface] - internet_connection_checker_plus 1.0.1 [flutter http] - motion_toast 2.3.0 [flutter] - package_info_plus 1.4.3 [flutter package_info_plus_platform_interface package_info_plus_linux package_info_plus_macos package_info_plus_windows package_info_plus_web] - paginate_firestore 1.0.3 [flutter bloc flutter_bloc cloud_firestore provider] - pasteboard 0.2.0 [flutter flutter_web_plugins js] - url_launcher 6.1.5 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] - video_player 2.4.5 [flutter html video_player_android video_player_avfoundation video_player_platform_interface video_player_web] dev dependencies: - change_app_package_name 1.1.0 - 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] - icons_launcher 1.2.1 [args image path universal_io yaml] transitive dependencies: - archive 3.3.1 [crypto path] - args 2.3.1 - async 2.8.2 [collection meta] - bloc 7.2.1 [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 - cloud_firestore_platform_interface 5.7.0 [collection firebase_core flutter meta plugin_platform_interface] - cloud_firestore_web 2.8.0 [cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js] - collection 1.16.0 - cross_file 0.3.3+1 [js meta] - crypto 3.0.2 [typed_data] - csslib 0.17.2 [source_span] - fake_async 1.3.0 [clock collection] - ffi 2.0.1 - file 6.1.2 [meta path] - firebase_auth_platform_interface 6.4.0 [collection firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 4.1.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_storage_platform_interface 4.1.12 [collection firebase_core flutter meta plugin_platform_interface] - firebase_storage_web 3.3.1 [async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http js meta] - flutter_bloc 7.3.3 [flutter bloc provider] - 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_plugin_android_lifecycle 2.0.7 [flutter] - flutter_spinkit 5.1.0 [flutter] - flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math] - get 4.6.5 [flutter] - html 0.15.0 [csslib source_span] - http_parser 4.0.1 [collection source_span string_scanner typed_data] - image 3.2.0 [archive meta xml] - image_picker_android 0.8.5+1 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface] - image_picker_for_web 2.1.8 [flutter flutter_web_plugins image_picker_platform_interface] - image_picker_ios 0.8.5+6 [flutter image_picker_platform_interface] - image_picker_platform_interface 2.5.0 [cross_file flutter http plugin_platform_interface] - intl 0.17.0 [clock path] - js 0.6.4 - lint 1.8.2 - lints 2.0.0 - matcher 0.12.11 [stack_trace] - material_color_utilities 0.1.4 - meta 1.7.0 - 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 2.0.0 [package_info_plus_platform_interface ffi flutter win32] - path 1.8.1 - path_drawing 1.0.0 [vector_math meta path_parsing flutter] - path_parsing 1.0.0 [vector_math meta] - 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.16 [flutter path_provider_platform_interface] - path_provider_ios 2.0.10 [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] - pedantic 1.11.1 - petitparser 5.0.0 [meta] - platform 3.1.0 - plugin_platform_interface 2.1.2 [meta] - process 4.2.4 [file path platform] - provider 6.0.3 [collection flutter nested] - rxdart 0.27.5 - sky_engine 0.0.99 - source_span 1.8.2 [collection path term_glyph] - sqflite 2.0.3 [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] - universal_io 2.0.4 [collection crypto meta typed_data] - 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.1.0 [flutter plugin_platform_interface] - url_launcher_web 2.0.12 [flutter flutter_web_plugins url_launcher_platform_interface] - url_launcher_windows 3.0.1 [flutter url_launcher_platform_interface] - uuid 3.0.6 [crypto] - vector_math 2.1.2 - video_player_android 2.3.8 [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.12 [flutter flutter_web_plugins video_player_platform_interface] - wakelock 0.6.2 [flutter meta wakelock_macos wakelock_platform_interface wakelock_web wakelock_windows] - wakelock_macos 0.4.0 [flutter flutter_web_plugins wakelock_platform_interface] - wakelock_platform_interface 0.3.0 [flutter meta] - wakelock_web 0.4.0 [flutter flutter_web_plugins js wakelock_platform_interface] - wakelock_windows 0.2.0 [flutter wakelock_platform_interface win32] - win32 2.7.0 [ffi] - xdg_directories 0.2.0+1 [meta path process] - xml 6.1.0 [collection meta petitparser] - yaml 3.1.1 [collection source_span string_scanner] ```

stx commented 2 years ago

We're also experiencing this on the web, which is a dealbreaker. Are you experiencing this on mobile, web or both?

In conjunction with https://github.com/firebase/flutterfire/issues/9106 and https://github.com/firebase/flutterfire/issues/9124 and https://github.com/firebase/flutterfire/issues/9195, amongst others, we had to rollback the entire latest Flutterfire release since they interconnect.

Here is the only working pubspec configuration we've found:

  cloud_firestore: 3.2.1
  cloud_firestore_platform_interface: 5.5.10
  cloud_firestore_web: 2.6.19
  cloud_functions: 3.3.1
  cloud_functions_platform_interface: 5.1.10
  cloud_functions_web: 4.2.18
  firebase_analytics: 9.1.12
  firebase_analytics_platform_interface: 3.1.10
  firebase_analytics_web: 0.4.0+17
  firebase_auth: 3.4.1
  firebase_auth_platform_interface: 6.3.1
  firebase_auth_web: 3.3.19
  firebase_core: 1.19.1
  firebase_core_platform_interface: 4.4.3
  firebase_core_web: 1.6.6
  firebase_dynamic_links: 4.3.1
  firebase_dynamic_links_platform_interface: 0.2.3+6
  firebase_messaging: 11.4.4
  firebase_messaging_platform_interface: 3.5.4
  firebase_messaging_web: 2.4.4
dinvlad commented 2 years ago

Seems like this is related to the switch from CDN version of Firebase JS libraries to the built-in version. Anything >= 3.4.2 doesn't preserve anonymous sessions on page reload. While versions <= 3.4.1 require manually injected JS libraries, and work.

OutdatedGuy commented 2 years ago

We're also experiencing this on the web, which is a dealbreaker. Are you experiencing this on mobile, web or both?

Yes, I'm experiencing this only on web and not on android.


In conjunction with https://github.com/firebase/flutterfire/issues/9106

Similar to this issue, only on web, uploading file using firebase_storage throws some random error but the file is uploaded in the end.

OutdatedGuy commented 2 years ago

Seems like this is related to the switch from CDN version of Firebase JS libraries to the built-in version. Anything >= 3.4.2 doesn't preserve anonymous sessions on page reload. While versions <= 3.4.1 require manually injected JS libraries, and work.

For that it seems there a function to persist auth state locally (on web only)

FirebaseAuth.instance.setPersistence(Persistence.LOCAL);

But instead of storing auth data in IndexedDB (like it usually does) it is using Local Storage for that.

dinvlad commented 2 years ago

Interestingly, I haven't noticed its use of Local Storage either. Otherwise, I think the session would persist on reload. Or do you mean this would be a workaround if we set persistence explicitly like that? Let me try..

dinvlad commented 2 years ago

OK, with this setting it does persist the session to local storage, but then on refresh still a new session is used (and persisted)..

OutdatedGuy commented 2 years ago

OK, with this setting it does persist the session to local storage, but then on refresh still a new session is used (and persisted)..

Yep, even if persistence is set, the state is lost on refresh.

dinvlad commented 2 years ago

Any ideas on what causes this loss?

Rexios80 commented 2 years ago

Same issue here

elmdecoste commented 2 years ago

We are experiencing this as well and it's causing us a lot of problems. All authentication state is lost on a reload of the application 😢

DmitrySboychakov commented 2 years ago

Same here.

darshankawar commented 2 years ago

@OutdatedGuy and everyone facing this issue, there's similar issue open describing your case, https://github.com/firebase/flutterfire/issues/9089, so please follow-up in it for further updates. There's an active PR to fix this issue that you can subscribe to: https://github.com/firebase/flutterfire/pull/9138 Closing this as a duplicate.