Limit user access in an Ionic app

1

I am creating an app with ionic and I want that, when the user not is logged in and goes to a page that is not the login or the registration, it takes it to the login. In case the user wants to go to the registry, let the app allow it.

My code is as follows:

$rootScope.$on('$stateChangeStart', function(event, toState, fromState){
    if(!UserFactory.isLoggedIn() && (toState.name !== 'login' || toState.name !== 'register') ){
        event.preventDefault();
        $state.go('login');
    }
});

But does it generate a loop for me, any solution?

    
asked by justMiLa 09.06.2016 в 19:26
source

3 answers

1

You can create a function with a promise that verifies if the user is authenticated and if not, that redirects to the login.

Then, in each state that a logged-in user is required, you solve that function. For example:

  $stateProvider
    .state('home', {
      url: '/',
      templateUrl: 'components/home/home.html',
      controller: 'homeCtrl as home',
      resolve: {
        authenticate: authenticate
      }
    })
  .state('login', {
    url: '/login',
    templateUrl: 'components/login/login.html',
    controller: 'loginCtrl as login'
  });
  // Además, cuando el usuario hace login almaceno su token en el localstorage.
  // Al iniciar la aplicación si no hay token es que no hay usuario logueado,
  // por lo que redirijo directamente al login.

  if (!(typeof window.localStorage.token !== 'undefined' &&
      window.localStorage.token !== null)) {
    $urlRouterProvider.otherwise('/login');
  } else {
    $urlRouterProvider.otherwise('/');
  }

Being my function to authenticate the following:

function authenticate($ionicPlatform, $q, Auth) {
// Auth es mi propio servicio que se encarga del login/logout.
  if (Auth.isLogged()) {
    return $q.resolve();
  } else {
    Auth.logout();
    return $q.reject();
  }
}

and Auth.logout () I have a $state.go('login'); at the end to direct the login.

    
answered by 11.10.2016 в 13:29
0

Only the conditional changes. Enter and instead of or

$rootScope.$on('$stateChangeStart', function(event, toState, fromState){
    if(!UserFactory.isLoggedIn() && toState.name !== 'login' && toState.name !== 'register'){
        event.preventDefault();
        $state.go('login');
    }
});

In the previous way if the user is not logged in and the status is 'login' the result of the comparison would be as follows

// no logueado estado login
if(true && (false || true)){
     // Esto es true y vuelve a login
}

// no logueado estado registro
if(true && (true || false)){
     // Esto es true y vuelve a login
}

// no logueado otro estado
if(true && (true || true)){
     // Esto es true y vuelve a login
}

The way I put you

// no logueado estado login
if(true && false && true)){
     // Esto es false y no hace nada
}

// no logueado estado registro
if(true && true && false)){
     // Esto es false y no hace nada
}

// no logueado otro estado
if(true && true && true)){
     // Esto es true y vuelve a login
}
    
answered by 09.06.2016 в 19:46
0

Another option is something like this:

        .state('app', {
            url: "/app",
            cache: false,
            abstract: true,
            templateUrl: "pagina.html",
            controller: 'AppCtrl',
            onEnter: function($state, Auth){
                if(!UserFactory.isLoggedIn()){

                   $state.go('login');

                }
            }                
        })

So I get that before entering go to the login, I had tried what you said but it gave me errors and with this option I am doing well.

For the record that I am starting and I am not a guru so if there is an error, do not be too hard:)

    
answered by 12.06.2016 в 23:49