[英].getCurrentUser() never returns null - firebase
我正在為我的應用程序創建 facebook 身份驗證,一切正常,除了當我退出 facebook 時,它仍然將我傳遞到應用程序的主菜單。 我認為這意味着 .getCurrentUser() 即使我已注銷也不會返回 null。
我試過注釋掉 updateUI(); 在我下面的代碼中,這似乎解決了問題,但是我希望此代碼能夠正常工作
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null) {
updateUI();
}
您需要附加一個 authstate 偵聽器。
“在某些情況下,getCurrentUser 將返回一個非空 FirebaseUser 但底層令牌無效。例如,如果用戶在另一台設備上被刪除並且本地令牌沒有刷新,則可能發生這種情況。在這種情況下,您可能會獲得有效的用戶 getCurrentUser,但對經過身份驗證的資源的后續調用將失敗。
getCurrentUser 也可能返回 null,因為 auth 對象尚未完成初始化。
如果附加 AuthStateListener,則每次底層令牌狀態更改時都會收到回調。 這對於對上述邊緣情況做出反應非常有用。” https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth.AuthStateListener
嘗試使用 AuthStateListener,示例:
//Declaration and defination
private FirebaseAuth firebaseAuth;
FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null){
//Do anything here which needs to be done after user is set is complete
updateUI();
}
else {
}
}
};
//Init and attach
firebaseAuth = FirebaseAuth.getInstance();
firebaseAuth.addAuthStateListener(authStateListener);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.