diff --git a/src/services/apiService.js b/src/services/apiService.js index c1df3da5..b910d222 100644 --- a/src/services/apiService.js +++ b/src/services/apiService.js @@ -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; diff --git a/src/services/syncService.js b/src/services/syncService.js index 8f975adf..387c5416 100644 --- a/src/services/syncService.js +++ b/src/services/syncService.js @@ -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); }); } diff --git a/src/services/tokenService.js b/src/services/tokenService.js index 12c1c8ac..c7291fb0 100644 --- a/src/services/tokenService.js +++ b/src/services/tokenService.js @@ -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(); }); }); }; diff --git a/src/services/userService.js b/src/services/userService.js index 408d039b..d7b2046a 100644 --- a/src/services/userService.js +++ b/src/services/userService.js @@ -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); + }); + } }); }; }