Where should I call $ionicHistory.clearHistory when changing state?

Hi everyone,

we have an application where we have two sets of states: the states where the user is authenticated (they’re all children of “site” state) and the states where the user is NOT authenticated (they’re all children of “login” state). We want to clear the history when going from one set of states to another, that is, when the user authenticates or logouts.

Our first thought was to add $ionicHistory.clearHistory() in the onEnter functions of “site” and “login” states, but that doesn’t seem to be working right: we still have the previous view in the history. So I wanted to ask, where should I clear the history to achieve this behaviour?

Our states are like this:

$stateProvider

// Login parent state.
.state('login', {
    url: '/login',
    abstract: true,
    template: 'login.html',
    cache: false,
    onEnter: function($ionicHistory) {
        $ionicHistory.clearHistory();
    }
})
// Login sub states.
.state('login.init', {
    url: '/init',
    template: 'init.html'
})

... (More login states)

// Site parent state (authenticated).
.state('site', {
    url: '/site',
    abstract: true,
    template: 'site.html',
    cache: false,
    onEnter: function($ionicHistory) {
        $ionicHistory.clearHistory();
    }
})
// Site sub states.
.state('login.index', {
    url: '/index',
    template: 'index.html'
})

... (More site states)

Any help would be appreciated.

Thanks!