My ui-router configuration is as follows:
$stateProvider .state('app', { url: '/app', abstract: true, templateUrl: 'sidemenu/sidemenu.html', controller: 'SideMenuCtrl' }) .state('login', { url: '/login', templateUrl: 'login/login.html', controller: 'LoginCtrl' }) .state('app.dashboard', { url: '/dashboard', views: { menuContent: { templateUrl: 'dashboard/dashboard.html', controller: 'DashboardCtrl' } } }) [More states here] $urlRouterProvider.otherwise('/app/dashboard');
When the user goes to /app/dashboard , I would like to check if they are allowed, and then:
- redirect to
/login if they are not authorized, or - allow you to see the control panel if they are authorized
This looks like a trick: (based on this example )
$rootScope.$on('$stateChangeSuccess', function(event) { var path = $location.path(); if (path === '/login') { return; } // Halt state change from even starting event.preventDefault(); Auth.getCurrentUser().then(function(currentUser) { if (currentUser === null) { $state.go('login'); } else { // Continue with the update and state transition $urlRouter.sync(); } }); });
The only problem with this solution is that if an unauthorized user navigates to the control panel, the toolbar template is visible first . Only when the authorization response is returned does it change to the login template.
Is there a way to not show the control panel while we authorize the user?
I also tried changing $stateChangeSuccess to $stateChangeStart , but it really didn't work (routing seems completely broken then).
source share