Skip to content

Commit bace2ff

Browse files
authored
Merge pull request #9 from helfi92/schedule-renewals-properly
Fix: properly schedule renewals
2 parents 20626c1 + 6ac1b12 commit bace2ff

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/components/Authorize/index.jsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ export default class Authorize extends Component {
245245
this.auth.checkSession({}, (err, authResult) => {
246246
if (authResult) {
247247
this.persistSession({ authResult });
248-
this.scheduleRenewal(authResult.expiresIn);
248+
// From https://tools.ietf.org/html/rfc6749#section-4.2.2, expiresIn
249+
// is in seconds but setTimeout takes a delay in milliseconds
250+
this.scheduleRenewal(authResult.expiresIn * 1000);
249251
}
250252

251253
const error =
@@ -259,12 +261,10 @@ export default class Authorize extends Component {
259261
});
260262
});
261263

262-
scheduleRenewal() {
263-
const expiration = new Date(
264-
JSON.parse(localStorage.getItem(SESSION)).expiration
265-
);
266-
const now = new Date();
267-
const delay = Math.max(0, expiration - now);
264+
scheduleRenewal(delay) {
265+
if (this.renewalTimer) {
266+
clearTimeout(this.renewalTimer);
267+
}
268268

269269
this.renewalTimer = setTimeout(async () => {
270270
try {
@@ -314,7 +314,17 @@ export default class Authorize extends Component {
314314
this.persistSession({ authResult, userInfo });
315315

316316
if (!this.props.disableAutoRenew) {
317-
this.scheduleRenewal(authResult.expiresIn);
317+
// Existing sessions will not have `authResult.expiresIn` up-to-date
318+
// since, for existing sessions, the logic above will return
319+
// whathever is in localStorage. We rely on the expiration
320+
// field instead.
321+
if (expiration) {
322+
this.scheduleRenewal(expiration - new Date());
323+
} else {
324+
// From https://tools.ietf.org/html/rfc6749#section-4.2.2, expiresIn
325+
// is in seconds but setTimeout takes a delay in milliseconds
326+
this.scheduleRenewal(authResult.expiresIn * 1000);
327+
}
318328
}
319329

320330
this.setState(

0 commit comments

Comments
 (0)