remove old auth bearer code. move sync methods

This commit is contained in:
Kyle Spearrin 2017-08-31 14:17:52 -04:00
parent eae2efea67
commit 19a59e6177
4 changed files with 41 additions and 105 deletions

View File

@ -516,47 +516,17 @@ function initApiService() {
function handleTokenState(self) {
var deferred = Q.defer();
self.tokenService.getAuthBearer(function (authBearer) {
self.tokenService.getToken(function (accessToken) {
// handle transferring from old auth bearer
if (authBearer && !accessToken) {
self.appIdService.getAppId(function (appId) {
postConnectToken(self, {
grant_type: 'password',
oldAuthBearer: authBearer,
username: 'abcdefgh', // has to be something
password: 'abcdefgh', // has to be something
scope: 'api offline_access',
client_id: 'browser',
deviceIdentifier: appId,
deviceType: self.utilsService.getDeviceType(),
deviceName: self.utilsService.getBrowser()
}, function (token) {
self.tokenService.clearAuthBearer(function () {
self.tokenService.setTokens(token.accessToken, token.refreshToken, function () {
resolveTokenQs(token.accessToken, self, deferred);
});
});
}, function (jqXHR) {
deferred.reject(jqXHR);
});
});
} // handle token refresh
else if (self.tokenService.tokenNeedsRefresh()) {
refreshToken(self, function (accessToken) {
resolveTokenQs(accessToken, self, deferred);
}, function (err) {
deferred.reject(err);
});
}
else {
if (authBearer) {
self.tokenService.clearAuthBearer(function () { });
}
self.tokenService.getToken(function (accessToken) {
if (self.tokenService.tokenNeedsRefresh()) {
refreshToken(self, function (accessToken) {
resolveTokenQs(accessToken, self, deferred);
}
});
}, function (err) {
deferred.reject(err);
});
}
else {
resolveTokenQs(accessToken, self, deferred);
}
});
return deferred.promise;

View File

@ -28,23 +28,23 @@ function initSyncService() {
return;
}
self.userService.getUserId(function (userId) {
var now = new Date();
needsSyncing(self, forceSync, function (needsSync, skipped) {
if (skipped) {
var now = new Date();
needsSyncing(self, forceSync, function (needsSync, skipped) {
if (skipped) {
self.syncCompleted(false);
callback(false);
return;
}
if (!needsSync) {
self.setLastSync(now, function () {
self.syncCompleted(false);
callback(false);
return;
}
if (!needsSync) {
self.setLastSync(now, function () {
self.syncCompleted(false);
callback(false);
});
return;
}
});
return;
}
self.userService.getUserId(function (userId) {
syncProfile(self).then(function () {
return syncFolders(self, userId);
}).then(function () {
@ -75,18 +75,18 @@ function initSyncService() {
return;
}
self.getLastSync(function (lastSync) {
self.apiService.getAccountRevisionDate(function (response) {
var accountRevisionDate = new Date(response);
self.apiService.getAccountRevisionDate(function (response) {
var accountRevisionDate = new Date(response);
self.getLastSync(function (lastSync) {
if (lastSync && accountRevisionDate <= lastSync) {
callback(false, false);
return;
}
callback(true, false);
}, function () {
callback(false, true);
});
}, function () {
callback(false, true);
});
}

View File

@ -4,7 +4,6 @@
function initTokenService() {
var _token,
_authBearer,
_decodedToken,
_refreshToken;
@ -53,24 +52,6 @@ function initTokenService() {
});
};
TokenService.prototype.getAuthBearer = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
if (_authBearer) {
return callback(_authBearer);
}
chrome.storage.local.get('authBearer', function (obj) {
if (obj && obj.authBearer) {
_authBearer = obj.authBearer;
}
return callback(_authBearer);
});
};
TokenService.prototype.setRefreshToken = function (refreshToken, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
@ -142,28 +123,15 @@ function initTokenService() {
});
};
TokenService.prototype.clearAuthBearer = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
_authBearer = null;
chrome.storage.local.remove('authBearer', function () {
callback();
});
};
TokenService.prototype.clearToken = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
_token = _decodedToken = _refreshToken = _authBearer = null;
chrome.storage.local.remove('authBearer', function () {
chrome.storage.local.remove('accessToken', function () {
chrome.storage.local.remove('refreshToken', function () {
callback();
});
_token = _decodedToken = _refreshToken = null;
chrome.storage.local.remove('accessToken', function () {
chrome.storage.local.remove('refreshToken', function () {
callback();
});
});
};

View File

@ -130,16 +130,14 @@ function initUserService() {
var self = this;
self.tokenService.getToken(function (token) {
self.tokenService.getAuthBearer(function (authBearer) {
if (!token && !authBearer) {
callback(false);
}
else {
self.getUserId(function (userId) {
callback(userId !== null);
});
}
});
if (!token) {
callback(false);
}
else {
self.getUserId(function (userId) {
callback(userId !== null);
});
}
});
};
}