Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for auto refresh of tokens. #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions AngularJSAuthentication.Web/app/services/authInterceptorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
app.factory('authInterceptorService', ['$q', '$injector','$location', 'localStorageService', function ($q, $injector,$location, localStorageService) {

var authInterceptorServiceFactory = {};
var $http;

var _request = function (config) {

Expand All @@ -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;
}]);
}]);
31 changes: 17 additions & 14 deletions AngularJSAuthentication.Web/app/services/authService.js
Original file line number Diff line number Diff line change
@@ -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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you prefer injecting $http using $injector instead of adding $http as a dependency?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to overcome the issue of circular dependencies. When configuring the interceptor, you make the $httpProvider depend on the interceptor. So if you try to inject $http in the service constructor, you'll get a circular dependency error. To overcome this, I use the $injector service, to postpone injecting the $httpProvider until after everything is ready.

This is also the reason why $injector.get('authService'); is injected using $injector, since the authService depends on $http as well.


var serviceBase = ngAuthSettings.apiServiceBaseUri;
var $http;
var authServiceFactory = {};

var _authentication = {
Expand All @@ -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;
});
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down