问题
I'm trying to add a user using the Firebase auth and it returns the displayName as null, even though it asks for it via the UI on sign up. Any ideas why and how to fix? Here's the code I am using to do this:
startActivityForResult( AuthUI.getInstance()
.createSignInIntentBuilder()
.setProviders(Collections.singletonList(new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build()))
.build(), RC_SIGN_IN);
and in the Activity for Result....
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
// user is signed in!
//save user
saveUser();
startMainActivity();
return;
}
save user code is as follows:
private void saveUser(){
FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
final User user = new User();
if (fUser.getDisplayName() != null) {
user.setUserName(fUser.getDisplayName());
} else {
Log.e("FB_info", "User has no display name");
}
user.setUserEmail(fUser.getEmail());
final DatabaseReference userRef = FirebaseDatabase.getInstance().getReference(FirebaseRefs.getUsersRef()).child(fUser.getUid());
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()){
userRef.updateChildren(user.toMap());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Any idea why the display name is returning null?
回答1:
So this appears to be (now known) bug in the Firebase itself (Not Firebase-UI). The bug appears to be introduced with version Firebase Android SDK v9.8.0
. For the time being one of the workarounds is to force users to logout and re-login after they have registered with system. The other involves downgrading FirebaseUI to the version which uses older Firebase SDK version.
Source: https://github.com/firebase/FirebaseUI-Android/issues/409
回答2:
You Can Also Get Name From firebaseAuthWithGoogle Method.
private void firebaseAuthWithGoogle(final GoogleSignInAccount acct) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
} else {
System.out.println(acct.getDisplayName());
System.out.println(acct.getPhotoUrl());
}
}
}); }
来源:https://stackoverflow.com/questions/40683510/displayname-showing-null-after-creating-user-with-firebase-auth