firebaseAuth.getCurrentUser() return null DisplayName

前端 未结 9 1859
[愿得一人]
[愿得一人] 2020-12-09 05:24

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 cod

相关标签:
9条回答
  • 2020-12-09 05:45

    Just to add to Ymmanuel's answer (Thank you!) with some example code for anyone else looking for a quick copy and paste:

    FirebaseUser user = firebaseAuth.getCurrentUser();
    if (user != null) {
        // User is signed in              
        String displayName = user.getDisplayName();
        Uri profileUri = user.getPhotoUrl();
    
        // If the above were null, iterate the provider data
        // and set with the first non null data
        for (UserInfo userInfo : user.getProviderData()) {
            if (displayName == null && userInfo.getDisplayName() != null) {
                displayName = userInfo.getDisplayName();
            }
            if (profileUri == null && userInfo.getPhotoUrl() != null) {
                profileUri = userInfo.getPhotoUrl();
            }
        }
    
        accountNameTextView.setText(displayName);
        emailTextView.setText(user.getEmail());
        if (profileUri != null) {
            Glide.with(this)
                    .load(profileUri)
                    .fitCenter()
                    .into(userProfilePicture);
        }
    }
    

    The above will try to use the first display name and photo url from the providers if it wasn't initially found in the User object.

    Bonus: Using glide for images: https://github.com/bumptech/glide .

    0 讨论(0)
  • 2020-12-09 05:45

    I found a solution for this problem, in the Firebase documentation! The solution is to update the user profile using the: UserProfileChangeRequest:

    UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                          .setDisplayName(mUser.getName())
                          .setPhotoUri(Uri.parse("https://example.com/mario-h-user/profile.jpg"))
                          .build();
    
                firebaseUser.updateProfile(profileUpdates)
                          .addOnCompleteListener(new OnCompleteListener<Void>() {
                              @Override
                              public void onComplete(@NonNull Task<Void> task) {
                                  if (task.isSuccessful()) {
                                      Log.d(TAG, "User profile updated.");
                                  }
                              }
                          });
    

    The variable mUser is already filled with the content from the fields. I used this piece of code inside the FirebaseAuth.AuthStateListener() -> onAuthStateChanged()

    0 讨论(0)
  • 2020-12-09 05:45

    I was getting this problem when I had:

    compile 'com.google.firebase:firebase-auth:10.0.0'
    compile 'com.firebaseui:firebase-ui-auth:1.0.1'
    

    A workaround that fixed it for me was to replace that with:

    compile 'com.google.firebase:firebase-auth:9.6.0'
    compile 'com.firebaseui:firebase-ui-auth:0.6.0'
    

    Iterating through user.getProviderData() as suggested elsewhere didn't fix it for the later versions.

    0 讨论(0)
  • 2020-12-09 05:48

    Based on Alan's and Ymmanuel's answers here's the 2 helper methods that I'm using:

    public static String getDisplayName(FirebaseUser user) {
        String displayName = user.getDisplayName();
        if (!TextUtils.isEmpty(displayName)) {
            return displayName;
        }
    
        for (UserInfo userInfo : user.getProviderData()) {
            if (!TextUtils.isEmpty(userInfo.getDisplayName())) {
                return userInfo.getDisplayName();
            }
        }
    
        return null;
    }
    
    public static Uri getPhotoUrl(FirebaseUser user) {
        Uri photoUrl = user.getPhotoUrl();
        if (photoUrl != null) {
            return photoUrl;
        }
    
        for (UserInfo userInfo : user.getProviderData()) {
            if (userInfo.getPhotoUrl() != null) {
                return userInfo.getPhotoUrl();
            }
        }
    
        return null;
    }
    
    0 讨论(0)
  • 2020-12-09 05:51

    This issue is resolved in the latest verison of firebase ui compile 'com.firebaseui:firebase-ui:1.2.0'

    0 讨论(0)
  • 2020-12-09 05:55

    I had the same issue, I solved it by signing out the user and resigning them back in.

    Call FirebaseAuth.getInstance().signOut(); to sign them out, then try again. As I've discovered, this issue is common with using email/password authentication and Social login (Facebook in my case) at the same time.

    0 讨论(0)
提交回复
热议问题