Ionic/angular js : When hardware backbutton is clicked twice. App must exit

I am working on an login based application. So if i press back button from login page app must exit

app.run(function($ionicPlatform, $state){
  $ionicPlatform.registerBackButtonAction(function (event) {
    if ( ($state.$current.name=="app.login") ||
         ($state.$current.name=="app.discussions")
        ){
            // H/W BACK button is disabled for these states (these views)
            // Do not go to the previous state (or view) for these states. 
            // Do nothing here to disable H/W back button.
        } else {
            // For all other states, the H/W BACK button is enabled
            navigator.app.backHistory();
        }
    }, 100);

})

Right now i am using this code, but it triggers if back-button is hit once.

Indeed this code triggers at the first touch as it is intended to do so.
To manage the 2nd click, you could set up a timer for 500ms for instance, with a boolean and if the button is clicked again within this time frame, then you quit. Otherwise, you reset the boolean.