如果用户未登录,我正在使用https://github.com/witoldsz/angular-http-auth非常成功地弹出一个模式。我试图抢占所有请求,即使用户不在t 认证。目前,当用户点击应用程序时,它会按预期弹出模式,但在成功登录后,重新发送缓冲的请求会返回以下错误:
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);
}
}
}]);
}])