0

Im using angular-permission library. I defined some of permissions in run():

    PermPermissionStore
    .definePermission('isAuthorize', function () {
        console.log('isAuthorize', OAuth.isAuthenticated());
        return OAuth.isAuthenticated();
    });

and then im checking users permissions in my routes.js

            .state('app.main', {
            abstract: true,
            template: '<ui-view></ui-view>',
            data: {
                permissions: {
                    only: ['isAuthorize'],
                    redirectTo: {
                        default: 'login'
                    }
                }
            }
        })

But i need to ask for profileData first on every refresh of the page, and permission-checking needs to run after i resolve promise. I've try to add resolve on parent State like this, but it still first asking for permisson for app.main and then promise is being resolved.

            .state('app', {
            abstract: true,
            template: '<ui-view></ui-view>',
            controller: function ($rootScope, userData) {
                $rootScope.roles = userData.user.roles;
            },
            resolve: {
                userData: ["ProfileFactory", function (ProfileFactory) {
                    return ProfileFactory
                        .get()
                        .$promise;
                }]
            }
        })

1 Answers1

1

I have not used angular permissions module. But if you are using ui.router it's very easy to implement by your own. Here is an example from the doc's.

https://ui-router.github.io/docs/latest/classes/transition.transition-1.html#onbefore

Sample use of onBefore hook

  .run(($transitions, $state, $injector) => {
    $transitions.onBefore({
      to: 'app.**'
    }, () => {
      const $window = $injector.get('$window');
      if (!$window.sessionStorage.getItem('user')) {
        return $state.target('login', $state.transition.params());
      }
      return true
    });
  });

Without login as test@gmail.com and password 123 you can't go to the home page

https://plnkr.co/edit/ZCN2hB34mMEmBJulyaAJ?p=preview

fernando
  • 707
  • 8
  • 24
  • it may work, but when i've tried to use $transitions i recived "Unknown provider: $transitionsProvider" I also find this: https://github.com/angular-ui/ui-router/issues/2891 But i think there must be way to do onBefore transition somehow. – Konrad Straszewski Nov 28 '16 at 09:03
  • $transitions needs ui-router1.0.0 – Konrad Straszewski Nov 28 '16 at 12:14
  • I was assuming that you are using new version of ui.router. For the old versions you can do something like, $rootScope.$on('$stateChangeStart', (ev, toState, toStateParams) => authService.authorize(ev, toState, toStateParams)); – fernando Nov 29 '16 at 00:10