重定向到外部URL

时间:2014-09-10 16:44:45

标签: angularjs angularjs-scope

我正在使用angularJS开发Web应用程序,但只有身份验证页面不使用angularJS,它只使用html。

因此,当用户断开连接时,必须将其重定向到身份验证页面,因此我创建了一个在任何请求之前执行的拦截器,并使用我的应用程序中的服务来验证用户是否已连接,如果他不是必须重定向到身份验证页面。

这是我的拦截器代码:

                $httpProvider.interceptors
                        .push(function($q, $injector) {
                            return {
                                'request' : function(request) {
                                        var $http = $injector.get('$http');
/*
calls service from my application to verify the if the user is connected
*/
                                        $http
                                                .get("sessionValidator")
                                                .success(
                                                        function(data) {
                                                            if (data.result == 'sessionNull'
                                                                    || data.role != 'ROLE_USER') {
                                                                window.location.href = '/authenticationPage';
                                                            } 

                                                        });
return request;
                                },
                            };
                        });     

我的问题是,当我调用服务时会产生一个循环(因为它是另一个请求,并且拦截器将再次被执行)。解决我的问题或者是否有另一种方法来执行此操作。

1 个答案:

答案 0 :(得分:1)

因此,根据您的描述和您的评论,您可以设置一个拦截请求和响应的服务。我创建了一个通用的,请参阅下面的代码plunker。您可以扩展该代码以实现您想要的目标。

var app = angular.module("myApp", []);

app.service("interceptorService", function($http, $q) {

  // this service only has post and get, but post I documented get
  return {

    get: function(url, parameters){
      var deferred = $q.defer(); // the promise which we will return

      $http.get(url, {params: parameters}).then(function(successResponse){

        // check for null session or role being not a user, and redirect if response is approved
        if (successResponse.data.result == 'sessionNull' || successResponse.data.role != 'ROLE_USER'){
          window.location.href = '/authenticationPage';

          // this should probably be rejected because you don't want to process the response if the person is not a user, also the person is being redirected anyways
          deferred.reject(successResponse);
        } else{
          // since this is successful, we can resolve this successfully
          deferred.resolve(successResponse);
        }

      }, function(failureResponse){

        console.log(failureResponse);
        deferred.reject(failureResponse);


      });
      return deferred.promise;

    },
    post: function(url, parameters){
      var deferred = $q.defer();

      $http.post(url, parameters).then(function(successResponse){
        console.log(successResponse);
        deferred.resolve(successResponse);
      }, function(failureResponse){
        console.log(failureResponse);
        deferred.reject(failureResponse);
      });
      return deferred.promise;

    }
  }
})

app.controller('myCtrl', ['$scope', 'interceptorService', function($scope, interceptorService){

  var url = 'http://api.sba.gov/geodata/city_county_links_for_state_of/tx.xml';
  interceptorService.get(url).then(function(response){
    $scope.result = "http call succeeded";
  }, function(reason){
    $scope.result = "http call failed";
  });

}]);