Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Firebase Auth, how to detect that an anonymous user was UPGRADED to an e.g. Facebook user?

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 Firebase-UI 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?

Note: I know its possible to detect what auth provider (id) a user is using to authenticate. Here is a bit of code I use for this:

val FirebaseUser.loginType: LoginType

get() {
    if (isAnonymous) {
        return LoginType.Anonymous
    }
loop@ for (userInfo in providerData) {
    when (userInfo.providerId) {
        EmailAuthProvider.PROVIDER_ID -> return LoginType.Email
        GoogleAuthProvider.PROVIDER_ID -> return LoginType.Google
        FacebookAuthProvider.PROVIDER_ID -> return LoginType.Facebook
        else -> continue@loop
    }
}
return LoginType.Unknown

}

That is not my issue, my questions is: how to detect that a user has been upgraded from anonymous to e.g. Facebook?

like image 288
HixField Avatar asked Oct 16 '25 15:10

HixField


1 Answers

how to detect that a user has been upgraded from anonymous to e.g. Facebook?

When an anonymous user signs in for the first time, save the authentication type in the database:

Firestore-root
  |
  --- users (collection)
        |
        --- uid
             |
             --- type: "anonymous"

When a user is changing the authentication type, you should simply change the type field in the database to hold "Facebook" instead of "anonymous". This is possible because the uid will always be the same, no matter what the provider id. To be notified when the operation takes place, simply attach a listener on the type property and you'll be notified in real-time.

like image 54
Alex Mamo Avatar answered Oct 18 '25 07:10

Alex Mamo