Search code examples
androidfirebasefirebase-authenticationfirebaseui

How to use FirebaseUI for account linking on Android?


I am using Firebase Authentication in my android application. I am able to login users with their Gmail Id or phone number. I want to link phone number if the user is already signed-in with email id or vise-vers. I referred to Firebase Auth documentation for account linking but they have done it manually (getting phone number and OTP manually). But on its main page of FirebaseUI, it is written

Account Linking - flows to safely link user accounts across identity providers.

as one of its features. But it is not mentioned how to use FirebaseUI for account linking.

Here is what I have done till now:

I am using these three functions to sign in the user.

public void signInWithAllProviders() {
    List<AuthUI.IdpConfig> providers = Arrays.asList(
            new AuthUI.IdpConfig.EmailBuilder().build(),
            new AuthUI.IdpConfig.PhoneBuilder().build(),
            new AuthUI.IdpConfig.GoogleBuilder().build(),
            new AuthUI.IdpConfig.FacebookBuilder().build());
    startActivityForResult(
            AuthUI.getInstance()
                    .createSignInIntentBuilder()
                    .setAvailableProviders(providers)
                    .build(),
            RC_SIGN_IN);
}

public void signInWithPhone() {
    startActivityForResult(
            AuthUI.getInstance().
                    createSignInIntentBuilder().
                    setAvailableProviders(Collections.singletonList(new AuthUI.IdpConfig.PhoneBuilder().build())).
                    build(),
            RC_PHONE_SIGN_IN);
}

public void signInWithEmail() {
    startActivityForResult(
            AuthUI.getInstance().
                    createSignInIntentBuilder().
                    setAvailableProviders(Arrays.asList(
                            new AuthUI.IdpConfig.GoogleBuilder().build(),
                            new AuthUI.IdpConfig.EmailBuilder().build())).
                    build(),
            RC_EMAIL_SIGN_IN);
}

When the user is already signed in, and he tries to sign in with a phone number by calling signInWithPhone(), the FirebaseUI is replacing the previously signed in credentials. So I am not able to link accounts.

Am I missing anything?


Solution

  • FirebaseUI handles automatic account linking (when one account per email is enabled). This covers the following similar cases:

    • User signs up with Google on one device.
    • User tries to sign with Facebook to the same app on another device or after sign out, using the same Google email.
    • Typically this will throw an error that linking is required since a verified account using the same email already exists.
    • In order to recover, FirebaseUI asks the user to sign in with Google to the existing account and then links the Facebook credential to it. The next time the user tries to sign in, they can use either their Google or Facebook account.

    FirebaseUI will support the case above but does not support manual linking as you are trying to do, where you want to link a phone number to a signed in user. Feel free to file a feature request for it in the FirebaseUI repo.