I'm developing an hybrid app using Ionic 3 and Firebase as authentication provider. The app consist in 2 pages. A login page and an home page. On the login page there's a button that log-in the user using email+password and on the home page another button logout the current user. Basically, the first time the app is loaded everything works fine. But after loggin-in then logging-out then logging-in again the function inside onAuthStateChange gets called twice, then three times, then five and so on following the Fibonacci's series. This is the login page code:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
console.log('User logged-in');
if (user.emailVerified) {
self.goToHome();
} else {
self.showAlert();
firebase.auth().signOut();
}
}
});
}
and this is the logout code:
firebase.auth().onAuthStateChanged(function(user) {
if (user == null) {
console.log('User logged-out');
navCtrl.push(WelcomePage);
}
});
chrome console shows this behavior:
(the error at the end of the image was my fault as I used the wrong password)
The functions called by the buttons (but I'm 99% sure that the problem is 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();
}
The function goToHome to move the user to the homepage:
goToHome() {
this.navCtrl.push(MainPage);
}
Solution as pointed out by Ben in his comment:
1: moving all navController related functions from onAuthStateChanged 2: using setRoot instead of pop() and push()
goToHome() {
this.navCtrl.setRoot(HomePage);
}
logout() {
firebase.auth().signOut();
this.navCtrl.setRoot(WelcomePage);
}
3) To fix be able to auto-login the user if he/she didn't logout I check if firebase.auth().currentuser exist, but using a timeout because Firebase needs some time before functioning properly:
setTimeout(function(){
var user = firebase.auth().currentUser;
if (user != null) {
self.goToHome();
}
}, 1500);
component
route logic. what this doself.goToHome();
? – Hareesh