Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase User's display name and photo url is not retrieved

I am developing an Android App using Firebase, In my app I am using Firebase Anonymous Login and Google sign In.

When the application starts if the user is not Logged In, then I am using Anonymous Authentication to log the user in.

Afterwards when user chooses to Sign In using Google, then I am converting Anonymous Account to a permanent account.

My issue over here is, When user's account is converted from Anonymous Account to permanent account (using Google Sign In in this case), I am not getting User's Display Name and Photo Url.

For converting from Anonymous Account to permanent account I am using below code.

    AuthCredential credential = GoogleAuthProvider.getCredential(googleIdToken, null);

            mAuth.getCurrentUser().linkWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d(TAG, "linkWithCredential:onComplete:" + task.isSuccessful());

                        if (!task.isSuccessful()) {
                            Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.",Toast.LENGTH_SHORT).show();

//If Google Account already linked up with other UID
    Tasks.await(mAuth.signInWithCredential(credential)).getUser();
                        }

                    }
                });

After the Sign In process completes, the AuthStateListener onAuthStateChanged is called, Then in onAuthStateChanged I am extracting User's Display Name, User's Photo Url and User's Email. Below is the onAuthStateChanged code.

@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null && !(user.isAnonymous())) {
    Log.d("userDetails", "UID: " + user.getUid());
    Log.d("userDetails", "Name: " + user.getDisplayName());
    Log.d("userDetails", "PhotoUrl: " + user.getPhotoUrl().toString());
    Log.d("userDetails", "Email: " + user.getEmail());
}

In the log I am getting null for user.getDisplayName() and user.getPhotoUrl()

I don't understand what I am doing wrong. Please help.

Thanks & Regards,

like image 791
Omi Mishra Avatar asked Feb 14 '17 12:02

Omi Mishra


People also ask

How can I get Display name in Firebase?

Get a user's profile // The user object has basic properties such as display name, email, etc. // you have one. Use User.getToken() instead. // The user object has basic properties such as display name, email, etc.

What does Firebase auth () CurrentUser return?

What does Firebase auth () CurrentUser return? If a user isn't signed in, CurrentUser returns null. Note: CurrentUser might also return null because the auth object has not finished initializing.


1 Answers

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
user.getProviderData();

loop through providers and get the desired provider

userData.getProviderId().equals(GoogleAuthProvider.PROVIDER_ID)
Uri photoUrl = userData.getPhotoUrl();
like image 85
Mohammad Yahia Avatar answered Sep 29 '22 11:09

Mohammad Yahia