Pat*_*ejo 11 android firebase firebase-authentication
当我使用我的Google帐户登录并使用getDisplayName()获取名称时,我的名称显示正确,但在AuthStateListener中则没有.
这是我的代码的一部分:
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
}
}
Run Code Online (Sandbox Code Playgroud)
但是在我的AuthStateListener中
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser nuser = firebaseAuth.getCurrentUser();
if (nuser != null) {
Log.i("NAME", nuser.getDisplayName()); <--- RETURN NULL
}
}
Run Code Online (Sandbox Code Playgroud)
有人知道为什么会这样吗?
只是添加Ymmanuel的答案(谢谢!),并为其他任何寻找快速复制和粘贴的人提供一些示例代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果最初未在User对象中找到,则上面将尝试使用提供程序中的第一个显示名称和照片URL.
奖励:使用滑动图像:https://github.com/bumptech/glide.
这是一个棘手的问题,因为它在文档中并不那么清楚......
检查getProviderData()
如下所示:https: //firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser#public-method-summary
您可以迭代该列表,它将包含与该帐户关联的所有提供商,包括提供商,其中providerId ="google.com",其显示名称为= YOUR_GOOGLE_USERNAME
如果你不能让它发挥作用,请告诉我
埃德蒙·约翰逊是对的。这个问题是在 Firebase Auth 9.8.0 中引入的。解决方法包括降级到 9.6.1 或强制“重新登录”,因为在用户注销并重新登录后会填充信息。Firebase 问题中描述了该问题
Firebase UI 贡献者之一 - Alex Saveau 已将其报告为 Firebase 的错误。
归档时间: |
|
查看次数: |
14397 次 |
最近记录: |