AngularJS请求拦截器和http-auth-interceptor

时间:2014-07-21 20:52:53

标签: angularjs angular-http-interceptors

如果用户未登录,我正在使用https://github.com/witoldsz/angular-http-auth成功弹出模式。我试图抢占所有请求,如果用户未经过身份验证,甚至会出去。目前,当用户点击应用程序时,它会按预期弹出模式,但在成功登录后,重新发送缓冲的请求会返回以下错误:

TypeError: Cannot read property 'protocol' of undefined
at urlIsSameOrigin (http://localhost:9000/bower_components/angular/angular.js:14340:17)
at sendReq (http://localhost:9000/bower_components/angular/angular.js:8256:25)
at $http.serverRequest (http://localhost:9000/bower_components/angular/angular.js:7995:16)
at wrappedCallback (http://localhost:9000/bower_components/angular/angular.js:11485:81)
at wrappedCallback (http://localhost:9000/bower_components/angular/angular.js:11485:81)
at http://localhost:9000/bower_components/angular/angular.js:11571:26
at Scope.$eval (http://localhost:9000/bower_components/angular/angular.js:12595:28)
at Scope.$digest (http://localhost:9000/bower_components/angular/angular.js:12407:31)
at Scope.$apply (http://localhost:9000/bower_components/angular/angular.js:12699:24)
at done (http://localhost:9000/bower_components/angular/angular.js:8287:45) 

似乎代码缓冲了两个模板请求,然后弹出模态。成功通过模态后,sendReq将在两个缓冲的请求上运行,并在返回这些请求时运行,并且这些请求在urlIsSameOrigin中发生错误。有人有什么建议吗?这是我的模块:

'use strict';

angular.module('app')
  .factory('myAuth', ['$http','mySessionService','authService','$rootScope', function ($http, mySessionService, authService, $rootScope) {
    return {

      /* snip */

    };
  }])
  .config(['$httpProvider', function($httpProvider) {

    function isAuthenticated(mySessionService) {
      var session = mySessionService.getUserProfile();
      if(typeof session !== 'undefined' && !!session.username){
        return true;
      }
      return false
    }

    function isWhiteListed(url){
      switch (url){
        case "views/login.html":
        case "api/auth/":
          return true;
        default:
          return false;
      }
    }

    $httpProvider.interceptors.push(['$rootScope', '$q', 'httpBuffer', 'mySessionService',function($rootScope, $q, httpBuffer,mySessionService) {
      return {
        request: function(config) {
          //if not logged in    and not login form or auth endpoint:
          if(!isAuthenticated(mySessionService) && !isWhiteListed(config.url)){

            var deferred = $q.defer();
            $rootScope.$broadcast('event:auth-loginRequired', config);
            httpBuffer.append(config, deferred);
            return deferred.promise;
          }

          return config || $q.when(config);
        }
      }
    }]);
  }])

0 个答案:

没有答案