2016-09-20 2 views
0

EDIT×2角度UIルータ - 約束し、あなたの提案に続いて、再帰的なリダイレクト問題が

、私は多くの変更を行いました。

今回は本当に約束しましたが、いくつか問題がありました。私は最終的に私は今、私の約束の作品を期待リダイレクトのためにそれを作った

 // If it is, return true 
    else { 
     console.log('Valid token'); 
     deferred.resolve(true);  // Removed "return" 
    } 
} 
// If there's no valid token, return false 
else { 
    console.log('No token'); 
    $localStorage.$reset(); 
    $state.go('login'); 
    deferred.reject(false);   // Removed "return" 
} 

:私はこれを変更しなければならなかった

 // If it is, return true 
    else { 
     console.log('Valid token'); 
     return deferred.resolve(true); 
    } 
} 
// If there's no valid token, return false 
else { 
    console.log('No token'); 
    $localStorage.$reset(); 
    $state.go('login'); 
    return deferred.reject(false); 
} 

:あなたはこのためにいくつかの時点では動作しませんでした提案は何

...

// Watching login page 
$transitions.onStart({to: 'login'}, function (trans) { 

    // Injecting the authentication service 
    var auth = trans.injector().get('AuthService'); 

    // returning the promise with handlers 
    return auth.isAuthenticated().then(function (res) { 

     // If the token is valid, redirect to the dashboard 
     return trans.router.stateService.target('dashboard.home'); 
    }, function(e) { 

     // If the token is invalid or missing, keep the login page 
     return trans.router.stateService.target; 
    }); 
}); 

// Watching the dashboard private page 
$transitions.onStart({to: 'dashboard.**'}, function (trans) { 

    // Injecting the authentication service 
    var auth = trans.injector().get('AuthService'); 

    // returning the promise with handlers 
    return auth.isAuthenticated().then(function (res) { 

     // If the user is correctly identified, do nothing 
     return trans.router.stateService.target; 
    }, function (e) { 

     // If the token is invalid or missing, deleting datas 
     $localStorage.$reset(); 

     // Setting error message 
     $localStorage.loginError = {'token_expired': true}; 

     // Redirecting to the login page 
     return trans.router.stateService.target('login'); 
    }) 
}); 
+0

にリダイレクトを処理します。上のコードでは、約束を返せません。 –

+0

こんにちはShaiilendra、私はこのアドオンの1.0ベータ2版とちょっと混乱しています。私は新しいTransitionシステムがちょっと複雑になっています...これを手助けする例はありますか? 実際、ログインは正常に動作しています。トークンの確認と更新も問題ありません。私の問題は、認証されていない場合でもダッシュボードにアクセスするのを防ぎ、ログインインなしでダッシュボードに更新可能なトークンがまだ残っている場合に直接アクセスできるようにすることです。 これはこれが失敗するところです。私が最初のもの(delog user)を使っているだけならOKです。私は2つを置く場合、私は無限ループに入り、なぜ見ることができません! –

答えて

0

isAuthenticatedメソッドでは約束を返せません。また、リダイレクトの問題が発生する可能性があるので、をisAuthenticatedから削除してください。この方法は、ここで

vm.isAuthenticated = function() { 
       var deferred = $q.defer(); 
       var ts = Math.round((new Date()).getTime()/1000); 

       // Getting token datas 
       var exp = vm.getClaimsFromToken(); 

       console.log('Expire dans : ' + (exp.exp - ts) + 's'); 

       // Check if hte token exist 
       if ($localStorage.token) { 

        // Check if it is still valid 
        if (ts > exp.exp) { 

         // Refresh the token 
         return vm.refreshToken().then(
          function(res) { 
           if (res) { 
            console.log('Refreshing Really Done'); 
            console.log(res); 
            return deferred.resolve(res); 
           } 
          }, 
          // Handle error 
          function (e) { 
           if (!e) { 
            console.log('Refreshing Failed'); 
            $localStorage.$reset(); 
            // $state.go('login'); 
            return deferred.reject(e); 
           } 
          } 
         ) 
        } 
        // If it is, return true 
        else { 
         console.log('Valid token'); 
         return deferred.resolve(true); 
        } 
       } 
       // If there's no valid token, return false 
       else { 
        console.log('No token'); 
        $localStorage.$reset(); 
        // $state.go('login'); 
        return deferred.reject(false); 
       } 
      return deferred.promise; 
      }; 

れたまま、この方法はreturn deferred.promise最初に、あなたの方法のロジックに基づいて約束を返しているように、それが解決するか、約束を拒否している必要があります。

は今、あなたは、あなたが実際にその状態に入る前に、ユーザーを認証しますた状態(UIルータ)との「決意」を、使用しようとする必要があり、このようなway-

// Watching login page 
$transitions.onStart({to: 'login'}, function (trans) { 

    // Injecting the authentication service 
    var auth = trans.injector().get('AuthService'); 

    // returning the promise with handlers 
    auth.isAuthenticated().then(function (res) { //remove return keyword 

     // If the token is valid, redirect to the dashboard 
     return trans.router.stateService.target('dashboard.home'); 
    }, function(e) { 

     // If the token is invalid or missing, keep the login page 
     // return trans.router.stateService.target; //removed this line 
    }); 
}); 

// Watching the dashboard private page 
$transitions.onStart({to: 'dashboard.**'}, function (trans) { 

    // Injecting the authentication service 
    var auth = trans.injector().get('AuthService'); 

    // returning the promise with handlers 
    auth.isAuthenticated().then(function (res) { //remove return keyword 

     // If the user is correctly identified, do nothing 
     // return trans.router.stateService.target; //removed this line 
    }, function (e) { 

     // If the token is invalid or missing, deleting datas 
     $localStorage.$reset(); 

     // Setting error message 
     $localStorage.loginError = {'token_expired': true}; 

     // Redirecting to the login page 
     return trans.router.stateService.target('login'); 
    }) 
}); 
+0

これはリダイレクトループを解決しません。私はまだ.run $移行オプションを使ってループを起こしています。理由を見つけることができません。私はあなたが提案したすべての変更を行いました。 私の.runステートメントを見れば、このようなループのものがどこにあるのかを見ることができます。私はおそらくリダイレ​​クトが "before"ステートメントの1つを避けるためにどこから来ているかを確認する必要があります。他の1つですが、これは以前のバージョンのUIルーターで古い状態のイベントを処理していた私の理解度を上回ります。 –

+0

@FrançoisH。私はコードを変更しました。これを確認して、両方の部分でロジックを変更して認証を処理しました。まだ問題が見つかった場合はお知らせください。 –

関連する問題