Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UI Router $state.go is not redirecting?

This is a common issue, I see it a ton but nothing seems to work. Here is what I'm doing. I want to have some dynamic animations with my states, so basically login will do some cool animations and move into the actual interface. Now, I started nesting the views like this:

    $stateProvider
        .state('login', {
            url: '/login',
            title: "Login",
            views: {
                "master": {
                    controller: "LoginController",
                    templateUrl: "/components/login/login.html",
                    authentication: false
                }
            }
        })
        .state("logout", {
            url: "/logout",
            title: "Logout",
            authentication: false
        })
        .state('foo', {
            url: '/',
            controller: "HomeController",
            views: {
                "master": {
                    templateUrl: '/components/ui-views/masterView.html'
                },
                "sidebar@foo": {
                    templateUrl: '/components/sidebar/_sidebar.html'
                },
                "header@foo": {
                    templateUrl: '/components/header/_header.html'
                }
            }
        })
        .state('foo.inventory', {
            url: '/inventory',
            title: "Inventory",
            views: {
                "content@foo": {
                    controller: "InventoryController",
                    templateUrl: "/components/inventory/inventory.html"
                }
            }
        });

So while running this I need to redirect to logout, but it gets stuck. It won't move from this logout state at all. Here is how I'm handling that:

function run($http, $rootScope, $cookieStore, $state, $templateCache, $timeout, AuthService, modalService, const) {
        var timeout;

        FastClick.attach(document.body);
        $rootScope.globals = $cookieStore.get('globals') || {};

if ($state.current.name !== 'login' && !$rootScope.globals.guid) {
            $state.go('login');
        } else if ($state.current.name == 'login' && $rootScope.globals.guid) {
            $state.go('foo');
        }

        $rootScope.$on('$stateChangeStart', function (event, next, current, fromState, fromParams) {
            var authorizedRoles = next.level != undefined ? next.level : 1,
                needAuth = next.authentication != undefined ? next.authentication : true;

            if (needAuth) {
                if (!AuthService.isAuthorized(authorizedRoles, true)) {
                    event.preventDefault();
                    if (AuthService.isAuthenticated()) {
                        $rootScope.$broadcast(const.auth.notAuthorized);
                        $state.go('foo', {});
                    } else {
                        $rootScope.$broadcast(const.auth.notAuthenticated);
                    }
                }
            }

            if (next.name == 'logout') AuthService.logout($rootScope.globals);
        });

}

So why would this not work? It seems like this work fine. But the $state.go('login') returns a bad value.

If anyone could guide me in the right direction, or tell me what is wrong exactly.

like image 510
Mike Huebner Avatar asked Dec 05 '22 23:12

Mike Huebner


1 Answers

The issue is that the $state.go is in the run, there doesn't seem to be many docs on this topic. $state.go is not initialized yet and will not run.

like image 145
Mike Huebner Avatar answered Dec 19 '22 20:12

Mike Huebner