3

Using angular-ui-router, I have something like:

.state('page', {
    url: '/page',
    templateUrl: '/page.html'
})

This template URL may return a "401 Unauthorized". Is it possible to handle the http response when the router tries to load the url and handle it, so I can show some message or redirect the user?

Natan
  • 4,686
  • 5
  • 30
  • 48
  • Look at this: http://stackoverflow.com/questions/20230691/injecting-state-ui-router-into-http-interceptor-causes-circular-dependency – michelem Oct 29 '15 at 15:43

1 Answers1

2

You can register an interceptor for your application. The implementation of this interceptor

    $httpProvider.responseInterceptors.push([
      '$q',
      '$location',
      '$rootScope',
     (function($q, $location, $rootScope) {
  return {
    responseError: function(response) {
      if (response.status === 401) {
          console.log(response.status + ' intercepted');
          $location.path('/unauthorized/');
          $q.reject(response);
      }
      return response;
    }
  };
});
]

After this you need to register /unauthorized in your states with a custom page template.

asaad
  • 441
  • 2
  • 15