diff --git a/AngularJSAuthentication.Web/app/services/authInterceptorService.js b/AngularJSAuthentication.Web/app/services/authInterceptorService.js index ee4f5563..b0c70f14 100644 --- a/AngularJSAuthentication.Web/app/services/authInterceptorService.js +++ b/AngularJSAuthentication.Web/app/services/authInterceptorService.js @@ -2,6 +2,7 @@ app.factory('authInterceptorService', ['$q', '$injector','$location', 'localStorageService', function ($q, $injector,$location, localStorageService) { var authInterceptorServiceFactory = {}; + var $http; var _request = function (config) { @@ -16,24 +17,33 @@ app.factory('authInterceptorService', ['$q', '$injector','$location', 'localStor } var _responseError = function (rejection) { + var deferred = $q.defer(); if (rejection.status === 401) { var authService = $injector.get('authService'); - var authData = localStorageService.get('authorizationData'); - - if (authData) { - if (authData.useRefreshTokens) { - $location.path('/refresh'); - return $q.reject(rejection); - } - } - authService.logOut(); - $location.path('/login'); + authService.refreshToken().then(function (response) { + _retryHttpRequest(rejection.config, deferred); + }, function () { + authService.logOut(); + $location.path('/login'); + deferred.reject(rejection); + }); + } else { + deferred.reject(rejection); } - return $q.reject(rejection); + return deferred.promise; + } + + var _retryHttpRequest = function (config, deferred) { + $http = $http || $injector.get('$http'); + $http(config).then(function (response) { + deferred.resolve(response); + }, function (response) { + deferred.reject(response); + }); } authInterceptorServiceFactory.request = _request; authInterceptorServiceFactory.responseError = _responseError; return authInterceptorServiceFactory; -}]); \ No newline at end of file +}]); diff --git a/AngularJSAuthentication.Web/app/services/authService.js b/AngularJSAuthentication.Web/app/services/authService.js index f6f16898..b8f304fa 100644 --- a/AngularJSAuthentication.Web/app/services/authService.js +++ b/AngularJSAuthentication.Web/app/services/authService.js @@ -1,7 +1,8 @@ 'use strict'; -app.factory('authService', ['$http', '$q', 'localStorageService', 'ngAuthSettings', function ($http, $q, localStorageService, ngAuthSettings) { +app.factory('authService', ['$q', '$injector', 'localStorageService', 'ngAuthSettings', function ($q, $injector, localStorageService, ngAuthSettings) { var serviceBase = ngAuthSettings.apiServiceBaseUri; + var $http; var authServiceFactory = {}; var _authentication = { @@ -14,6 +15,7 @@ app.factory('authService', ['$http', '$q', 'localStorageService', 'ngAuthSetting _logOut(); + $http = $http || $injector.get('$http'); return $http.post(serviceBase + 'api/account/register', registration).then(function (response) { return response; }); @@ -30,6 +32,7 @@ app.factory('authService', ['$http', '$q', 'localStorageService', 'ngAuthSetting var deferred = $q.defer(); + $http = $http || $injector.get('$http'); $http.post(serviceBase + 'token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) { if (loginData.useRefreshTokens) { @@ -80,25 +83,25 @@ app.factory('authService', ['$http', '$q', 'localStorageService', 'ngAuthSetting var authData = localStorageService.get('authorizationData'); - if (authData) { - - if (authData.useRefreshTokens) { + if (authData && authData.useRefreshTokens) { - var data = "grant_type=refresh_token&refresh_token=" + authData.refreshToken + "&client_id=" + ngAuthSettings.clientId; + var data = "grant_type=refresh_token&refresh_token=" + authData.refreshToken + "&client_id=" + ngAuthSettings.clientId; - localStorageService.remove('authorizationData'); + localStorageService.remove('authorizationData'); - $http.post(serviceBase + 'token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) { + $http = $http || $injector.get('$http'); + $http.post(serviceBase + 'token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) { - localStorageService.set('authorizationData', { token: response.access_token, userName: response.userName, refreshToken: response.refresh_token, useRefreshTokens: true }); + localStorageService.set('authorizationData', { token: response.access_token, userName: response.userName, refreshToken: response.refresh_token, useRefreshTokens: true }); - deferred.resolve(response); + deferred.resolve(response); - }).error(function (err, status) { - _logOut(); - deferred.reject(err); - }); - } + }).error(function (err, status) { + _logOut(); + deferred.reject(err); + }); + } else { + deferred.reject(); } return deferred.promise;