Android FirebaseUI Auth: Upgrade From Anonymous Account to Permanent Account

Assuming you are using firebase-ui-auth for handle signin flow, you can easy convert anonymous account to permanent account (new permanent account will share same uid as anonymouse account).

fun getAuthIntent(): Intent {    val currentUser = FirebaseAuth.getInstance().currentUser    val providers = if (currentUser == null) {        arrayListOf(            AuthUI.IdpConfig.GoogleBuilder().build(),            // AuthUI.IdpConfig.EmailBuilder().build(),            AuthUI.IdpConfig.AnonymousBuilder().build()        )    } else { // remove anonymous if already sign in        arrayListOf(            AuthUI.IdpConfig.GoogleBuilder().build()        )    }    val intent = AuthUI.getInstance()        .createSignInIntentBuilder()        .setAvailableProviders(providers)        // if current user is anonymous, the new signin will have the same uid        // if the signin is an existing user, a conflict will happen        .enableAnonymousUsersAutoUpgrade()        .build()    startActivityForResult(intent, REQUEST_SIGN_IN)}
class TestFragment : Fragment() {    companion object {        private const val REQUEST_SIGN_IN = 1    }    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {        super.onActivityResult(requestCode, resultCode, data)        if (requestCode == REQUEST_SIGN_IN) {            val response = IdpResponse.fromResultIntent(data)            if (resultCode == RESULT_OK) {                val user = FirebaseAuth.getInstance().currentUser                if (user != null) {                    Timber.d("name=${user.displayName}, email=${}, uid=${user.uid}")                }            }            else {                response?.also { response ->                    // when the signin credential is not a new user, while current user is anonymous                    // as existing user cannot be linked to existing anonymous user                    if (response.error?.errorCode == ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT) {                        val currentAnonymousUser = FirebaseAuth.getInstance().currentUser                        // TODO: save anonymous data                        val newCredential = response.credentialForLinking                        if (newCredential != null) {                            FirebaseAuth.getInstance().signInWithCredential(newCredential)                                .addOnSuccessListener {                                    val newUser = FirebaseAuth.getInstance().currentUser                                    // TODO: migrate/merge anonymous data to existing user                                }                        }                    }                    else {                        Timber.e("REQUEST_SIGN_IN error", response.error)                    }                }            }        }    }

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.