I have a singleton in my Android app, started at startup of the app, that listens to auth state changes like so:
fun listenForAuthChanges() {
if (authStateListener != null) {
FirebaseAuth.getInstance().removeAuthStateListener(authStateListener!!)
}
authStateListener = FirebaseAuth.AuthStateListener { auth ->
val user = auth.currentUser
writeStatusToFirebase()
if (user != null) {
Log.debug("User : ${user.uid} -> ${user.loginType}")
} else {
Log.debug("User : signed out")
loginAnonymously()
}
}
FirebaseAuth.getInstance().addAuthStateListener(authStateListener!!)
}
This works perfectly, detecting if a use is logged out, or logged in.
As you can see in the code above using the loginAnonymously()
, when there is no user logged-in then I automatically login anonymously.
This al works like a charm, however.... when I call the FirebaseUI to login and the user logs in via Facebook the Auth state listener is not called.
I figured out that FirebaseUI actually does not create a new user, instead the anonymous user is upgraded to Facebook user (checked in the Firebase Auth console and by using breakpoints in the Android studio console). This is actually the behaviour that I want.
So I guess, the conclusion is that the Auth state listener is not called because the user's uid does not change?
However, I do need a reliable way to detect also this event (meaning user upgraded from anonymous to e.g. Facebook).
What would be the best way to do this?