From 4d2cae0b0f3f74bdef762cad95b0174adf5d9e46 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 27 Mar 2017 22:22:56 -0400 Subject: [PATCH] share profile promise result when called at same time --- src/app/services/authService.js | 35 +++++++++++++++++-------------- src/app/services/cryptoService.js | 2 +- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/app/services/authService.js b/src/app/services/authService.js index 62646ab39b..b3c1b60fce 100644 --- a/src/app/services/authService.js +++ b/src/app/services/authService.js @@ -69,27 +69,30 @@ angular return deferred.promise; }; + var _setDeferred = null; _service.setUserProfile = function () { - var deferred = $q.defer(); + if (_setDeferred && _setDeferred.promise.$$state.status === 0) { + return _setDeferred.promise; + } + + _setDeferred = $q.defer(); var token = tokenService.getToken(); if (!token) { - deferred.reject(); - return deferred.promise; + _setDeferred.reject(); + return _setDeferred.promise; } var decodedToken = jwtHelper.decodeToken(token); - - _userProfile = { - id: decodedToken.name, - email: decodedToken.email - }; - apiService.accounts.getProfile({}, function (profile) { - _userProfile.extended = { - name: profile.Name, - twoFactorEnabled: profile.TwoFactorEnabled, - culture: profile.Culture + _userProfile = { + id: decodedToken.name, + email: decodedToken.email, + extended: { + name: profile.Name, + twoFactorEnabled: profile.TwoFactorEnabled, + culture: profile.Culture + } }; if (profile.Organizations) { @@ -106,13 +109,13 @@ angular _userProfile.organizations = orgs; cryptoService.setOrgKeys(orgs); - deferred.resolve(_userProfile); + _setDeferred.resolve(_userProfile); } }, function () { - deferred.reject(); + _setDeferred.reject(); }); - return deferred.promise; + return _setDeferred.promise; }; _service.addProfileOrganization = function (org) { diff --git a/src/app/services/cryptoService.js b/src/app/services/cryptoService.js index a8d8c72daa..63be004e35 100644 --- a/src/app/services/cryptoService.js +++ b/src/app/services/cryptoService.js @@ -26,7 +26,7 @@ angular }; _service.setOrgKeys = function (orgKeysCt, privateKey) { - if (!orgKeysCt.length) { + if (!orgKeysCt || Object.keys(orgKeysCt).length === 0) { return; }