Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebaseAuth.getCurrentUser() return null DisplayName

When I signIn with my google account and get the name with the getDisplayName(), my name appear correctly, but in the AuthStateListener doesn't.

here part of my code:

private void handleSignInResult(GoogleSignInResult result) {

    Alert.dismissProgress();

    if (result.isSuccess()) {
        GoogleSignInAccount acct = result.getSignInAccount();

        if(acct != null) {
            Log.i("NAME", acct.getDisplayName()); <-- RETURN MY NAME CORRECTLY
            credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
            fuser.linkWithCredential(credential).addOnCompleteListener(authResult);                    
        } else {
            //ERROR
        }

    } else {
        //ERROR
    }
}

But in my AuthStateListener

@Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser nuser = firebaseAuth.getCurrentUser();

        if (nuser != null) {

            Log.i("NAME", nuser.getDisplayName()); <--- RETURN NULL 
        }
    }

Somebody know why this can happen?

like image 747
Patricio Cornejo Avatar asked Jun 06 '16 15:06

Patricio Cornejo


1 Answers

This is a tricky one since it is not so clear in the documentation...

Check the getProviderData()

as defined here: https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser#public-method-summary

You can iterate that List and it will have all the providers associated with that account, included a provider with the providerId = "google.com" with a display Name = YOUR_GOOGLE_USERNAME

let me know if you cannot make it work

like image 91
Ymmanuel Avatar answered Sep 16 '22 18:09

Ymmanuel