2
votes

I've used auth().onAuthStateChanged() in my Ionic app so when the user is logged in, the app goes to user profile $state but the problem is, since the observer keep checking for changes I am unable to change the $state, whenever I try it changes back to user profile $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)
    }
    });

now when I'm in profile $state and try to go to another state - dashboard for example it goes then come back to profile state on it's own within a second.

Kindly help me fix this and thanks in advance

1

1 Answers

0
votes

If you only need to verify that the user is logged in once, you can unsubscribe from the observer.

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)
    }
    });