401 Never Being Caught by Interceptor


#1

My suspicion is that this is more an Angular question than an Ionic question, but I’m not sure. I am trying to handle 401 status code from my server via an interceptor-- similar to [this forum question][1] or [this post’s][2] step number 8. In every other network request response, I hit the response property of the interceptor, but not with 401. My promises never reject and the loading screen never hides.

Any advice for the 401 black hole I am stuck in?

My interceptor:

.factory('interceptorService', function($rootScope, $q, Session, AuthorizationHeader) {
  var interceptorService = {
    request: function(config) {
      $rootScope.$broadcast('loading:show');
      config.headers['Authentication-Key'] = Session.key();
      config.headers['Authentication-Timestamp'] = new Date().toJSON();
      config.headers['Authentication-Account'] = Session.accountName();
      if(typeof config.headers['Authorization'] === 'undefined') {
        config.headers['Authorization'] = AuthorizationHeader.buildHashed(config, Session.key());
      }
      return config || $q.when(config);
    },

    response: function(response) {
      console.log('response');
      console.log(response);
      if(response.status === 401) {
        console.log('401');
      }
      $rootScope.$broadcast('loading:hide');
      return response || $q.when(response);
    },

    requestError: function(config) {
      $rootScope.$broadcast('loading:hide');
      return $q.reject(config);
    },

    responseError: function(response) {
      console.log('response error');
      $rootScope.$broadcast('loading:hide');
      return $q.reject(response);
    }
  };

  return interceptorService;
})

I never see ‘401’ or ‘response error’. I can see from my network traffic I am getting a 401 from the server. 403s hit the errorResponse property, so I know that works.

Thanks in advance.
[1]: $ionicLoading in http interceptor
[2]: http://bitoftech.net/2014/06/09/angularjs-token-authentication-using-asp-net-web-api-2-owin-asp-net-identity/


#2

Is there any more information I can provide to inspire an answer from the community?


#3

For anyone out there struggling with the same issue: I think it is almost certainly an issue with the www-authenticate header I’m getting back from my server. Here is a blog post that leads me to think so.

I would LOVE any suggestions on a good work around since we can’t make $http behave synchronously.


#4

Having the same issue here. I’ve tried to use an interceptor, but it’s almost like the app just sits waiting on an auth challenge, which means it gives me no advantage over promises.

Also, it seems to be related to a known bug: https://issues.apache.org/jira/browse/CB-2415


#5

@Ryan_IRL That has been my exact experience and the bug you mentioned is linked in the blog posted I linked. What have you been doing to work around this. Right now the best solution I have come up with is to use $timeout but that is quite distasteful.


#6

I have not found a solution yet. I may have to resort to a timeout as well until the bug is resolved.


#7

@Ryan_IRL this Stack Overflow-er suggested this: http://stackoverflow.com/a/25941925/1254263

If you have control over your server, you can attempt to strip the www-authenticate from that side. I, unfortunately, do not have that option… :-1:

HTH