combine token refresh methods. added api logging

This commit is contained in:
Kyle Spearrin 2017-09-07 13:21:49 -04:00
parent e09e15f72f
commit 01e48377e9
1 changed files with 49 additions and 44 deletions

View File

@ -116,8 +116,10 @@ function initApiService() {
// Account APIs // Account APIs
ApiService.prototype.getAccountRevisionDate = function (success, error) { ApiService.prototype.getAccountRevisionDate = function (success, error) {
log('getAccountRevisionDate invoked');
var self = this; var self = this;
handleTokenState(self).then(function (token) { handleTokenState(self).then(function (token) {
log('Revision Date API Call');
$.ajax({ $.ajax({
type: 'GET', type: 'GET',
url: self.baseUrl + '/accounts/revision-date?' + token, url: self.baseUrl + '/accounts/revision-date?' + token,
@ -130,6 +132,7 @@ function initApiService() {
} }
}); });
}, function (jqXHR) { }, function (jqXHR) {
log('Error handling token state for Revision Date API Call');
handleError(error, jqXHR, true, self); handleError(error, jqXHR, true, self);
}); });
}; };
@ -505,7 +508,7 @@ function initApiService() {
function handleError(errorCallback, jqXHR, tokenError, self) { function handleError(errorCallback, jqXHR, tokenError, self) {
if (jqXHR && (tokenError && jqXHR.status === 400) || jqXHR.status === 401 || jqXHR.status === 403) { if (jqXHR && (tokenError && jqXHR.status === 400) || jqXHR.status === 401 || jqXHR.status === 403) {
console.log('Logging out from apiService at ' + new Date() + '. Reason: Status ' + jqXHR.status + '.'); log('Logging out. Reason: Status ' + jqXHR.status + '.');
console.log(jqXHR); console.log(jqXHR);
if (self && self.logoutCallback) { if (self && self.logoutCallback) {
self.logoutCallback(true, function () { }); self.logoutCallback(true, function () { });
@ -523,59 +526,61 @@ function initApiService() {
function handleTokenState(self) { function handleTokenState(self) {
var deferred = Q.defer(); var deferred = Q.defer();
self.tokenService.getToken(function (accessToken) { self.tokenService.getToken(function (accessToken) {
if (self.tokenService.tokenNeedsRefresh()) { log('Got access token');
refreshToken(self, function (accessToken) {
resolveTokenQs(accessToken, self, deferred); if (!self.tokenService.tokenNeedsRefresh()) {
}, function (err) { log('Token doesn\'t need refreshing');
deferred.reject(err);
});
}
else {
resolveTokenQs(accessToken, self, deferred); resolveTokenQs(accessToken, self, deferred);
return;
} }
log('Token needs refresh');
self.tokenService.getRefreshToken(function (refreshToken) {
if (!refreshToken || refreshToken === '') {
log('No existing refresh token.');
error();
return;
}
log('Got existing refresh token. Do refresh call.');
$.ajax({
type: 'POST',
url: self.identityBaseUrl + '/connect/token',
data: {
grant_type: 'refresh_token',
client_id: 'browser',
refresh_token: refreshToken
},
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
dataType: 'json',
success: function (response) {
log('Successfully refreshed.');
var tokenResponse = new IdentityTokenResponse(response);
self.tokenService.setTokens(tokenResponse.accessToken, tokenResponse.refreshToken, function () {
log('New token set.');
resolveTokenQs(tokenResponse.accessToken, self, deferred);
});
},
error: function (jqXHR, textStatus, errorThrown) {
log('Error refreshing.');
deferred.reject(jqXHR);
}
});
});
}); });
return deferred.promise; return deferred.promise;
} }
function refreshToken(self, success, error) {
self.tokenService.getRefreshToken(function (refreshToken) {
if (!refreshToken || refreshToken === '') {
error();
return;
}
postConnectToken(self, {
grant_type: 'refresh_token',
client_id: 'browser',
refresh_token: refreshToken
}, function (token) {
self.tokenService.setTokens(token.accessToken, token.refreshToken, function () {
success(token.accessToken);
});
}, function (jqXHR) {
error(jqXHR);
});
});
}
function resolveTokenQs(token, self, deferred) { function resolveTokenQs(token, self, deferred) {
log('Resolving token.');
deferred.resolve('access_token3=' + token); deferred.resolve('access_token3=' + token);
} }
function postConnectToken(self, data, success, error) { function log(msg) {
$.ajax({ console.log(new Date() + ' - API Service: ' + msg);
type: 'POST',
url: self.identityBaseUrl + '/connect/token',
data: data,
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
dataType: 'json',
success: function (response) {
success(new IdentityTokenResponse(response));
},
error: function (jqXHR, textStatus, errorThrown) {
error(jqXHR);
}
});
} }
} }