1

I get an infinite digest loop inside my $stateChangeStart handler when the app initially loads.

I have already reviewed the current postings on this. While my problem seems to be exactly as these posts state, none of the solutions and answers proposed nor the accepted answers solve my particular issue. I am not trying to solve the authorization topic, I am trying to resolve permission to go to route based on the user's role.

I added the following object to the $state object when a route is defined

 $stateProvider.state('routeName', {
    // ...
    authorization: {
        allowAll: false,
        allowRoles: [
            'admin',
            'power',
            'user',
            'guest'
        ],
        denyAll: false,
        denyRoles: [{
            role: 'user',
            route: 'dashboard',
            data: {
                //...
            }
        },{
            role: 'power',
            route: 'dashboard',
            data: {
                //...
            }
        }],
        failover: "home"
    }
});

I have this as my handler for $stateChangeStart

$rootScope.$on('$stateChangeStart', function (e, toState, toParams, fromState, fromParams) {

    var userRole = UserFactory.role;

    // default to app route if route 'failover' not defined
    var failOver = toState.authorization.failover || 'home';

    // User is allowed to continue through to the state.
    if (toState.authorization.allowAll ||
        _.contains(toState.authorization.allowRoles, userRole)) {
      // Do nothing user is allowed this route
        return;
    } else if (!toState.authorization.denyAll &&
        toState.authorization.denyRoles.length > 0) {

        // User is denied this route find the route to redirect to
        var entry = _.find(toState.authorization.denyRoles, function (item) {
            return item.role == userRole;
        });

        if (entry) {
            e.preventDefault();
            $state.go(entry.route, entry.data || { });

            return;
        }
    }

    // All deny conditions not specifically targeted
    e.preventDefault();
    $state.go(failOver);
});

Here is the full plunker.

Community
  • 1
  • 1
Itanex
  • 1,664
  • 21
  • 33
  • Wanted to assist, but with your plunker, I was not reproduce the infinite loop... Is it broken or working plunker? ... just if you still search for assistance... – Radim Köhler May 06 '15 at 11:54
  • Yeah the plunker is broken, though i ended up figuring out my issue. turns out there is an issue in the UIRouter itself that when my code runs on certain routes of mine, the competition creates the infinite loop. I have been rather busy to write back on this. I am coming of this project soon so I will be working out this code as a sampler for auth in routing. :) – Itanex Jun 04 '15 at 17:49

0 Answers0