当Auth失败时,Angular $ http拦截器会打开多个模态

时间:2015-07-24 14:56:26

标签: angularjs angular-http angular-http-interceptors angular-http-auth

我在$http interceptor失败时使用$auth来显示登录模式。这是我的简要设置:

$provide.factory('modalWhenLoggedOut', ['$q', '$injector', '$rootScope',
          function($q, $injector, $rootScope) {

        return {

             responseError: function(rejection) {

                 // $injector.get to fix circular dependency error
                 var loginModal = $injector.get('LoginModalService');
                 var $http = $injector.get('$http');
                 var $state = $injector.get('$state');

                 // Check for reasons instead of status code
                 var rejectionReasons = ['token_not_provided', 'token_expired', 'token_absent', 'token_invalid'];

                 // Loop through each rejection reason and redirect
                 angular.forEach(rejectionReasons, function(value, key) {

                     if(rejection.data.error === value) {
                        // Show the login modal
                         var deferred = $q.defer();

                         loginModal()
                           .then(function () {
                             deferred.resolve( $http(rejection.config) );
                         })
                         .catch(function () {
                             $state.go('index');
                             deferred.reject(rejection);
                        });

                        return deferred.promise;
                     }
                 });

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

// Push the new factory onto the $http interceptor array
$httpProvider.interceptors.push('modalWhenLoggedOut');

上面的代码工作正常。但我遇到的问题是当api抛出多次auth失败错误时,拦截器会同时打开多个模态。通常在1页中,我可能有2-3次各种服务联系后端api,当auth失败时,所有这些api都会在无效令牌期间返回auth failure错误。这个拦截器将它作为3个taken_invalid错误并将其显示为3个登录模态1。我该如何防止这种情况?

我怎样才能确保拦截器只显示1个登录模式,无论auth失败多少次?

我的服务看起来像这样:

.service('LoginModalService', ['$modal', '$rootScope', 
            function($modal, $rootScope) {

                    function updateRootScope() {
                            $rootScope.loginProgress = false;
                    }

                  if ($rootScope.loginProgress = false) {
                    return function() {
                      var instance = $modal.open({
                        templateUrl: rootPath + 'views/partials/LoginModalTemplate.html',
                        controller: 'LoginModalCtrl'
                      })

                    return instance.result.then(updateRootScope);
                  };
                  }
}])

在服务中,我尝试使用$rootScope.loginProgress作为开关来确定是否打开了登录模式。但是,如果模态已经打开($rootScope.loginProgress为真),我不知道如何返回一个空的承诺。

1 个答案:

答案 0 :(得分:0)

为了克服我的问题,我现在使用http-auth-interceptor拦截器,然后根据登录模式打开状态更改$rootScope.loginProgress。由于多个401而打开的连续模态将检查此登录模态状态,然后决定是否打开模态。使用http-auth-interceptor,我可以通过观看'event:auth-loginRequired'来添加切换。这是我正在使用的代码:

scope.$on('event:auth-loginRequired', function() {      
            if(!$rootScope.loginProgress) {
                        $rootScope.loginProgress = true;
                        LoginModalService()
                            .then(function () {
                              authService.loginConfirmed();
                                    $rootScope.loginProgress = false;
                          })
                          .catch(function () {
                              authService.loginCancelled();
                                    $rootScope.loginProgress = false;
                          });
            }
});

其中LoginModalService()是打开登录模式的服务。