0

I want that if a user is logged in he can't go to the login and OTP page by hardware back button.

.controller('viewoemCtrl', function($rootScope,$scope,$http,$ionicPopup,$state,$ionicHistory,$ionicLoading,productService,$ionicPlatform) {
$scope.user = {};  //declares the object user

 $ionicPlatform.onHardwareBackButton(function () {
    if (true) { // your check here
        $ionicPopup.confirm({
            title: 'Exit from App!',
            template: 'are you sure you want to exit?'
        }).then(function (res) {
            if (res) {
                navigator.app.exitApp();
            }
        })
    }
 })
 })
Rahi.Shah
  • 1,305
  • 11
  • 20

1 Answers1

0

You don't need to check the click on the back button, because that itself will call a state transition from ui-router. just register a callback to the $stateChangeStart event fired by ui-router and control there if the user is logged in and if the target state is login, in this case you prevent the transition. This will handle any case of state transition (the back button, a direct link from the menu etc.)

 $rootScope.$on('$stateChangeStart', function(event, toState, toParams,
  fromState, fromParams){ 
   //userIsLogged is a flag you should retrieve from your code
   if(userIsLogged && toState === 'login'){ 
     event.preventDefault(); 
   }
 })

take a look at the documentation

Karim
  • 8,454
  • 3
  • 25
  • 33