Angular ui-router $state.go is not redirecting inside resolve

后端 未结 6 963
刺人心
刺人心 2021-02-05 04:57

In my angularjs app, I am checking if user lands on landing page and is already authenticated, then redirect him to home page.

.state(\'landingpage\', {
                 


        
6条回答
  •  既然无缘
    2021-02-05 05:32

    You can use resolve to provide your controller with content or data that is custom to the state. resolve is an optional map of dependencies which should be injected into the controller.

    You could have a controller that checks for the AuthState and have the redirection accordingly.

         .state('landingpage', {
            abstract: "true",
            url: "/landingpage",
            templateUrl: "app/landingpage/landingpage.html",
            resolve: {
                AutoLoginCheck: ['$window', function ($window) {
    
                    if($window.localStorage.access_token != null)
                    {
                        if($window.sessionStorage.access_token == null) {
                            $window.sessionStorage.access_token = $window.localStorage.access_token;
                        }
    
                       //assuming userInfoService does the authentication
                        var isAuthenticated = userInfoService.SetUserAuthenticated(true);
                        return isAuthenticated;
    
                    }
                }]
            },
            controller: ['$state','AutoLoginCheck', function($state, AutoLoginCheck){
              if(AutoLoginCheck){
                //authenticated
                $state.go('app.home');
              } else {
                //redirect to unauthenticated page
                $state.go('....');
              }
            }]
        })
    

提交回复
热议问题