情况就是这样,我正在使用联邦提供商和电子邮件的firebase身份验证。当用户登录成功时,会保存在firebase实时数据库中。这非常有效。我遇到的问题如下:
因此,在这种情况下,当我调用一个方法在数据库中按id查找用户时,从未调用onDataChange。
以下是 AuthStateListener 代码:
this.mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null) {
//There is an active session with an user id but the user doesn't exist anymore in Firebase Authentication console
FBAuthManager.this.mAuthCurrentUser = firebaseAuth.getCurrentUser();
AppUtils.writeLog("User to find: " + firebaseAuth.getCurrentUser().getUid());
databaseManager.getUserById(firebaseAuth.getCurrentUser(), true, listener);
} else
listener.onFailedToCompleteTask("user is null");
}
};
这是 getUserById 方法:
public void getUserById(@NonNull String user_id, boolean single_value, @NonNull final DAOManagerListener.OnGetUserListener listener){
checkingDatabaseConnection();
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//It 's never been called when the user id doesn't exist in the Firebase Authentication console
if ( dataSnapshot.exists()) {
UserChat userChat = dataSnapshot.getValue(UserChat.class);
listener.onGetUser(userChat);
}
else
listener.onFailedToGetUser("user doesn't exist");
}
@Override
public void onCancelled(DatabaseError databaseError) {
listener.onFailedToGetUser( databaseError.getMessage() );
}
};
if ( single_value )
this.mDatabaseReference.child(USERS_CHILD).child(user_id).addListenerForSingleValueEvent(valueEventListener);
else
this.mDatabaseReference.child(USERS_CHILD).child(user_id).addValueEventListener(valueEventListener);
}
checkingDatabaseConnection 方法表示数据库未连接
DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
AppUtils.writeLog("connected");
} else {
AppUtils.writeLog("not connected");
}
}
@Override
public void onCancelled(DatabaseError error) {
AppUtils.writeLog("Listener was cancelled");
}
});
我的firebase规则仅在用户通过身份验证时设置为读写。
我不知道是否可以检查firebase控制台中是否存在应用中具有活动firebase会话的用户。
我希望自己解释一下。如果您需要更多信息,请告诉我。