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

后端 未结 4 1616
暗喜
暗喜 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:25

    the code should be something like this

         $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState)                { //, fromParams
           console.log(toState.name + "," + fromState.name);
            if(fromState.name === "") {
              if (Auth.isLoggedIn()) {
                  $state.go('welcome');
                   event.preventDefault();
              } else {
                $state.go('home');
                   event.preventDefault();
            } else {
               if (toState.authenticate && !Auth.isLoggedIn()) {
                   $toState.go('login');
                   event.preventDefault();
               }
            }
        }
    

    so if user entering the application, then if he is logged in take him to welcome else take him to home.

    once he is inside, then if he hits some route which needs auth.. then redirect him to login page..

    sorry if i did not understood you requirement fully...

提交回复
热议问题