[英]Firebase's set() not working inside onAuthStateChanged()
[英]firebase onAuthStateChanged following Fibonacci's series
我正在開發使用Ionic 3和Firebase作為身份驗證提供程序的混合應用程序。 該應用程序分為2頁。 登錄頁面和主頁。 在登錄頁面上,有一個按鈕可使用電子郵件+密碼登錄用戶,在主頁上,有一個按鈕可注銷當前用戶。 基本上,第一次加載應用程序時一切正常。 但是在登錄然后退出然后再次登錄之后,onAuthStateChange內部的函數將被調用兩次,然后調用三次,然后調用五次,依此類推。 這是登錄頁面代碼:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
console.log('User logged-in');
if (user.emailVerified) {
self.goToHome();
} else {
self.showAlert();
firebase.auth().signOut();
}
}
});
}
這是注銷代碼:
firebase.auth().onAuthStateChanged(function(user) {
if (user == null) {
console.log('User logged-out');
navCtrl.push(WelcomePage);
}
});
chrome控制台顯示此行為:
(圖像末尾的錯誤是我的錯,因為我使用了錯誤的密碼)
按鈕調用的函數(但我99%確信問題出在onAuthStateChanged上):
doLogin() {
console.log('Trying to log in...');
var email = this.account.email;
var password = this.account.password;
if (this.validateEmail(email) && password.length >= 8) {
loading.present();
firebase.auth().signInWithEmailAndPassword(email, password)
.then(function(data) {
loading.dismiss();
});
}
logout() {
firebase.auth().signOut();
}
函數goToHome將用戶移至主頁:
goToHome() {
this.navCtrl.push(MainPage);
}
Ben在評論中指出的解決方案:
1:從onAuthStateChanged中移走所有與navController相關的功能2:使用setRoot代替pop()和push()
goToHome() {
this.navCtrl.setRoot(HomePage);
}
logout() {
firebase.auth().signOut();
this.navCtrl.setRoot(WelcomePage);
}
3)要修復如果用戶未注銷就能夠自動登錄的問題,我檢查了firebase.auth()。currentuser是否存在,但是使用了超時,因為Firebase需要一些時間才能正常運行:
setTimeout(function(){
var user = firebase.auth().currentUser;
if (user != null) {
self.goToHome();
}
}, 1500);
您應該將用戶發送到firebase.auth().signInWithEmailAndPassword(email, password)
之后的頁面,而不是在偵聽器上。 Firebase偵聽器的行為異常。
嘗試這個:
doLogin() {
console.log('Trying to log in...');
var email = this.account.email;
var password = this.account.password;
if (this.validateEmail(email) && password.length >= 8) {
loading.present();
firebase.auth().signInWithEmailAndPassword(email, password).then(function(data) {
loading.dismiss();
var user = firebase.auth().currentUser;
if (user) {
console.log('User logged-in');
if (user.emailVerified) {
self.goToHome();
} else {
self.showAlert();
firebase.auth().signOut();
}
} else {
// No user is signed in.
}
}, function(error) {
//error handling
});
}
}
我的登錄時遇到了類似的問題。 函數/偵聽器firebase.auth().onAuthStateChanged(
會在每次身份驗證狀態更改時觸發,我希望您的onAuthStateChanged
代碼中的問題與onAuthStateChanged
firebase.auth().signOut();
觸發新的onAuthStateChanged
...導致奇怪的啟動行為。
您已經放置了firebase.auth().signOut();
在您的注銷按鈕中,建議您刪除firebase.auth().signOut();
從登錄頁面上的onAuthStateChanged
開始,然后我希望斐波那契會停止(但是您真的希望它停止嗎?)
讓我知道是否有任何結果。 希望能幫助到你,
我看到您在onAuthStateChanged
中推送頁面。 這可能會導致問題。
onAuthStateChanged login
1個新主頁 onAuthStateChanged logout
創建1個onAuthStateChanged logout
,然后按1個新的登錄頁面(因此,單擊onAuthStateChanged login
加載2個登錄頁面和2個onAuthStateChanged login
) onAuthStateChanged login
,因此這將導致加載3個注銷頁面和3個onAuthStateChanged logout
onAuthStateChanged logout
,從而創建3個新的登錄頁面+ 2個現有的登錄頁面= 5 要測試是否存在這種情況,您可以開始使用登錄頁面加載模塊,在登錄時按下主頁,在注銷時不要推送登錄頁面,而是popToRoot()
希望這可以幫助,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.