Not getting the user's name when anonymous linking with google account

Viewed 497

I have two way method for signing-in, one is normal Google sign-in and other is Anonymous Sign-in. In my google sign-in there is no problem and everything works fine even the user's credentials are also coming great including name, email and firebase id. Now the problem is that when I link my anonymous account to the google id using firebase, it is also working fine but at the time of getting credentials all I'm getting is null when getDisplayName() is called.

NOTE: I'm getting the firebase id and email correctly but the name is coming null.

This is my log output here :

11-15 12:58:26.712 21983-21983/in.pinelane.myhovi E/userName: null
11-15 12:58:26.712 21983-21983/in.pinelane.myhovi E/userEmail: xyz@gmail.com

Since my code is working fine but still no luck, code goes like this :

private void linkGuestWithCredential(AuthCredential credentials) {

    firebaseAuth.getCurrentUser().linkWithCredential(credentials)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if( task.isSuccessful()){
                        mProgress.dismiss();
                        Log.d(TAG, "linkWithCredential:success");

                        FirebaseUser permanentUser = task.getResult().getUser();
                        callAnonymousUpdateApi(permanentUser);

                        Intent signIntent = new Intent(LoginActivity.this, MainActivity.class);
                        signIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(signIntent);
                        Toast.makeText(LoginActivity.this, "Welcome to MyHovi!", Toast.LENGTH_LONG)
                                .show();
                        finish();
                        overridePendingTransition(R.anim.right_enter, R.anim.left_exit);

                    }else{
                        mProgress.dismiss();
                        Log.e(TAG, "linkWithCredential:failure", task.getException());
                        Toast.makeText(getApplicationContext(), "Sign-in failed", Toast.LENGTH_SHORT)
                                .show();
                    }
                }
            });

In the callAnonymousUpdateApi(permanentUSer) I'm actually getting the credentials :

private void callAnonymousUpdateApi(FirebaseUser permanentUser) {

    final String name = permanentUser.getDisplayName();
    final String email = permanentUser.getEmail();
    final String firebaseId = permanentUser.getUid();

    RequestParams params = new RequestParams();
    params.add("user_id", userId);
    params.add("name", name);
    params.add("email", email);
    params.add("firebase_id", firebaseId);
    params.add("user_complete", "true");
}

What I have tried so far is :

  1. Changing FirebseUser permanentUSer = task.getResult().getUser(); to FirebseUser permanentUSer = FirebaseAuth.getInstance().getCurrentUser();

  2. Tried the same thing in different way that is in the callAnonymousUpdateApi(FirebaseUser permanentUser) what I've done is trying the to get the name as final String name = FirebaseAuth.getInstance().getCurrentUser().getDisplayName();

but still no luck, any help would be highly appreciated. Thanks

2 Answers

When linking a provider, the provider's displayName and photoURL are not automatically copied to the top level. You can simply copy these fields when linking to an anonymous user. You can get the Google displayName:

FirebaseAuth.getInstance().getCurrentUser().getProviderData(‌​).get(0).getDisplayName()

You would then updateProfile:

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder().setDisplayName(displayName).build();
user.updateProfile(profileUpdates)
   .addOnCompleteListener(new OnCompleteListener<Void>() {
     @Override
     public void onComplete(@NonNull Task<Void> task) {
       if (task.isSuccessful()) {
         Log.d(TAG, "User profile updated.");
       }
     }
   }
});

I am aware that this is an old question but just to provide an answer for people with the same question:

Bojeil's Answer: FirebaseAuth.getInstance().getCurrentUser().getProviderData(‌).get(0).getDisplayName() is correct! However, he is getting the getDisplayName() for the first provider using get(0) What you have to do is to check for the provider id that you want and git its display name or an easier solution would be to go throw all of them checking for getDisplayName() != null like this:

String displayName = "";
for (UserInfo userInfo : firebaseAuth.getCurrentUser().getProviderData()) {
    if (userInfo.getDisplayName() != null) {
        displayName = userInfo.getDisplayName();
        break;
    }
}

refer to this document for more details:

Related