按照你的建議,我犯了一個很大的變化。
我做了(我認爲?)這次是真正的承諾,但有幾個問題。
什麼你建議在某些時候對於這個沒有工作:
// 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');
})
});
你應該嘗試使用「解析」與國家(UI路由器),在其中你會真正進入到狀態之前驗證用戶。在上面給出的代碼中,你沒有回覆諾言。 –
嗨Shaiilendra,我對這個插件的1.0 beta 2版本有點困惑,我發現新的Transition系統有點複雜......你有任何例子來幫助我處理這個問題嗎? 事實上,我的登錄工作正常。檢查和更新令牌也可以。我的問題是我想阻止人們訪問儀表板,如果沒有進行身份驗證,並允許他們直接訪問,如果他們仍然有一個可更新的令牌到儀表板沒有登錄。 這是失敗的地方。如果我只使用第一個(delog用戶)就沒關係。如果我把這兩個,我進入一個無限循環,不明白爲什麼! –