首页 文章

firebase onAuthStateChanged跟随Fibonacci的系列

提问于
浏览
0

我正在使用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控制台显示此行为:

(因为我使用了错误的密码,图像末尾的错误是我的错)

Chrome console showing the functions getting called following the Fibonacci series

按钮调用的函数(但我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 2移动所有navController相关函数:使用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);

3 回答

  • 0

    我看到你正在推动 onAuthStateChanged 中的页面 . 这可能会导致问题 .

    • 在第一个登录页面上,当您单击登录时,您创建1 onAuthStateChanged login ,您将推送1个新主页

    • 在主页上,当您单击注销时,您创建1 onAuthStateChanged logout 您推送1个新登录页面(因此您点击后有2个登录页面和2个 onAuthStateChanged login 加载)

    • 在新登录页面上单击登录后,您不会创建1个新的登出页面,但是因为您已经有2个 onAuthStateChanged login ,所以这会导致3个登出页面加载3个 onAuthStateChanged logout

    • 当您在新主页上单击“注销”时,将调用3 onAuthStateChanged logout ,从而创建3个新登录页面2现有登录页面= 5

    • 等等 .

    要测试是否是这种情况,您可以开始使用登录页面加载模块,当您登录时推送主页,当您注销时不要按下登录页面但是popToRoot()

    希望这可以帮助,

  • 0

    您应该在 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
            });
        }
    }
    
  • 0

    我的登录时遇到了类似的问题 . 函数/监听器 firebase.auth().onAuthStateChanged( 将在每次验证状态发生变化时触发,我预计 onAuthStateChanged 代码中的问题是 firebase.auth().signOut(); ,这将触发新的 onAuthStateChanged ...导致奇怪的初始行为 .

    你已经在你的注销按钮中放了 firebase.auth().signOut(); ,我建议你从登录页面的 onAuthStateChanged 删除 firebase.auth().signOut(); ,然后我希望斐波那契会停止(但你真的希望它停止吗?)

    如果有任何结果,请告诉我 . 希望能帮助到你,

相关问题