From 400932c6de3c3d77e5f83ae0e347baf4710a9820 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 11 Apr 2017 15:00:53 -0400 Subject: [PATCH] refresh access token after creating org --- src/app/config.js | 21 ++++--------------- src/app/services/authService.js | 17 +++++++++++++++ .../settingsCreateOrganizationController.js | 10 ++++++++- .../views/settingsCreateOrganization.html | 2 +- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/app/config.js b/src/app/config.js index dbc1b56ca4..86b9c6be6a 100644 --- a/src/app/config.js +++ b/src/app/config.js @@ -10,7 +10,7 @@ angular whiteListedDomains: ['api.bitwarden.com', 'localhost'] }); var refreshPromise; - jwtInterceptorProvider.tokenGetter = /*@ngInject*/ function (options, appSettings, tokenService, apiService, + jwtInterceptorProvider.tokenGetter = /*@ngInject*/ function (options, appSettings, tokenService, authService, jwtHelper, $q) { if (options.url.indexOf(appSettings.apiUri) !== 0) { return; @@ -24,28 +24,15 @@ angular if (!token) { return; } - + if (!tokenService.tokenNeedsRefresh(token)) { return token; } - var refreshToken = tokenService.getRefreshToken(); - if (!refreshToken) { - return; - } - - var deferred = $q.defer(); - apiService.identity.token({ - grant_type: 'refresh_token', - client_id: 'web', - refresh_token: refreshToken - }, function (response) { - tokenService.setToken(response.access_token); - tokenService.setRefreshToken(response.refresh_token); + refreshPromise = authService.refreshAccessToken().then(function (newToken) { refreshPromise = null; - deferred.resolve(response.access_token); + return newToken || token; }); - refreshPromise = deferred.promise; return refreshPromise; }; diff --git a/src/app/services/authService.js b/src/app/services/authService.js index 53bb0477c5..3857dfe561 100644 --- a/src/app/services/authService.js +++ b/src/app/services/authService.js @@ -168,5 +168,22 @@ angular return tokenService.getToken() !== null; }; + _service.refreshAccessToken = function () { + var refreshToken = tokenService.getRefreshToken(); + if (!refreshToken) { + return null; + } + + return apiService.identity.token({ + grant_type: 'refresh_token', + client_id: 'web', + refresh_token: refreshToken + }).$promise.then(function (response) { + tokenService.setToken(response.access_token); + tokenService.setRefreshToken(response.refresh_token); + return response.access_token; + }); + } + return _service; }); diff --git a/src/app/settings/settingsCreateOrganizationController.js b/src/app/settings/settingsCreateOrganizationController.js index 9086ef784e..fc890a2957 100644 --- a/src/app/settings/settingsCreateOrganizationController.js +++ b/src/app/settings/settingsCreateOrganizationController.js @@ -70,7 +70,15 @@ $analytics.eventTrack('Created Organization'); authService.addProfileOrganizationOwner(result, shareKey); - $state.go('backend.org.dashboard', { orgId: result.Id }).then(function () { + authService.refreshAccessToken().then(function () { + goToOrg(result.Id); + }, function () { + goToOrg(result.Id); + }); + } + + function goToOrg(id) { + $state.go('backend.org.dashboard', { orgId: id }).then(function () { toastr.success('Your new organization is ready to go!', 'Organization Created'); }); } diff --git a/src/app/settings/views/settingsCreateOrganization.html b/src/app/settings/views/settingsCreateOrganization.html index 44721b41b3..231cf82d8c 100644 --- a/src/app/settings/views/settingsCreateOrganization.html +++ b/src/app/settings/views/settingsCreateOrganization.html @@ -4,7 +4,7 @@

Organizations allow you to share parts of your vault with others as well as manage related users - for a specific entity (such as a company). + for a specific entity (such as a family, small team, or large company).