4

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

Alok
  • 8,452
  • 13
  • 55
  • 93

2 Answers2

4

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.");
       }
     }
   }
});
bojeil
  • 29,642
  • 4
  • 69
  • 76
  • No luck with this one too. All I'm getting is null. I've tried with permanentUser.getProvidersData().get(0).getDisplayName() also. Thanks for the help but I'm in need of something workable. – Alok Nov 18 '17 at 13:40
  • It may be the Google account you are using does not have a display name or is not exposing or something of the like. Try testing with other Google emails. – bojeil Nov 20 '17 at 05:18
  • Since I'm using the Google Sign-in method and in here i'm getting the data including name, email and firebaseid as mentioned in my question. But when it comes to linking of the account it doesn't work for name I'm getting email and the firebaseid only and name as null – Alok Nov 20 '17 at 06:59
  • I already explained that linking will not auto-populate. You have to copy them manually. If you already have the display name from google sign in sdk, you can copy it directly. – bojeil Nov 20 '17 at 17:41
1

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:

Amer Alahmar
  • 85
  • 10