davidlee davidlee - 5 months ago 126
AngularJS Question

can't logout from ionic

Hi I have an issue with ionic login and logout.

Each time after logout, i can still click the back button and it will bring me back to my previous page. may i know how to clear or delete session when logout so that user unable to go back to previous page from the login?

var default_stat;
$scope.logout = function(){
$ionicLoading.show({template:'Logging out....'});
$localstorage.set('loggin_state', '');
$state.go('login');
$ionicLoading.hide();
$ionicHistory.clearHistory();
$ionicHistory.clearCache();
};


during login i use localstorage to indicate user has logged in

$localstorage.set('loggin_state', '1');

Answer

I would do something like this:

$scope.logout = function(){
    $ionicLoading.show({template:'Logging out....'});
    $localstorage.set('loggin_state', '');

    $timeout(function () {
        $ionicLoading.hide();
        $ionicHistory.clearCache();
        $ionicHistory.clearHistory();
        $ionicHistory.nextViewOptions({ disableBack: true, historyRoot: true });
        $state.go('login');
        }, 30);

};

I've found out that adding a little delay allow $ionicHistory to clear the cache.

$ionicHistory.nextViewOptions({ disableBack: true, historyRoot: true });
  • disableBack: The next view should forget its back view, and set it to null.
  • historyRoot: The next view should become the root view in its history stack.