diff --git a/app/components/account-token.js b/app/components/account-token.js index 31043845b6..5ea7bf91d4 100644 --- a/app/components/account-token.js +++ b/app/components/account-token.js @@ -1,10 +1,21 @@ import Component from '@ember/component'; +import { reads } from '@ember/object/computed'; +import { inject as service } from '@ember/service'; export default Component.extend({ classNames: ['account-token'], + api: service(), + auth: service(), + + localStorage: service('storage'), + authStorage: reads('localStorage.auth'), + + flashes: service(), + tokenIsVisible: false, showCopySuccess: false, + showRegenerateButton: false, actions: { tokenVisibility() { @@ -19,5 +30,15 @@ export default Component.extend({ this.toggleProperty('showCopySuccess'); } }, + + regenerateToken() { + this.api.patch('/access_token').then((data) => { + this.auth.handleTokenRegeneration(data['token']); + + this.flashes.success('Token successfully regenerated!'); + }).catch(() => { + this.flashes.error('There was an error regenerating the token.'); + }); + } }, }); diff --git a/app/services/auth.js b/app/services/auth.js index 9a3710effc..c7288c239c 100644 --- a/app/services/auth.js +++ b/app/services/auth.js @@ -328,6 +328,16 @@ export default Service.extend({ } }), + handleTokenRegeneration(token) { + const currentUser = this.currentUser; + this.storage.accounts.removeObject(currentUser); + currentUser.set('authToken', token); + this.storage.accounts.addObject(currentUser); + this.reloadUser(currentUser); + this.storage.set('activeAccount', currentUser); + this.storage.setRegeneratedToken(token); + }, + actions: { switchAccount(id) { diff --git a/app/services/storage/auth.js b/app/services/storage/auth.js index 6e1bc0aab4..da0fe017df 100644 --- a/app/services/storage/auth.js +++ b/app/services/storage/auth.js @@ -109,6 +109,10 @@ export default Service.extend({ isBecome: computed(() => !!storage.getItem('travis.auth.become')), + setRegeneratedToken(token) { + storage.setItem('travis.token', token); + }, + clearLoginData() { storage.removeItem('travis.token'); storage.removeItem('travis.user'); diff --git a/app/styles/app/layouts/profile.scss b/app/styles/app/layouts/profile.scss index 9ca16e5826..7231068e1f 100644 --- a/app/styles/app/layouts/profile.scss +++ b/app/styles/app/layouts/profile.scss @@ -410,6 +410,14 @@ $profile-breakpoint: 600px; } } +.token-field-regen { + line-height: 30px; +} + +.token-actions-regen { + width: 19em !important; +} + .token-actions { width: 15em; display: flex; @@ -420,6 +428,10 @@ $profile-breakpoint: 600px; padding-left: 0.4em; } +.token-actions-regen button { + width: calc(1/3*100% - (1 - 1/3)*0.5em) !important; +} + .token-actions button { display: inline-flex; justify-content: center; diff --git a/app/templates/account/settings.hbs b/app/templates/account/settings.hbs index 0f554f4dae..6bf3bcf495 100644 --- a/app/templates/account/settings.hbs +++ b/app/templates/account/settings.hbs @@ -26,7 +26,7 @@ developer.travis-ci.com .

- +

diff --git a/app/templates/components/account-token.hbs b/app/templates/components/account-token.hbs index e8b5201244..aea51f0412 100644 --- a/app/templates/components/account-token.hbs +++ b/app/templates/components/account-token.hbs @@ -2,7 +2,7 @@ Token - + {{#if this.showCopySuccess}} Token copied! @@ -18,7 +18,7 @@ {{/if}} -
+
+ {{#if this.showRegenerateButton}} + + {{/if}}