Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip angularjs interceptor for an HTTP request?

I have an angularjs app, in which I have an interceptor that adds the authorization token to the header of each request.

However, somewhere in the application I need to use and external API where the interceptor ruins it, because it adds this authorization header which is not acceptable by this external API provider. How can I make angularjs HTTP skip the interceptor, only on this one specific case?

The interceptor code is below:

app.factory('authInterceptorService', ['$q', '$injector', '$location', 'localStorageService', function ($q, $injector, $location, localStorageService) {
    var authInterceptorServiceFactory = {};
    var $http;

    var _request = function (config) {

        config.headers = config.headers || {};

        var authData = localStorageService.get('authorizationData');
        if (authData) {
            //console.log("token: " + authData.token.substring(0, 10));
            //console.log("user: " + authData.userName);
            config.headers.Authorization = 'Bearer ' + authData.token;
        }
        return config;
    }

    var _responseError = function (rejection) {
        var deferred = $q.defer();
        if (rejection.status === 401) {
            var authService = $injector.get('authService');
            authService.refreshToken().then(function (response) {
                _retryHttpRequest(rejection.config, deferred);
            }, function () {
                authService.logOut();
                $location.path('/login');
                deferred.reject(rejection);
            });
        } else {
            deferred.reject(rejection);
        }
        return deferred.promise;
    }

    var _retryHttpRequest = function (config, deferred) {
        console.log('retrying');
        $http = $http || $injector.get('$http');
        $http(config).then(function (response) {
            deferred.resolve(response);
            //console.log("success:" +response);
        }, function (response) {
            deferred.reject(response);
            //console.log("error:" + response);
        });
    }

    authInterceptorServiceFactory.request = _request;
    authInterceptorServiceFactory.responseError = _responseError;

    return authInterceptorServiceFactory;
}]);
like image 254
Behrooz Avatar asked Jun 06 '16 03:06

Behrooz


People also ask

Can we bypass interceptor in angular?

So now to bypass the interceptor we can use HttpBackend. For more details about it, please refer to this. When injected, HttpBackend dispatches requests directly to the backend, without going through the interceptor chain. So as per the definition, we can surely use it for our current use case.

Why do we need AngularJS interceptor?

The angular interceptor is a medium connecting the backend and front-end applications. Whenever a request is made, the interceptors handle it in between. They can also identify the response by performing Rxjs operators. The interceptors do not initiate the handle method and handle the requests at their level.


1 Answers

Easy

$http.get("url" , {noAuth : true}).then(success(),error());

In the Interceptor

  var _request = function (config) {

    config.headers = config.headers || {};

    var authData = localStorageService.get('authorizationData');
    if (authData && !config.noAuth) {
        //console.log("token: " + authData.token.substring(0, 10));
        //console.log("user: " + authData.userName);
        config.headers.Authorization = 'Bearer ' + authData.token;
    }
    return config;
}
like image 52
Saneesh B Avatar answered Sep 18 '22 17:09

Saneesh B