Skip to content

Commit 6cee85f

Browse files
committed
fix(oauth2): Show access token validity period on account details page
1 parent 70f7bc8 commit 6cee85f

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

lib/account.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class Account {
242242
break;
243243
}
244244
result[key] = value;
245-
for (let subKey of ['created', 'expires']) {
245+
for (let subKey of ['created', 'expires', 'generated']) {
246246
if (result[key][subKey]) {
247247
let dateVal = /^[0-9]+$/.test(result[key][subKey]) ? Number(result[key][subKey]) : result[key][subKey];
248248
let date = new Date(dateVal);
@@ -386,12 +386,14 @@ class Account {
386386
}
387387
}
388388

389-
if (
390-
accountData[key].expires &&
391-
typeof accountData[key].expires === 'object' &&
392-
accountData[key].expires.toString() !== 'Invalid Date'
393-
) {
394-
connectData.expires = accountData[key].expires.toISOString();
389+
for (let subKey of ['created', 'expires', 'generated']) {
390+
if (
391+
accountData[key][subKey] &&
392+
typeof accountData[key][subKey] === 'object' &&
393+
accountData[key][subKey].toString() !== 'Invalid Date'
394+
) {
395+
connectData[subKey] = accountData[key][subKey].toISOString();
396+
}
395397
}
396398

397399
result[key] = JSON.stringify(connectData);
@@ -2095,13 +2097,17 @@ class Account {
20952097
throw new Error('Failed to renew token');
20962098
}
20972099

2100+
let now = new Date();
2101+
20982102
let updates = {
20992103
accessToken: r.access_token,
2100-
expires: new Date(Date.now() + r.expires_in * 1000).toISOString()
2104+
expires: new Date(now.getTime() + r.expires_in * 1000).toISOString(),
2105+
generated: now.toISOString()
21012106
};
21022107

21032108
if (r.refresh_token) {
21042109
updates.refreshToken = r.refresh_token;
2110+
updates.refreshTokenGenerated = now.toISOString();
21052111
}
21062112

21072113
if (r.scope) {

lib/routes-ui.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ function formatAccountData(account) {
526526
.filter(entry => entry);
527527

528528
account.oauth2.expiresStr = account.oauth2.expires ? account.oauth2.expires.toISOString() : false;
529+
account.oauth2.generatedStr = account.oauth2.generated ? account.oauth2.generated.toISOString() : false;
529530
}
530531

531532
return account;

static/js/app.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ document.addEventListener('DOMContentLoaded', () => {
172172
updateRelativeTimes();
173173
setInterval(updateRelativeTimes, 15 * 1000);
174174

175+
for (let t of document.querySelectorAll('.local-time')) {
176+
let date = new Date(t.dataset.time);
177+
t.textContent = new Intl.DateTimeFormat().format(date);
178+
}
179+
180+
for (let t of document.querySelectorAll('.local-date-time')) {
181+
let date = new Date(t.dataset.time);
182+
t.textContent = new Intl.DateTimeFormat(undefined, { timeStyle: 'medium', dateStyle: 'short' }).format(date);
183+
}
184+
175185
let clip = new ClipboardJS('.copy-btn');
176186
if (!clip) {
177187
console.log('Can not set up clipboard');

views/accounts/account.hbs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,22 @@
254254
</dd>
255255
{{/if}}
256256

257+
{{#if account.oauth2.expiresStr}}
258+
<dt class="col-sm-3">Access token validity</dt>
259+
<dd class="col-sm-9">
260+
261+
{{#if account.oauth2.generatedStr}}
262+
<span class="local-date-time code-link" data-time="{{account.oauth2.generatedStr}}"
263+
title="{{account.oauth2.generatedStr}}"></span> &mdash;
264+
{{else}}
265+
To
266+
{{/if}}
267+
268+
<span class="local-date-time code-link" data-time="{{account.oauth2.expiresStr}}"
269+
title="{{account.oauth2.expiresStr}}"></span>
270+
</dd>
271+
{{/if}}
272+
257273
</dl>
258274

259275
</div>

0 commit comments

Comments
 (0)