问题
I am trying to link my firebase account to my google account and facebook account, and till here everything is working fine.here is my code how I am linking accounts.
mAuth.getCurrentUser().linkWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "linkWithCredential:success");
FirebaseUser user = task.getResult().getUser();
updateUI(user);
} else {
Log.w(TAG, "linkWithCredential:failure", task.getException());
Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
Now I want to get, is my current account is linked to facebook or google. is it possible to get this information or should I save this information to the database?
回答1:
Now I want to get, is my current account is linked to facebook or google?
You can check the provider id to see if the user is signed in with Google or Facebook like this:
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null) {
for (UserInfo userInfo : firebaseUser.getProviderData()) {
if (userInfo.getProviderId().equals("google.com")) {
Log.d(TAG, "User is signed in with Google");
} else if(userInfo.getProviderId().equals("facebook.com")) {
Log.d(TAG, "User is signed in with Facebook");
}
}
}
You can also use the following code:
List<? extends UserInfo> infos = user.getProviderData();
for (UserInfo ui : infos) {
if (ui.getProviderId().equals(GoogleAuthProvider.PROVIDER_ID)) {
Log.d(TAG, ui.getProviderId());
}
}
来源:https://stackoverflow.com/questions/52362602/how-to-get-linked-account-with-current-user-in-firebase