Divert to alternate homepage if user is not logged in using UI-Router & AngularJS

后端 未结 4 1610
暗喜
暗喜 2020-12-17 06:59

I would like to have two home pages, the first would be for users who have not logged in and the second for users that are logged in.

This is my current set up:

4条回答
  •  甜味超标
    2020-12-17 07:09

    I had problem like this and I solved it like this

     .run(function ($rootScope, $location, AuthService) {
    
            // start showing PRELOADER because we doing async call and we dont know when it will be resolved/rej
            AuthService.checkLoginStatus().then(
                (resp) => {
                    // fire logged in user event
                    $rootScope.$broadcast('userLogged',resp);
                    $location.path(YourHomePageUrl);
    
                },
                (err)=> {  
                    // Check that the user is not authorized redirect to login page                  
                        $location.path(loginURL);
                    }
                }).finally(
                ()=> {
                    // finnaly Set a watch on the $routeChangeStart
                    /// Hide PRELOADER
                    $rootScope.$on('$routeChangeStart',
                        function (evt, next, curr) {
                          if (!AuthService.isLoggedIn()) {
                                    $location.path(art7.API.loginURL);
                                }
    
                        });
                }
            )
    
    
        });
    

    and im not using interceptor for handling 401 not authorized errors, thats my solution

提交回复
热议问题