Android Firebase linking multiple account providers with matching email

Viewed 1357

In my firebase dashboard I have set multiple accounts for one email option.

I have simple email, Facebook and Google Plus authentication in my application.

I handle each of them 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's 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's 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 to make users that has same emails for Facebook and Google Plus able to authorize either with Facebook and Google Plus.

This this documentation documentation says that I should skip FirebaseAuth.signInWith methods and call these functions:

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

Now there is confusing. How I can call getCurrentUser when it is still null because I have skipped signInWith methods?

Documentation also says that I handle merging whic I dont understand.

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

Also, signInWithCredenial does not have an await method.

That means that I should be linking multiple accounts with same email after Logining?

1 Answers
Related