Zrom
Zrom

Reputation: 1230

Redirect to an external URL

I'm developing a web application using angularJS but only the authentication page doesn't use angularJS, it uses simply html .

So when the user is disconnected it must redirect him to authentication page,so I made an interceptor that executed before any request and use a service from my application to validate if the user is connected , if he's not he must be redirected to the authentication page .

This my interceptor code :

                $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;
                                },
                            };
                        });     

My problem is there is a loop generated when I call the service(because it's another request and the interceptor will be executed again).Any solutions for my problem or is there another way to do this.

Upvotes: 0

Views: 1914

Answers (1)

SoluableNonagon
SoluableNonagon

Reputation: 11755

So from your description and your comments you could set up a service which intercepts both the request and the response. I created a generic one, see code below with example plunker. You can expand that code quite a bit to achieve what you want.

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

}]); 

Upvotes: 1

Related Questions