5

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');
davidlee
  • 5,611
  • 17
  • 56
  • 82
  • I have the same problem, it does not clear the cache or history. I read in docs that $ionicHistory will only clear the cache of all the other views except the view you are currently on. I need answer to this same question. – Karan Kumar May 29 '15 at 04:46
  • take a look at this http://stackoverflow.com/questions/29841166/reset-unload-controller-after-changestate-logout-called-angularjs – Karan Kumar May 29 '15 at 05:01

3 Answers3

14

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.
LeftyX
  • 35,328
  • 21
  • 132
  • 193
5

$ionicHistory.clearCache() now returns promise so you can ensure cache is cleared. So you can call like this:

$ionicHistory.clearCache().then(function() {
    //now you can clear history or goto another state if you need
    $ionicHistory.clearHistory();
    $ionicHistory.nextViewOptions({ disableBack: true, historyRoot: true });
    $state.go('login');
})

There is no need for the above timeout like things.

credit goes to

Tanweer
  • 567
  • 1
  • 5
  • 17
2

This is because ionic is caching the view, So that will stop ionic going through the controller.

So you could bust the cache as follows

<ion-view cache-view="false" view-title="My Title!">
  ...
</ion-view> 

read here for more

sameera207
  • 16,547
  • 19
  • 87
  • 152