问题
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:
To link accounts, there should be an existing session. For example lets say that a new user creates an account using Google as the auth provider.
In summary, to do this you will need to:
- Use GIDSignIn to authenticate the user with Google.
- Then Google returns an Id Token (if everything goes well).
- You will use the token to create a GoogleAuthProvider credential object.
- And then you use this credential to authenticate in Firebase calling signInWithCredential.
The process is similar with other auth providers like Facebook. In order to link the account with Facebook, you will need to do the first three steps mentioned above (related to Facebook auth) but instead of "signInWithCredential" you will need to call "linkWithCredential". If everything goes well, now the user will be able to authenticate to the same account with Google or Facebook.
If you call "signInWithCredential", you will create a new account that uses Facebook as auth provider. So a user instead of being able to access to a single account with two(or more) auth providers, that user will have two separate accounts for each auth provider. This is why the documentation says that you should skip calling FirebaseAuth.signInWith methods.
Regarding your question about merging, the documentation mentions: "The call to linkWithCredential will fail if the credentials are already linked to another user account". This means that a user has already an account with the auth provider. If you want the user to access the information from both accounts, you will need to create the logic to merge the information from one account to the other.
来源:https://stackoverflow.com/questions/44125583/android-firebase-linking-multiple-account-providers-with-matching-email