【发布时间】:2015-04-09 23:27:40
【问题描述】:
您好,我正在开发一个 Angular 应用程序,并且在尝试重试在 http 拦截器中发出的最新请求时遇到了一些问题。 我有这个拦截器用于对每个请求进行身份验证验证,并且我在令牌到期的响应中也有它。问题是,当我的令牌过期时,我需要触发刷新令牌以获取新的访问权限,如果成功,则重试拦截的请求。
一切正常。但是...我有多个被拒绝的 401 呼叫,当我尝试登录时(您可以看到 console.log 消息)。似乎只有第一个被拒绝的调用才会触发刷新令牌请求。什么是最好的方法或可以使它起作用的方法?提前 Ty。
var _responseError = function (rejection) {
if (rejection.status === 401) {
var authService = $injector.get('authService');
var state = $injector.get('$state');
var http = $injector.get('$http');
var deferred = $q.defer();
if (rejection.data && rejection.data.ErrorCode == errorCodes.missingRole){
return $q.reject(rejection);
}
//var authData = localStorageService.get(authConstant.cookieName);
var authData = authService.getAuth();
if (authData) {
return authService.refreshToken().then(function (response) {
console.log("call is: ", rejection);
if (response.access_token){
//Todo: find a method to resend last request;
return http(rejection.config);
}
},
function (err) {
state.go('app.login');
return $q.reject(rejection);
});
}
authService.logOut();
state.go('app.login');
}
return $q.reject(rejection);
};
【问题讨论】:
标签: javascript angularjs http request interceptor