1

I implemented authInterceptorService so when response is not authorize I send request for new token and then I want to resend previoes request. So I need to store it somewhere and resend, what is best way to do it?

var _responseError = function (rejection) {
        if (rejection.status === 401) {
            var authService = $injector.get('authService');
            var authData = localStorageService.get('authorizationData');

            if (authData) {
                authService.refreshToken().then(function (response) {
                        //redirect to original request


                    },
                 function (err) {
                     $location.path('/login');
                 });
            }

            authService.logOut();
            $location.path('/login');
        }
        return $q.reject(rejection);
    }
kosnkov
  • 5,609
  • 13
  • 66
  • 107
  • on failure, you get the configuration of the failed call in the error response, you could just reuse that. Or you could make the first request a function and call it independently. See below. – SoluableNonagon Nov 11 '14 at 16:52

1 Answers1

1

Why not make it its own request?

// define the original request
var originalRequest = function( some input ){
    $http.post( 'url' , data ).then(onSuccess, _responseError);
}

then in your _responseError function, just call the original request

var _responseError = function (rejection) {
        if (rejection.status === 401) {
            var authService = $injector.get('authService');
            var authData = localStorageService.get('authorizationData');

            if (authData) {
                authService.refreshToken().then(function (response) {
                        //redirect to original request

                        originalRequest( some input );

                    },
                 function (err) {
                     $location.path('/login');
                 });
            }

            authService.logOut();
            $location.path('/login');
        }
        return $q.reject(rejection);
    }

Also, on failure, you do get the original configuration of the request, so you could just use that...

            if (authData) {
                authService.refreshToken().then(function (response) {
                        //redirect to original request

                        $http.post(rejection.config).then( ... , ... );

                    },
                 function (err) {
                     $location.path('/login');
                 });
            }
SoluableNonagon
  • 11,541
  • 11
  • 53
  • 98