2016-09-20 76 views
0

編輯X2角UI路由器 - 承諾和遞歸重定向問題

按照你的建議,我犯了一個很大的變化。

我做了(我認爲?)這次是真正的承諾,但有幾個問題。

什麼你建議在某些時候對於這個沒有工作:

 // 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); 
} 

我不得不這樣更改爲:

 // 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" 
} 

我終於做到了,因爲我現在希望我的諾言作品重定向...

// 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

你應該嘗試使用「解析」與國家(UI路由器),在其中你會真正進入到狀態之前驗證用戶。在上面給出的代碼中,你沒有回覆諾言。 –

+0

嗨Shaiilendra,我對這個插件的1.0 beta 2版本有點困惑,我發現新的Transition系統有點複雜......你有任何例子來幫助我處理這個問題嗎? 事實上,我的登錄工作正常。檢查和更新令牌也可以。我的問題是我想阻止人們訪問儀表板,如果沒有進行身份驗證,並允許他們直接訪問,如果他們仍然有一個可更新的令牌到儀表板沒有登錄。 這是失敗的地方。如果我只使用第一個(delog用戶)就沒關係。如果我把這兩個,我進入一個無限循環,不明白爲什麼! –

回答

0

您不會在您的isAuthenticated方法中返回承諾a nd也從isAuthenticated方法中刪除state.go('login'),因爲它可能會導致重定向問題。這個方法應該是這樣的原樣

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並根據您的方法的邏輯,它是解決或拒絕承諾。

現在處理重定向在這樣的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」語句之一與另一個,但這超過了我的理解,因爲它與老版本的UI路由器一起處理舊的狀態事件。 –

+0

@FrançoisH。我修改了代碼。現在檢查它,我已經改變了兩個部分的邏輯來處理認證。如果您仍然發現任何問題,請告知我。 –