Skip to content

Commit

Permalink
Api key regenerate (#2782)
Browse files Browse the repository at this point in the history
* Implement access token regenerate and remove

* Fix
  • Loading branch information
AndriiMysko authored Oct 9, 2023
1 parent 15b3529 commit 9122aac
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
21 changes: 21 additions & 0 deletions app/components/account-token.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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.');
});
}
},
});
10 changes: 10 additions & 0 deletions app/services/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions app/services/storage/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
12 changes: 12 additions & 0 deletions app/styles/app/layouts/profile.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/templates/account/settings.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
developer.travis-ci.com
</ExternalLinkTo>.
</p>
<AccountToken @token={{this.auth.token}}/>
<AccountToken @token={{this.auth.token}} @showRegenerateButton={{true}}/>
</section>
<section>
<h3>
Expand Down
12 changes: 10 additions & 2 deletions app/templates/components/account-token.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<span class="label">
Token
</span>
<span class="token-field">
<span class="token-field {{if this.showRegenerateButton 'token-field-regen' ''}}">
{{#if this.showCopySuccess}}
<span class="token-copied-text">
Token copied!
Expand All @@ -18,7 +18,7 @@
{{/if}}
</span>
</div>
<div class="token-actions">
<div class="{{if this.showRegenerateButton 'token-actions-regen' ''}} token-actions">
<CopyButton
class="copy-button"
@clipboardText={{this.token}}
Expand All @@ -34,4 +34,12 @@
{{if this.tokenIsVisible "Hide token" "View token"}}
</span>
</button>
{{#if this.showRegenerateButton}}
<button class="show-token" onclick={{action "regenerateToken"}}>
<SvgImage @name="icon-restart" @class="icon" />
<span>
Regenerate
</span>
</button>
{{/if}}
</div>

0 comments on commit 9122aac

Please sign in to comment.