Firebase auth()。onAuthStateChanged()

问题描述 投票:2回答:1

我在我的Ionic应用程序中使用了auth()。onAuthStateChanged(),因此当用户登录时,该应用程序进入用户个人资料$ state,但问题是,由于观察者不断检查更改,所以我无法更改$ state,无论何时尝试,它都会变回用户个人资料$ state。

    firebase.auth().onAuthStateChanged(function(user) {
    $scope.currUser = user;
    if (user) {
        $scope.go('profile') //function to change states.
    } else {
    // No user is signed in.
        $scope.go("login") //login is where the app starts (login page)
    }
    });

[现在,当我进入配置文件$ state并尝试进入另一个状态时-例如,仪表板会在几秒钟内自行返回到配置文件状态。

请帮助我解决此问题,并预先感谢

angularjs ionic-framework firebase firebase-authentication
1个回答
0
投票

如果只需要确认用户已登录一次,则可以取消订阅观察者。

var unsubscribe = firebase.auth().onAuthStateChanged(function(user) {
    unsubscribe();
    $scope.currUser = user;
    if (user) {
        $scope.go('profile') //function to change states.
    } else {
    // No user is signed in.
        $scope.go("login") //login is where the app starts (login page)
    }
    });
© www.soinside.com 2019 - 2024. All rights reserved.