2016-04-08 4 views
0

私は401エラーのためのresponseErrorハンドラを持っています。 HTTPインターセプタは、HTTP要求を認証して再試行を継続するようにモーダルユーザに要求します。しかし、問題は、2つ以上のモーダルが表示されているリクエストインターセプタに複数の失敗があり、数回認証する必要がある場合です。どのように私は最初の要求で認証された後にすべての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

@タイラー、モーダルが追加されました。あなたはすべての要求が失敗し、それは私と一緒にうまくいかない場合、拒否された約束の原因を必要としません。 – user256968

答えて

0

あなただけのAuth工場への複数の呼び出しのための1個のモーダルをしたい場合は、同じモーダルインスタンスは毎回アクセスできるように、あなたはそれを定義する必要があります処理するコードがある場合は

.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 
      }); 
     } 
    } 
});