I am building and AngularJS app using ES6 classes with traceur transpiling to ES5 in AMD format.
in my module I import the interceptor class and register it as a service
To compelement the other fine answers regarding arrow functions, I think it's a bit cleaner using a static factory method in the Interceptor:
export default class AuthenticationInterceptor {
static $inject = ['$q', '$injector', '$rootRouter'];
constructor ($q, $injector, $rootRouter) {
this.$q = $q;
this.$injector = $injector;
this.$rootRouter = $rootRouter;
}
static create($q, $injector, $rootRouter) {
return new AuthenticationInterceptor($q, $injector, $rootRouter);
}
responseError = (rejection) => {
const HANDLE_CODES = [401, 403];
if (HANDLE_CODES.includes(rejection.status)) {
// lazy inject in order to avoid circular dependency for $http
this.$injector.get('authenticationService').clearPrincipal();
this.$rootRouter.navigate(['Login']);
}
return this.$q.reject(rejection);
}
}
Usage:
.config(['$provide', '$httpProvider', function ($provide, $httpProvider) {
$provide.factory('reauthenticationInterceptor', AuthenticationInterceptor.create);
$httpProvider.interceptors.push('reauthenticationInterceptor');
}]);