2016-04-08 21 views
0

我有401錯誤的responseError處理程序。 HTTP攔截器將要求模式用戶進行身份驗證並繼續重試HTTP請求。但問題是,如果我有多個失敗的請求攔截器顯示我多於一個模式,我需要幾次驗證。在第一次請求中進行身份驗證後,如何重試所有HTTP請求?

$httpProvider.interceptors.push(function($q, $injector, Auth) { 
    return { 
     responseError: function(rejection) { 
      if (rejection.status === 401) { 

       return Auth.authenticate().then(function() { 
        return $injector.get('$http')(rejection.config); 
       });      
      } 

      return $q.reject(rejection); 
     } 
    }; 
}); 

.factory('Auth', function($injector) { 
return { 
    authenticate: function() { 
     var $uibModal = $injector.get('$uibModal'); 

     var modal = $uibModal.open({ 
      templateUrl: '/authenticateModal.html', 
      controller: 'AuthenticateModalController', 
     }); 

     return modal.result.then(function() { 
      // success 
     }); 
    } 
} 
}); 



.controller('AuthenticateModalController', function ($scope, $uibModalInstance, $http, Auth) { 
$scope.submit = function() {   
    Auth.login({ 
      'email': $scope.email, 
      'password': $scope.password 
    }).then(function successCallback(response) { 
     $uibModalInstance.close(); 
    }, function errorCallback(response) { 
     console.log('error'); 
    }); 
}; 
}) 
+0

凡被處理該代碼拒絕承諾和模式? – tyler

+0

@tyler,Modal補充說。你不需要拒絕承諾,因爲如果失敗了,所有的請求都會失敗,這對我很好。 – user256968

回答

0

如果你只想1種模式的多次調用你的Auth工廠,你應該每次都定義它使同一模式的情況下,可以訪問:

.factory('Auth', function($injector) { 
    var $uibModal = $injector.get('$uibModal'); 
    var modal 
    return { 
     authenticate: function() { 
      if (!modal) { 
       modal = $uibModal.open({ 
        templateUrl: '/authenticateModal.html', 
        controller: 'AuthenticateModalController', 
       }); 
      } 

      return modal.result.then(function() { 
       // success 
      }); 
     } 
    } 
});