3

I try to perform a http.post call in a http interceptor, but I get a

Circular dependency found: $http <- Ace <- authInterceptor <- $http <- $templateRequest <- $compile

I see why but I dont know how to solve it... Still new to angular and a little confusing sometime, I hope you can help me :) Heres is my code:

var app = angular.module('AceAngularApi', []);

app.service('Ace', ['$http', '$q', '$injector', '$window', function($http, $q, $injector, $window) {

    var user = null;

    var getCurrentUser = function() {
        var url = "http://localhost:8080/api/currentuser";

        var response = $http.post(url, {}).then(function(response) {});
        return response;
    };
    return {
        getCurrentUser: getCurrentUser,
    }
}]);

app.factory('authInterceptor', ['$rootScope', '$q', '$window', '$injector', 'Ace',
    function($rootScope, $q, $window, $injector, Ace) {
        return {
            request: function(config) {
                config.headers = config.headers || {};
                if ($window.localStorage.token) {
                    config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
                }
                return config;
            },
            response: function(response) {
                if (response.status === 401) {
                    // handle the case where the user is not authenticated
                } else {
                    Ace.getCurrentUser().then(function() {
                        console.log("Got current user");
                    });
                }
                return response || $q.when(response);
            }
        };
    }
]);

app.config(function($httpProvider) {
    $httpProvider.interceptors.push('authInterceptor');
});
1
  • 4
    Try once with injecting Ace using injector like var Ace = $injector.get('Ace'); in authInterceptor Commented Aug 5, 2015 at 8:03

1 Answer 1

8

You are trying to define $http's pre-processing functionality by injecting authInterceptor into $httpProvider, however authInterceptor has dependency on $http, it leads to circular dependency problem.

To get around this circular dependency issue, you can use the $injector service to wire up Ace

app.factory('authInterceptor', ['$rootScope', '$q', '$window', '$injector',
function($rootScope, $q, $window, $injector) {
    return {
        response: function(response) {
            if (response.status === 401) {
                // handle the case where the user is not authenticated
            } else {
                var Ace = $injector.get('Ace');
                Ace.getCurrentUser().then(function() {
                    console.log("Got current user");
                });
            }
            return response || $q.when(response);
        }
    };
  }
]);

Another workaround is registering the interceptor in a run() block instead of a config() block, but keep in mind, before run() is executed, any call to $http have nothing to do with authInterceptor

Sign up to request clarification or add additional context in comments.

1 Comment

Thank that worked, at least partly. Now I can send a request but it gets stock in a endless loop, calling Ace.getCurrentUser() over and over again....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.