Android Firebase linking multiple account providers with a matching email

In the firebase control panel, I have set the option multiple accounts for one email

.

My app has simple email, Facebook and Google Plus authentication.

I handle each one like this in my LoginActivity:

Google Plus:

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    dialog.dismiss();
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user information
                        FirebaseUser user = mAuth.getCurrentUser();
                        proceed();
                    } else {
                        // If sign in fails, display a message to the user.
                        Toast.makeText(LoginActivity.this, task.getException().getMessage(),
                                Toast.LENGTH_SHORT).show();

                    }

                    // ...
                }
            });
}

      

Facebook:

private void handleFacebookAccessToken(AccessToken token) {
    AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
    dialog.show();
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    dialog.dismiss();
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user information
                        FirebaseUser user = mAuth.getCurrentUser();
                        proceed();
                    } else {
                        Toast.makeText(LoginActivity.this, task.getException().getMessage(),
                                Toast.LENGTH_SHORT).show();
                    }

                }
            });

}

      

Simple email:

mAuth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        dialog.dismiss();
                        proceed();
                    } else {
                        Toast.makeText(LoginActivity.this, "Wrong email or password", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                }
            });

      

Now, I want users who have the same emails for Facebook and Google Plus to be able to allow Facebook and Google Plus.

This documentation of this documentation says that I should skip the methods FirebaseAuth.signInWith

and call these functions:

AuthCredential credential = GoogleAuthProvider.getCredential(googleIdToken, null);
mAuth.getCurrentUser().linkWithCredential(credential)

      

Now this is confusing. How can I call getCurrentUser when it is still zero because I missed the signInWith methods?

The documentation also says that I am handling a merge, which I do not understand.

currentUser = auth.signInWithCredential(credential).await().getUser();

      

Also, signInWithCredenial has no method await

.

Does this mean I have to link multiple accounts with the same email address after logging in?

+3


source to share


1 answer


To link accounts, an existing session must exist. For example, let's say a new user creates an account using Google as their authorization provider.

In general, for this you need:

  • Use GIDSignIn to authenticate a user with Google.
  • Then Google returns the Id Token (if all goes well).
  • You will be using a token to create a GoogleAuthProvider credential object.
  • And then you use those credentials to authenticate to Firebase by calling signInWithCredential.


The process is similar to other auth providers like Facebook. To link your account to Facebook, you will need to do the first three steps mentioned above (related to Facebook auth), but instead of using "signInWithCredential" you will need to call "linkWithCredential". If all goes well, the user will now be able to authenticate to the same Google or Facebook account.

If you call "signInWithCredential", you will create a new account that uses Facebook as the auth provider. Thus, instead of having access to a single account with two (or more) auth providers, a user will have two separate accounts for each authorization provider. This is why the documentation says that you should skip calls to the FirebaseAuth.signInWith methods.

Regarding the merge question, the documentation mentions: "The call to linkWithCredential will fail if the credentials are already associated with another user account." This means that the user already has an account with the auth provider. If you want a user to access information from both accounts, you need to create logic to merge information from one account to another.

+3


source







All Articles