flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

🐛 [firebase_auth] FirebaseAuth.instance.currentUser initially null for the first few seconds initializing firebase on windows

Open Justus-M opened this issue 1 year ago • 32 comments

Bug report

Describe the bug

FirebaseAuth.instance.currentUser is always null when initializing firebase on windows, even if the user is logged in. It then changes to a non-null value within a few seconds.

Steps to reproduce

Steps to reproduce the behavior:

  1. Login with FirebaseAuth on flutter windows
  2. restart app and print FirebaseAuth.instance.currentUser immediately after initializing firebase
  3. See that the value is null initially, and then changes after about 20 seconds

Expected behavior

I expect the logged in user object to be shown on initialization, otherwise I have to add a delay (not sure how long it needs to be) or I don't know whether to show the login page or not. On mac and web, currentUser is immediately loaded on initialization.

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
PS C:\Users\justinmulli\blazesql_flutter> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.16.4, on Microsoft Windows [Version 10.0.17763.5122], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[X] Android toolchain - develop for Android devices
    X Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.2)
[!] Android Studio (not installed)
[√] VS Code (version 1.85.1)
[√] Connected device (3 available)
[√] Network resources

! Doctor found issues in 2 categories.
PS C:\Users\justinmulli\blazesql_flutter>```


---

Justus-M avatar Dec 19 '23 09:12 Justus-M

Hi @Justus-M, is this limited to only windows or do you experience it on other platforms as well?

Can you share the code sample you use as well?

danagbemava-nc avatar Dec 19 '23 11:12 danagbemava-nc

I haven't had time to create a minimum reproducible example but I imagine you can just use the example app and print FirebaseAuth.instance.currentUser immediately after initializing firebase.

I have added a delay of a few seconds and then it is no longer null, so to be honest this is no longer a priority for me. I believe it's an issue with the library so I wanted to bring it to your attention since flutterfire is still in the early stages for windows and has various issues.

But the code is literally just

print(FirebaseAuth.instance.currentUser)

This prints null on windows after initializing firebase, even if the user was logged in before closing the app.

On mac, web, and mobile it does not return null

Justus-M avatar Dec 23 '23 08:12 Justus-M

Hey @Justus-M. 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 avatar Jan 02 '24 02:01 google-oss-bot

I provided more information

Justus-M avatar Jan 02 '24 10:01 Justus-M

Sorry for the delay, it seems like the bot didn't remove the blocked label so I missed it in my notifications.

Reproducible using the plugin example app. I was already logged in, but the user came as null before the data was fully loaded.

cc @Lyokone

Launching lib\main.dart on Windows in debug mode...
CMake Deprecation Warning at C:/Users/Deana/Desktop/example/firebase_auth_example/build/windows/x64/extracted/firebase_cpp_sdk_windows/CMakeLists.txt:17 (cmake_minimum_required):
  Compatibility with CMake < 3.5 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


√  Built build\windows\x64\runner\Debug\example.exe.
Connecting to VM Service at ws://127.0.0.1:50047/3U53madUGIc=/ws
flutter: user: null
flutter: user: null
[ERROR:flutter/shell/common/shell.cc(1015)] The 'firebase_auth_plugin/auth-state/[DEFAULT]' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel.
See https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading for more information.
[ERROR:flutter/shell/common/shell.cc(1015)] The 'firebase_auth_plugin/id-token/[DEFAULT]' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel.
See https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading for more information.
flutter: user: User(displayName: , email: [email protected], isEmailVerified: false, isAnonymous: false, metadata: UserMetadata(creationTime: null, lastSignInTime: null), phoneNumber: , photoURL: , providerData, [UserInfo(displayName: , email: [email protected], phoneNumber: , photoURL: , providerId: password, uid: [email protected])], refreshToken: null, tenantId: null, uid: AumEL2awh0NyOgfw1m7C8nGrsiK2)
[log] User(displayName: , email: [email protected], isEmailVerified: false, isAnonymous: false, metadata: UserMetadata(creationTime: null, lastSignInTime: null), phoneNumber: , photoURL: , providerData, [UserInfo(displayName: , email: [email protected], phoneNumber: , photoURL: , providerId: password, uid: [email protected])], refreshToken: null, tenantId: null, uid: AumEL2awh0NyOgfw1m7C8nGrsiK2)

danagbemava-nc avatar Jan 02 '24 11:01 danagbemava-nc

Thanks for the report, I'm looking into it

Lyokone avatar Jan 04 '24 08:01 Lyokone

This happened on other platforms as well, tested on android

richard457 avatar Jan 26 '24 14:01 richard457

@Lyokone Is it possible that this also has an impact on the authentication for Firestore requests shortly after creating a new account?

I have the following issue: after creating an account with createUserWithEmailAndPassword on windows, I try to add a document to Firestore but the Firestore security rules reject the operation. It seems that even though FirebaseAuth.instance.currentUser is not null, firestore security rules don't recognize the user as being authenticated (the security rules check that the user is authenticated, and that the users id matches the document ID).

When I restart the app after creating the user (or log in with an existing user in the first place), it works fine.

Unfortunately I haven't been able to create a minimum reproducible example (for some reason I can't reproduce it in the test app), so I figured I'd ask you since you worked on a potentially related issue.

Justus-M avatar Feb 26 '24 11:02 Justus-M

I am seeing this issue too on Windows. After I sign in/anonymous sign in, hot restarting the app returns currentUser as null. This also happens on subsequent app runs too.

In my case, it is always null, even after few seconds. I tried this:

 FirebaseAuth.instance.authStateChanges().listen((event) {
      print('Auth state changed: $event');
    });

and it only prints null and never prints again.

BirjuVachhani avatar Apr 10 '24 10:04 BirjuVachhani

someone solved the problem ?

Hmida71 avatar May 01 '24 17:05 Hmida71

I am seeing this issue too on Windows. After I sign in/anonymous sign in, hot restarting the app returns currentUser as null. This also happens on subsequent app runs too.

In my case, it is always null, even after few seconds. I tried this:

 FirebaseAuth.instance.authStateChanges().listen((event) {
      print('Auth state changed: $event');
    });

and it only prints null and never prints again.

did you find solution ?

Hmida71 avatar May 01 '24 17:05 Hmida71

I haven't. This makes our app useless on Windows! 🥲

BirjuVachhani avatar May 02 '24 06:05 BirjuVachhani

I haven't. This makes our app useless on Windows! 🥲

u can make delay its work but still bad !

Hmida71 avatar May 02 '24 13:05 Hmida71

I haven't. This makes our app useless on Windows! 🥲

u can make delay its work but still bad !

I can't. Its not that it is only null for the first few seconds...it stays null. So it always logs out users when reopened.

BirjuVachhani avatar May 03 '24 06:05 BirjuVachhani

I haven't. This makes our app useless on Windows! 🥲

u can make delay its work but still bad !

I can't. Its not that it is only null for the first few seconds...it stays null. So it always logs out users when reopened.

try this : void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); FirebaseAuth.instance.authStateChanges().listen( (event) { if (event == null) { isLogin = false; } else { isLogin = true; } }, );

await Future.delayed((const Duration(seconds: 2))).then((value) { final user = FirebaseAuth.instance.currentUser; if (user == null) { isLogin = false; } else { isLogin = true; } }); print(isLogin); runApp( .....

Hmida71 avatar May 03 '24 14:05 Hmida71

Are there any updates on this? I'm experiencing this too.

remcova avatar Aug 06 '24 18:08 remcova

No updates as far as I'm aware, except that I lost several customers because of it

Justus-M avatar Aug 06 '24 18:08 Justus-M

No updates as far as I'm aware, except that I lost several customers because of it

Yeah same here. I'm trying to find a solution at the moment. I'll post it here once I find it.

remcova avatar Aug 06 '24 19:08 remcova

@Justus-M do you use a YouTube scope by any chance for authentication?

remcova avatar Aug 08 '24 14:08 remcova

No, I had users encountering this issue with a normal email and password login

On Thu 8. Aug 2024 at 16:43, Remco van Akker @.***> wrote:

@Justus-M https://github.com/Justus-M do you use a YouTube scope by any chance for authentication?

— Reply to this email directly, view it on GitHub https://github.com/firebase/flutterfire/issues/12055#issuecomment-2276007722, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI2UQLB5IIHR5BTRT5Q45LTZQN72LAVCNFSM6AAAAABA22MTZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGAYDONZSGI . You are receiving this because you were mentioned.Message ID: @.***>

Justus-M avatar Aug 08 '24 15:08 Justus-M

Okay. I think I found a solution. It's the way you handle authentication. My current implementation that works now, looks like this: await FirebaseAuth.instance.signInWithCredential(credential).whenComplete(() async { final user = FirebaseAuth.instance.currentUser; // do something with user });

Before I did it like this: FirebaseAuth.instance.signInWithCredential(credential); FirebaseAuth.instance.authStateChanges().listen((User? user) async { // do something with the user object }

I hope this solves the issue for you too!

No, I had users encountering this issue with a normal email and password login On Thu 8. Aug 2024 at 16:43, Remco van Akker @.> wrote: @Justus-M https://github.com/Justus-M do you use a YouTube scope by any chance for authentication? — Reply to this email directly, view it on GitHub <#12055 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI2UQLB5IIHR5BTRT5Q45LTZQN72LAVCNFSM6AAAAABA22MTZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGAYDONZSGI . You are receiving this because you were mentioned.Message ID: @.>

remcova avatar Aug 08 '24 16:08 remcova

I was never adding a listener in the first place, your issue probably wasn’t network related like everyone else in this thread from the sounds of it

On Thu 8. Aug 2024 at 18:03, Remco van Akker @.***> wrote:

Okay. I think I found a solution. It's the way you handle authentication. My current implementation that works now, looks like this: await FirebaseAuth.instance.signInWithCredential(credential).whenComplete(() async { final user = FirebaseAuth.instance.currentUser; // do something with user });

Before I did it like this: FirebaseAuth.instance.signInWithCredential(credential); FirebaseAuth.instance.authStateChanges().listen((User? user) async { // do something with the user object }

I hope this solves the issue for you too!

No, I had users encountering this issue with a normal email and password login … <#m_5950327318018341650_> On Thu 8. Aug 2024 at 16:43, Remco van Akker @.> wrote: @Justus-M https://github.com/Justus-M https://github.com/Justus-M https://github.com/Justus-M do you use a YouTube scope by any chance for authentication? — Reply to this email directly, view it on GitHub <#12055 (comment) https://github.com/firebase/flutterfire/issues/12055#issuecomment-2276007722>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI2UQLB5IIHR5BTRT5Q45LTZQN72LAVCNFSM6AAAAABA22MTZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGAYDONZSGI https://github.com/notifications/unsubscribe-auth/AI2UQLB5IIHR5BTRT5Q45LTZQN72LAVCNFSM6AAAAABA22MTZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGAYDONZSGI . You are receiving this because you were mentioned.Message ID: @.>

— Reply to this email directly, view it on GitHub https://github.com/firebase/flutterfire/issues/12055#issuecomment-2276177979, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI2UQLHZJZ7XYWGRBS7YWWLZQOJF7AVCNFSM6AAAAABA22MTZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGE3TOOJXHE . You are receiving this because you were mentioned.Message ID: @.***>

Justus-M avatar Aug 08 '24 16:08 Justus-M

This issue makes Firebase useless because Windows app is un-usable. Can someone please look into this? @Lyokone

BirjuVachhani avatar Aug 10 '24 04:08 BirjuVachhani

having same issue

venky9885 avatar Aug 13 '24 16:08 venky9885

How do you handle the authentication then? I'm saying that I'm not experiencing this issue anymore.

I was never adding a listener in the first place, your issue probably wasn’t network related like everyone else in this thread from the sounds of it On Thu 8. Aug 2024 at 18:03, Remco van Akker @.> wrote: Okay. I think I found a solution. It's the way you handle authentication. My current implementation that works now, looks like this: await FirebaseAuth.instance.signInWithCredential(credential).whenComplete(() async { final user = FirebaseAuth.instance.currentUser; // do something with user }); Before I did it like this: FirebaseAuth.instance.signInWithCredential(credential); FirebaseAuth.instance.authStateChanges().listen((User? user) async { // do something with the user object } I hope this solves the issue for you too! No, I had users encountering this issue with a normal email and password login … <#m_5950327318018341650_> On Thu 8. Aug 2024 at 16:43, Remco van Akker @.> wrote: @Justus-M https://github.com/Justus-M https://github.com/Justus-M https://github.com/Justus-M do you use a YouTube scope by any chance for authentication? — Reply to this email directly, view it on GitHub <#12055 (comment) <#12055 (comment)>>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI2UQLB5IIHR5BTRT5Q45LTZQN72LAVCNFSM6AAAAABA22MTZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGAYDONZSGI https://github.com/notifications/unsubscribe-auth/AI2UQLB5IIHR5BTRT5Q45LTZQN72LAVCNFSM6AAAAABA22MTZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGAYDONZSGI . You are receiving this because you were mentioned.Message ID: @.> — Reply to this email directly, view it on GitHub <#12055 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI2UQLHZJZ7XYWGRBS7YWWLZQOJF7AVCNFSM6AAAAABA22MTZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGE3TOOJXHE . You are receiving this because you were mentioned.Message ID: @.**>

remcova avatar Aug 13 '24 19:08 remcova