0

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?

HixField
  • 3,538
  • 1
  • 28
  • 54
  • Check against "Facebook" provider, as explained in the duplicate. – Alex Mamo Apr 13 '20 at 07:53
  • @AlexMamo the question you refer to has absolutely nothing to do with my question. I am looking to get an sort of event to know this happened. I'm already using the code to checking for provider id but this is not event based. Can you please undo this and reopen my question? – HixField Apr 13 '20 at 07:56
  • @AlexMamo I rephrased my question and asked it again : https://stackoverflow.com/questions/61183783/in-firebase-auth-how-to-detect-that-an-anonymous-user-was-upgraded-to-an-e-g-f – HixField Apr 13 '20 at 08:07

0 Answers0