Android firebase associates multiple account providers with matching emails
In my firebase dashboard, I set up multiple accounts for an email option
My app has simple email, Facebook and Google plus authentication
I handle each of them in 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's:
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 mail:
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 with the same email to authorize Facebook and Google plus through Facebook and Google plus
This document says that I should skip the firebaseauth.signinwith method and call these functions:
AuthCredential credential = GoogleAuthProvider.getCredential(googleIdToken, null);
mAuth.getCurrentUser().linkWithCredential(credential)
Now it's confusing. How do I call getcurrentuser when it's still null because I skipped the signinwith method?
The document also says I don't understand when I deal with merging
currentUser = auth.signInWithCredential(credential).await().getUser();
In addition, signinwithcredenial has no await method
Does this mean that I should use the same email to link multiple accounts after signing in?
resolvent:
To link an account, you should have an existing session. For example, suppose a new user uses Google as an authentication provider to create an account
In short, to do this, you need to:
> use GIDSignIn to authenticate users through Google. > Google then returns a Id token (if all goes well). > you will create a GoogleAuthProvider credential object using a token. > then you use this credential to invoke signInWithCredential in Firebase for identity verification.
This process is similar to other auth providers such as Facebook. In order to link your account with Facebook, you need to perform the first three steps mentioned above (related to Facebook auth), rather than "signinwithcredential". You need to call "linkwithcredential". If everything goes well, users will now be able to authenticate the same account using Google or Facebook
If you call "signinwithcredential", you will create a new account using Facebook as an authentication provider. Therefore, users will not be able to access a single account with two (or more) authentication providers, This user will provide two separate accounts for each authentication provider. That's why documentation says you should skip calling the firebaseauth.signinwith method
On the issue of merging, the document mentions that "if the credentials have been linked to another user account, the call to linkwithcredential will fail". This means that the user already has an auth provider's account. If you want users to access information from two accounts, you will need to create logic to merge information from one account to another