编辑x2
根据你的建议,我做了很多改变。
这次(我认为?)这是一个真正的承诺,但有一些问题。
你提出的建议在某些方面没有起作用:
// 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 :(得分:0)
您未在isAuthenticated
方法中退回承诺,也从state.go('login')
方法中删除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
,并根据您的方法逻辑,它正在解决或拒绝承诺。
现在以这种方式处理重定向 -
// 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');
})
});