我不知道这里会出现什么问题。我想知道用户是否匿名登录。我使用signInAnonymously()
进行登录。一切都像魅力一样。但是对FirebaseAuth.getInstance().getCurrentUser().isAnonymous()
的调用总是返回false。我在firebase控制台中验证了用户ID实际上是匿名的。
这里可能出现什么问题?
:编辑: 更新的代码 - 为配置文件更新添加的代码 - 为匿名用户初始化添加的代码
public final void initiateAnonymousSignIn(@NonNull final String displayName, @Nullable final Bitmap avatar) {
onRegistrationProgressStarted();
Task<AuthResult> signInTask = FirebaseAuth.getInstance().signInAnonymously();
signInTask.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
AuthResult result = task.getResult();
FirebaseUser user = result != null ? result.getUser() : null;
if (user != null) {
Log.d(getTag(), "Signed in anonymous user is "
+ (user.isAnonymous() ? "anonymous." : "not anonymous."));
}
UserService.isLoggedInWithAnonymousUser();
onRegistrationProgressFinished(true, null, AuthProvider.ANONYMOUS, displayName, avatar);
} else {
// Wrap FirebaseExceptions for better error handling.
onRegistrationProgressFinished(false, wrapFirebaseException(task.getException(), null),
AuthProvider.ANONYMOUS, displayName, avatar);
}
}
});
}
public void initiateUserAfterRegistration(@Nullable final String newDisplayName, @Nullable final Bitmap newAvatar) {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user == null) {
onUserLoadingFinished(null, DatabaseError
.fromException(new Exception("Can't initiate user after registration. User is not signed in.")));
return;
}
if (newDisplayName != null) {
Task<Void> updateTask = updateProfile(user, newDisplayName);
updateTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// proceed anyway, no matter if the profile could be updated or not
loadUserAndUpdateOrCreateIfNeeded(newDisplayName, newAvatar);
}
});
} else {
loadUserAndUpdateOrCreateIfNeeded(null, newAvatar);
}
}
个人资料更新:
private Task<Void> updateProfile(@NonNull FirebaseUser user, @NonNull final String displayName) {
UserProfileChangeRequest profileChangeRequest =
new UserProfileChangeRequest.Builder().setDisplayName(displayName).build();
Task<Void> updateTask = user.updateProfile(profileChangeRequest);
updateTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.i(TAG, "User profile updated successfully with new display name: " + displayName);
} else {
Log.w(TAG, "Could not update user profile with new display name.", task.getException());
}
}
});
return updateTask;
}
UserService.java
public class UserService {
public static boolean isLoggedInWithAnonymousUser() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
Log.d(TAG, "Current user is " + (user.isAnonymous() ? "anonymous." : "not anonymous."));
} else {
Log.d(TAG, "Current user is null.");
}
return user != null && user.isAnonymous();
}
}
这是我得到的意外输出:
03-19 01:48:38.385 6553-6565/com.myapp D/FirebaseAuth: Notifying listeners about user ( JCPHPVtShAf4QCPSUAYghteP2l32 ).
03-19 01:48:38.386 6553-6553/com.myapp D/FirebaseApp: Notifying auth state listeners.
03-19 01:48:38.386 6553-6553/com.myapp D/FirebaseApp: Notified 1 auth state listeners.
03-19 01:48:38.388 6553-6553/com.myapp D/LoginActivity: Signed in anonymous user is anonymous.
03-19 01:48:38.388 6553-6553/com.myapp D/UserService: Current user is anonymous.
03-19 01:48:38.956 6553-6553/com.myapp I/UserService: User profile updated successfully with new display name: asasdsd
03-19 01:48:39.143 6553-6588/com.myapp V/FA: Recording user engagement, ms: 25031
03-19 01:48:39.143 6553-6588/com.myapp V/FA: Using measurement service
03-19 01:48:39.143 6553-6588/com.myapp V/FA: Connecting to remote service
03-19 01:48:39.145 6553-6588/com.myapp V/FA: Activity paused, time: 11362503
03-19 01:48:39.146 6553-6588/com.myapp I/FA: Tag Manager is not found and thus will not be used
03-19 01:48:39.147 6553-6588/com.myapp D/FA: Logging event (FE): _e, Bundle[{_o=auto, _et=25031, _sc=LoginActivity, _si=-7803740637554967126}]
03-19 01:48:39.152 6553-6588/com.myapp V/FA: Using measurement service
03-19 01:48:39.152 6553-6588/com.myapp V/FA: Connection attempt already in progress
03-19 01:48:39.161 6553-6553/com.myapp D/Events: Registered AFragment
03-19 01:48:39.161 6553-6553/com.myapp D/AutoManageHelper: onStart true {0=com.google.android.gms.internal.zzaaa$zza@c70c388}
03-19 01:48:39.163 6553-6553/com.myapp D/MainActivity: onResume()
03-19 01:48:39.163 6553-6588/com.myapp V/FA: Using measurement service
03-19 01:48:39.163 6553-6588/com.myapp V/FA: Connection attempt already in progress
03-19 01:48:39.163 6553-6588/com.myapp V/FA: Activity resumed, time: 11362522
03-19 01:48:39.195 6553-6553/com.myapp I/MainActivity: onAuthenticated()
03-19 01:48:39.195 6553-6553/com.myapp D/UserService: Current user is not anonymous.
请注意登录后更新显示名称和之后的非匿名用户(在日志末尾)。这可能是个人资料更新后匿名状态不同步的错误吗?