EDIT 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');
})
});
您没有在isAuthenticated
方法中返回承诺,并且还从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
,根据你的方法的逻辑,它是解决或拒绝承诺。
现在以这样的方式处理重定向-
// 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');
})
});