按照你的建议,我犯了一个很大的变化。
我做了(我认为?)这次是真正的承诺,但有几个问题。
什么你建议在某些时候对于这个没有工作:
// 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用户)就没关系。如果我把这两个,我进入一个无限循环,不明白为什么! –