-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[🐛] Android - onAuthStateChanged does not fire when I refresh the app from the metro terminal by pressing R #7819
Comments
Hello 👋, to help manage issues we automatically close stale issues. This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?
Thank you for your contributions. |
Experiencing the same thing in an Expo managed project.
|
Facing the same issue. Using Expo SDK 51.
|
Same here with
|
Facile the same in expo 51, it used to work well before upgrading to :
|
Same, fresh install. iOS working fine. Android first load works, press R in terminal to refresh and onAuthStateChanged is never entered. |
Same here but without expo "@react-native-firebase/app": "^21.0.0", |
same, onAuthStateChange not work when i press R
|
i am also facing the same issue |
Hmm - do you see this log tag in Line 128 in 789d41b
And then after do you see any of these re-registering? Line 182 in 789d41b
Curious what react-native version and style (new architecture or old architecture) people are running that are seeing this? Because react-native-firebase still needs to forward-port to |
hey @mikehardy i can provide you info on what react native version i am using, and to answer your above question on refresh also Here is what i got from adb logcat
this is how i use it |
I just tried react-native 0.76.2 and react-native-firebase 21.4.0 from make-demo.sh against the firebase emulator auth instance and I was unable to reproduce. I double-tap Start with this https://github.com/mikehardy/rnfbdemo/blob/main/make-demo.sh Apply this diff to App.tsx for testing
diff --git a/App.tsx b/App.tsx
index 69e2500..48b231b 100644
--- a/App.tsx
+++ b/App.tsx
@@ -8,6 +8,7 @@
import React from 'react';
import type {PropsWithChildren} from 'react';
import {
+ Button,
SafeAreaView,
ScrollView,
StatusBar,
@@ -23,7 +24,7 @@ import firebase from '@react-native-firebase/app';
import analytics from '@react-native-firebase/analytics';
import appCheck from '@react-native-firebase/app-check';
import appDistribution from '@react-native-firebase/app-distribution';
-import auth from '@react-native-firebase/auth';
+import auth, { FirebaseAuthTypes } from '@react-native-firebase/auth';
import crashlytics from '@react-native-firebase/crashlytics';
import database from '@react-native-firebase/database';
import dynamicLinks from '@react-native-firebase/dynamic-links';
@@ -79,6 +80,12 @@ function App(): JSX.Element {
},
});
+ firebase.auth().useEmulator('http://localhost:9099');
+
+ firebase.auth().onAuthStateChanged((user) => {
+ console.log('onAuthStateChanged was called with user ' + user);
+ });
+
return (
<SafeAreaView style={backgroundStyle}>
<StatusBar
@@ -88,6 +95,8 @@ function App(): JSX.Element {
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={backgroundStyle}>
+ <Button title="sign in" onPress={async () => { console.log('anonymous sign in '); await firebase.auth().signInAnonymously()}} />
+ <Button title="sign out" onPress={async () => { console.log('signing out'); await firebase.auth().signOut(); }} />
<View
style={{
backgroundColor: isDarkMode ? Colors.black : Colors.white, |
perhaps the effect you are using is not firing on reload? I'm not sure, but at least if you just register the listener at app startup every time (as you should do with something as basic as your event listeners?) it would work ? |
the effect is atually firing. i have added console.log to see it, it is even reaching auth() but then i don't know why is it not firing |
Note I linked a PR here that has a title indicating it might fix this, it will not. The "fix" in that PR is to correctly teardown on hot reload as the react host goes away - it has nothing to do with setup. So it shouldn't have bearing on this issue but I noticed it while looking into this one |
I will tell you what is exactly happening and you can confirm if it is with reloading. While doing the process below at no time will we signin the user so user will always be null. When the app loads for the first time, everything works as expected i.e onAuthStateChanged Listener fires up with user = null. Now the app is loaded. Then we do not do anything and just press R (do not signin the user) on the metro terminal and it reloads. As per your checks I get the log instance-initialize and addAuthStateListener addAuthStateListener with event-body do not fire So is it supposed to be like this that since there is no change in auth state after reload it will not fire again. Is it something like that. Also I am not very familiar with native code so do not know how invariant is related to hot-reloading. If you need any other info I can provide |
If you don't change the user you won't see a handler called, and hot reloading doesn't change the user, so no handler called. This is as expected. As you signInAnonymously (or some other way, for testing) and signOut, you'll see the listener called, because now the user is changing. And for me at least, that continued to happen even after a reload, so I couldn't reproduce what the issue alleged - "onAuthStateChanged does not fire when I refresh the app from the metro terminal by pressing R". Well it doesn't fire immediately but the listener isn't dead or anything, it will fire when there are changes. That should be sufficient? |
Hey @mikehardy . First of all thank you for the clarification and thank you for all of your time. in this case the onAuthStateListener fires up with user null everytime i reload even when i do not change any state LOG base url: http://192.168.1.2:8000 info Reloading connected app(s)... is this behaviour changed in the new architecture or am i missing something here ? |
I've been using this library for years it's def a new (and unwanted) behavior because then idk what the user state is after app startup if this handler never gets called. Not called = potentially still intializing, called with null = initialized and actually not signed in
…-------- Original Message --------
On 11/16/24 8:00 AM, Sagnik wrote:
Hey ***@***.***(https://github.com/mikehardy) . First of all thank you for the clarification and thank you for all of your time.
the thing is I have one of my app that is on react native 0.73.1 and it uses
***@***.***/app": "^18.7.3",
***@***.***/auth": "^18.7.3",
***@***.***/messaging": "^18.8.0",
***@***.***/storage": "^18.7.3",
in this case the onAuthStateListener fires up with user null everytime i reload even when i do not change any state
LOG base url: http://192.168.1.2:8000
LOG env : development
LOG user null
info Reloading connected app(s)...
BUNDLE ./index.js
LOG user null
LOG base url: http://192.168.1.2:8000
LOG env : development
is this behaviour changed in the new architecture or am i missing something here ?
—
Reply to this email directly, [view it on GitHub](#7819 (comment)), or [unsubscribe](https://github.com/notifications/unsubscribe-auth/ACBA277NDR27IOEZQOI3SN32A27NZAVCNFSM6AAAAABIUQCZAWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIOBQGM4DCOBTHE).
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
I see - thanks for the clarification that it was really focused on that initial state update, I did not have that clear in my mind, and honestly didn't really understand that part of this listeners feature set until you clarified that In that case, the bug I fixed yesterday with #8139 really did fix this, and I confirm it by using the patches from the PR here: https://github.com/invertase/react-native-firebase/actions/runs/11862072310
So what was happening was:
With the patches from the PR the old behavior was restored, we are now correctly tearing down the old listener set on a hot-reload, so after the reload when you add a listener it is the first one and you get that initial state update Additionally, this conforms (again) with the documentation https://rnfirebase.io/auth/usage#listening-to-authentication-state
...plus it eliminates a platform difference since iOS was still behaving like android was in react-native <= 0.73 Didn't realize how important that initial state was at first, but it really was important between the documentation, the pre-0.74 behavior, and the platform difference! react-native-firebase 21.5.0 will be out shortly and it should have this fix (along with new feature of firestore sum and avg aggregate queries!) |
I was curious so I probed the case of multiple listeners also - the PR fixed them as well What I observed was that without handling the onCatalystInstanceDestroy deprecation there were no initial state callbacks on listeners added to With the patches, before or after a hot-reload, any time a listener was added it got an initial state callback. So should be truly fixed |
Thank you so much @mikehardy for taking time out to fix this. Really appreciate your help 🔥. |
I was having the same issue yesterday, until I find this. For my luck @mikehardy fixed just in time lol. |
Issue
So
auth.onAuthStateChanged()
works fine when I open the app in emulator. However, when I press R on the metro terminal the app reloads butauth.onAuthStateChanged()
does not fire. It works fine for iOS.Is this an expected behaviour? I am using react native CLI
My versions are -
"@react-native-firebase/app": "^20.0.0", "@react-native-firebase/auth": "^20.0.0", "react": "18.2.0", "react-native": "0.74.1"
My JS:
Closing and reopening the app shows Login but when I press R I see Loading only
The text was updated successfully, but these errors were encountered: