我正在创建一个社交平台,其中使用oauth2.0令牌系统完成用户身份验证,当accessToken过期时,我使用Angularjs OAuth.getRefreshToken();
请求refreshToken
之后我再次请求401 http request
使用deferred.resolve($http(rejection.config));
,但我现在面临的问题是我已经成功运行了一个脚本,因为之前的api没有运行,因为两个请求都是从不同的地方。这是我的代码。希望你们明白我在说什么。
app.js
app.run(['$rootScope', '$window', 'OAuth', '$cookies', '$timeout','$q', '$http', function($rootScope, $window, OAuth, $cookies, $timeout, $q, $http) {
$rootScope.$on('oauth:error', function(event, rejection) {
// Ignore `invalid_grant` error - should be catched on `LoginController`.
if ('invalid_token' === rejection.data.error || 'invalid_grant' === rejection.data.error || 'invalid_request' === rejection.data.error || 'invalid_client' === rejection.data.error || 'unauthorized_client' === rejection.data.error || 'unsupported_grant_type' === rejection.data.error) {
$cookies.remove('userId');
$timeout(function(){
window.location.href = 'index.php';
},200);
}
// Refresh token when a `invalid_token` error occurs.
if ('expired_token' === rejection.data.error) {
console.log(rejection);
/*var $injector = angular.injector();
var $http = $http || $injector.get('$http');*/
var deferred = $q.defer();
var scope = $rootScope;
OAuth.getRefreshToken();
//console.log(refreshtoken.$$state);
//console.log(rejection.config.headers.Authorization);
//var state = true;
/*setInterval(function() {
var accessToken = localStorage.getItem("accessToken");
if(refreshToken && state==true){
//rejection.config.headers.Authorization = 'Bearer ' + accessToken;
//console.log(accessToken);
//console.log(rejection.config.headers.Authorization);
//console.log(rejection.config);
//deferred.resolve($http(rejection.config));
//$window.location.reload();
console.log(accessToken);
console.log(refreshToken);
state = false;
}
}, 5000);*/
}
console.log(rejection);
console.log(rejection.data.error);
console.log(rejection.data.error_description);
// Redirect to `/login` with the `error_reason`.
//return $window.location.href = 'index.php';
});
}]);
controller.js
这是登录,我从中获得访问令牌。
OAuth.getAccessToken($scope.user).then( function successCallBack(response){
$scope.response = response;
if($scope.response.status == 200){
console.log($scope.response.data);
$scope.accessToken = $scope.response.data.access_token;
$scope.refreshToken = $scope.response.data.refresh_token;
localStorage.setItem("accessToken", $scope.accessToken);
localStorage.setItem("refreshToken", $scope.refreshToken);
var userId = response.headers('userid');
console.log(userId);
$cookies.put("userId", userId);
window.location.href = 'user_profile.php';
}
}, function errorCallBack(response){
console.log(response);
});
我搜索了很多google或facebook这样做但无法理解如何在重复上一次http请求后运行我已成功插入的代码。
var fd = new FormData();
fd.append('userId', $scope.userId);
$http({
method:'POST',
url:'xxxxxx/web/Users/public/removeFromBookmarks',
data:fd,
transformRequest: angular.identity,
headers: {'Content-Type': undefined,
'Authorization': 'Bearer ' + $scope.accessToken}
}).then( function successCallBack(response){
$scope.response = response;
$scope.status = $scope.response.data.status;
console.log($scope.status);
if($scope.status == true){
$scope.bookmarked = !$scope.bookmarked;
}
}, function errorCallBack(response){
$scope.status = status;
console.log($scope.status);
});
我想运行$scope.bookmarked = !$scope.bookmarked;
的代码并且第一次如果令牌未过期则很容易运行但是当app.js中的deferred.resolve($http(rejection.config));
重复相同的请求时,则相同的代码不跑。
提前致谢。