Home > database >  Link several user accounts (same email) Firebase
Link several user accounts (same email) Firebase

Time:11-26

I have the possibility that the user can choose if they want to log in with Google, Facebook, email/password, etc.

After testing my app, the following happened:

  1. I sign up with my name, email, and password
  2. Handle the get started logic
  3. Verify my auth users on Firebase (grey email icon)
  4. Sign out of the account
  5. Now, I want to log in with Google (same email used on the sign-up with email and password)
  6. The Google sign-in worked
  7. Verify my auth users on Firebase (the grey email icon changed into the Google one)
  8. Sign out of the account
  9. Can't log in with email and password anymore but the google sign in worked

After some research, I end up with the Link Multiple Auth Providers to an Account on Android documentation

I realized I have to refactor my code to not use the FirebaseAuth.signInWith methods

This is a little except of my loginEmailAndPassword:

val credential = EmailAuthProvider.getCredential(email, password)

firebaseAuth.currentUser!!.linkWithCredential(credential).addOnCompleteListener{ authTask: Task<AuthResult> ->
    if (authTask.isSuccessful) {

I have an 'else' meaning the (authTask.isSuccessful) did not happened and another 'if' with the FirebaseAuthUserCollisionException

                val exception: java.lang.Exception? = authTask.exception
            if (exception is FirebaseAuthUserCollisionException) {
                linkAndMerge(credential)

My goal is to link and merge, and I do not know how to link the accounts (both email grey and Google on Firebase)

    private fun linkAndMerge(credential: AuthCredential) {
    val authenticatedUserMutableLiveData: MutableLiveData<ResponseState<UserModel>> =
        MutableLiveData()

    val prevUser = firebaseAuth.currentUser
    firebaseAuth.signInWithCredential(credential)
        .addOnSuccessListener { result ->
            val currentUser = result.user
            // Merge prevUser and currentUser accounts and data
            // ...

        }
        .addOnFailureListener {
            authenticatedUserMutableLiveData.value = ResponseState.Error("Error")
        }
}

My questions:

  1. Can I call something to merge prevUser and currentUser accounts. I just want to the user have the possibility of using different authentications.
  2. I am not worried about the data because if it's the same User UID does not matter if the authentication provider
  3. Can I still use 'createUserWithEmailAndPassword'?

CodePudding user response:

Steps 1 to 9 provide the expected behavior. If you create a user with email and password and right after that you sign in with Google, the account will only be accessible with Google. Why? Because behind the scenes Firebase converts the account that was created with email and password into an account with the Google provider. Unfortunately, you cannot reverse that change.

The link in your question, is referring to the possibility to link an existing account to a specific provider. For example, if you implement anonymous authentication, then you can link that account with Google, for example. This means that the UID remains the same.

If you want to stop that mechanism from happening, then you should consider allowing the creation of different accounts for different providers. You can find this option which is called "Create multiple accounts for each identity provider" right inside the Firebase Console, in the Settings tab inside the Authentication.

  • Related