2016-07-06 2 views
1

私は3ページA、B、Cを持っています。角度jsでの認証の後、要求されたページに送り返す。

  • ユーザー(ユーザーがログインしているか否か)
  • ユーザーがログインページよりログインしていない場合は
に送り返さログインが成功した後
  • を、表示された認証用
  • チェックをクリックしてください

    今私がこれまで行ってきたこと。ここで認証されたページにユーザーをクリックすると、我々は、ユーザーが送信

    $stateProvider 
        .state("login", { 
         url: '/login', 
         controller: 'LoginController', 
         data : { pageTitle : 'Home', requiresAuth: true } 
         params : {'returnTo' : null } 
    }); 
    
  • 状態をログインするのparamsを追加し、の.configブロックにnullに設定私のworking plunker

    1. です追加のパラメータを含むログインページ$state.target

      $transitions.onBefore(requiresAuthCriteria, redirectToLogin, {priority: 10}); 
      
      var requiresAuthCriteria = { 
      to: function (state) { 
          return state.data && state.data.requiresAuth; 
      } 
      }; 
      var redirectToLogin = function($transition$, $injector) { 
      var AuthService = $injector.get('AuthService'); 
      if (!AuthService.isAuthenticated()) { 
          return $state.target('login', { returnTo : $transition$.to().name }); 
      } 
      

      };

    2. ログインコントローラーで、ログインに成功した後、私たちは、この放送イベントをキャッチし、$state.go

       $rootScope.$on(AUTH_EVENTS.loginSuccess, function(event) { 
           $timeout(function() { $state.go($rootScope.returnTo); }); 
           }); 
      

    しかし、私経由で要求されたページにユーザーを送る.runブロックでrootScope.broadcastイベント

    login 
        .controller('LoginController', function($rootScope, AUTH_EVENTS, AuthService, $stateParams){ 
        var self = this; 
        self.login = function(credentials){ 
        AuthService.login(credentials).then(function(user){ 
         $rootScope.returnTo = $stateParams.returnTo; 
         $rootScope.$broadcast(AUTH_EVENTS.loginSuccess); 
        }, function(){ 
         $rootScope.$broadcast(AUTH_EVENTS.loginFailed); 
        }); 
    }; 
    
  • にパラメータをバインドします私が認証を確認する必要があるので、これは有効なアプローチではないと思います。私はresolve

    .state
    resolve : { returnTo : returnTo } 
    
    function returnTo ($transition$) { 
        var redirectedFrom = $transition$.previous(); 
        // The user was redirected to the login state (via the requiresAuth hook) 
        if (redirectedFrom !== null) { 
         // Follow the current transition's redirect chain all the way back to the original attempted transition 
         while (redirectedFrom.previous()) { 
          redirectedFrom = redirectedFrom.previous(); 
         } 
         return { state: redirectedFrom.to(), params: redirectedFrom.params("to") }; 
        } 
        var fromState = $transition$.from(); 
        var fromParams = $transition$.params("from"); 
        console.log(fromState); 
        if (fromState.name !== '') { 
         return {state: fromState, params: fromParams}; 
        } 
        // If the fromState's name is empty, then this was the initial transition. Just return them to the home state 
        return { state: 'base' }; 
    } 
    

    と試みたが、これは動作していない理由を理解していません。親切に私はこの問題を解決するのに役立ち

  • 答えて

    0

    は今、代替resolve属性を使用しているさまざまな記事を

    サーフィンの多くの後に解決策を得ました。 params属性を削除し、それがすべてのコードログイン状態に

    1. だ、resolveプロパティを追加しても、ユーザが一度にログイン/ログインにアクセスできないようにonEnterプロパティを追加します。

      .state("login", { 
           url: '/login', 
           templateUrl: './user/login.html', 
           controller: 'LoginController', 
           controllerAs : 'vm', 
           resolve: { returnTo: returnTo }, 
           onEnter: function($state, AuthService){ 
            if(AuthService.isAuthenticated()) { 
             return $state.target('base', undefined, { location: false }); 
            } 
           } 
          }) 
      
    2. 本当の謎は、この$transitions$が関数の内部で利用可能でない方法ですreturnTo機能copied from here

      function returnTo ($transition$) { 
          var redirectedFrom = $transition$.previous(); 
          // The user was redirected to the login state (via the requiresAuth hook) 
          if (redirectedFrom !== null) { 
           // Follow the current transition's redirect chain all the way back to the original attempted transition 
           while (redirectedFrom.previous()) { 
            redirectedFrom = redirectedFrom.previous(); 
           } 
           // return to the original attempted "to state" 
           return { state: redirectedFrom.to(), params: redirectedFrom.params("to") }; 
          } 
          // The user was not redirected to the login state; they directly activated the login state somehow. 
          var fromState = $transition$.from(); 
          var fromParams = $transition$.params("from"); 
          if (fromState.name !== '') { 
           return {state: fromState, params: fromParams}; 
          } 
          // If the fromState's name is empty, then this was the initial transition. Just return them to the home state 
          return { state: 'base' }; 
      } 
      

    を定義!!!、誰かが

    感謝するであろうよりも説明することができます
    1. returnTo機能

      login 
          .controller('LoginController', function($rootScope, AUTH_EVENTS, AuthService, $state, returnTo){ 
          var self = this; 
          self.login = function(credentials){    
           AuthService.login(credentials).then(function(user){ 
           $rootScope.$broadcast(AUTH_EVENTS.loginSuccess); 
           AuthService.setCurrentUser(user); 
           $state.go(returnTo.state, returnTo.params, {reload: true }); 
          }, function(){ 
           $rootScope.$broadcast(AUTH_EVENTS.loginFailed); 
          }); 
      }; 
      

    からオブジェクトとしてリターンたstateparams値を使用して状態を変更し、.loginControllerおよびログインが成功した後に(関数名そのまま)$statereturnToを注入それが誰かを助けることを願って

    関連する問題