title: Doing Ajax request in interceptor
body: <p>I'm trying to create an interceptor in AngularJS that checks if I'm already logged in or not. It does this by looking at the <code>$rootScope.user</code> variable.</p>
  
  <p>If the<code>$rootScope.user</code> variable doesn't exist then I want to make an Ajax call to see if there is still a Session alive on the server.</p>
  
  <p>The problem is that I then end up in a infinite loop, because my interceptor is intercepting all my Ajax requests...</p>
  
  <p>I basically have this now:</p>
  
  <pre><code>.factory('resourceInterceptor', ['$rootScope', 'AccountService', function($rootScope, accountService) {
      return {
          request: function(request) {
              if(!$rootScope.user) {
                  var userData = accountService.getUserSession(); // creates loop
                  //var userData = $http.get('api/Account'); // Same thing
              }
  
              return request;
          }
      }
  }]);
  </code></pre>
  
  <p>But this doesn't work, it gives this error:</p>
  
  <pre><code>Error: [$injector:cdep] Circular dependency found: $http &lt;- $resource &lt;- AccountService &lt;- resourceInterceptor &lt;- $http &lt;- $compile
  </code></pre>
  
  <p>Or same with $http:</p>
  
  <pre><code>Error: [$injector:cdep] Circular dependency found: $http &lt;- resourceInterceptor &lt;- $http &lt;- $compile
  </code></pre>
  
  <p>So how can I then solve this issue? I'd like to do an Ajax request from my interceptor, so I can retrieve the user info from the database.</p>
  
  <hr>
  
  <p>Please note: I know that there a a lot of different, and perhaps better ways, of implementing some sort of authentication method, like using a token in the header. I'm aware of that and I'm <strong><em>not</em></strong> looking for answers about different, better authentication methods. I'd like to have answers about this specific problem.</p>