From their webpage:
Typical use case:
- somewhere the: $http(...).then(function(response) { do-something-with-response }) is invoked,
- the response of that requests is a HTTP 401,
- 'http-auth-interceptor' captures the initial request and broadcasts 'event:auth-loginRequired',
- your application intercepts this to e.g. show a login dialog (or whatever else),
- once your application figures out the authentication is OK, you are to call: authService.loginConfirmed(),
- your initial failed request will now be retried and finally, the do-something-with-response will fire.
So the only thing you need to do to interact with this is to have a parent scope to listen to even:auth-loginRequired
. This can be done in a directive, in a controller, it doesn't matter. I haven't used the software, but I'm imagining something like this:
angular.module('myApp', [])
.service('api', function(httpAutenticationService, $rootScope, $location) {
$rootScope.$on('event:auth-loginRequired', function() {
// For the sake of this example, let's say we redirect the user to a login page
$location.path('/login')
})
$http.get(API_URL).then( ... ); // If the user is not authenticated, this will be intercepted (1)
})
.controller('LoginController', function(httpAutenticationService) {
// Let's imagine you have a route with the 'controller' option to this controller
$http.get(LOGIN_URL).then(function() {
httpAutenticationService.loginConfirmed(); // The success callback from (1) will be executed now
}, function(){
alert("Invalid credentials")
})
})