How to get linked account with current user in firebase

纵饮孤独 提交于 2019-12-17 17:23:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!