I'm working with restangular and have set up an interceptor to handle 401 responses by redirecting to another state.
The issue is that angular only allows the injection of providers, not services, in config.
Is there a way to access the $state: ng.ui.IStateService
within my restangular interceptor?
I could inject the provider
$stateProvider: ng.ui.IStateProvider
, but I don't think it can be used to navigate to a specific state.
This is what I currently have:
/// <reference path="../../typings/globals/restangular/index.d.ts" />
((): void => {
"use strict";
angular
.module("app")
.config(configRestangular);
configRestangular.$inject = [
"$state",
"RestangularProvider",
"authSettings",
"stateNames",
"apiUrl"
];
function isCustomValidationErrorResponse(response: restangular.IResponse) {
return (response.data && response.data.customValidationError === true);
}
function configRestangular(
$state: ng.ui.IStateService,
restangularProvider: restangular.IProvider,
authSettings: IAuthSettings,
stateNames: IStateNames,
apiUrl: string) {
// restangular settings
restangularProvider.setBaseUrl(apiUrl);
restangularProvider.setErrorInterceptor((response: restangular.IResponse, deferred: ng.IDeferred<any>) => {
if (response.status === 401) {
$state.go(stateNames.login); //unable to use $state here, need alternative solution
}
return true;
});
}
})();