From 40d98004cd7cb1da7fe62794762b9ffc24a4d21e Mon Sep 17 00:00:00 2001 From: Chandra Penke Date: Fri, 1 Dec 2023 09:39:40 -0800 Subject: [PATCH] fix: honor disableIdle flag (#809) Closes #576 Co-authored-by: Kyle Peacock --- docs/generated/changelog.html | 4 +++- packages/auth-client/src/index.ts | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/docs/generated/changelog.html b/docs/generated/changelog.html index 4a8fb5916..1e4259db5 100644 --- a/docs/generated/changelog.html +++ b/docs/generated/changelog.html @@ -11,7 +11,9 @@

Agent-JS Changelog

Version x.x.x

-
    +
      +
    • fix: honor disableIdle flag
    • +

    Version 0.20.2

    • chore: lowering prettier version for CI
    • diff --git a/packages/auth-client/src/index.ts b/packages/auth-client/src/index.ts index 2b73fe829..dd9a89152 100644 --- a/packages/auth-client/src/index.ts +++ b/packages/auth-client/src/index.ts @@ -329,15 +329,18 @@ export class AuthClient { // The event handler for processing events from the IdP. private _eventHandler?: (event: MessageEvent) => void, ) { - const logout = this.logout.bind(this); - const idleOptions = _createOptions?.idleOptions; + this._registerDefaultIdleCallback(); + } + + private _registerDefaultIdleCallback() { + const idleOptions = this._createOptions?.idleOptions; /** * Default behavior is to clear stored identity and reload the page. * By either setting the disableDefaultIdleCallback flag or passing in a custom idle callback, we will ignore this config */ if (!idleOptions?.onIdle && !idleOptions?.disableDefaultIdleCallback) { this.idleManager?.registerCallback(() => { - logout(); + this.logout(); location.reload(); }); } @@ -372,17 +375,14 @@ export class AuthClient { this._identity = DelegationIdentity.fromDelegation(key, this._chain); this._idpWindow?.close(); - if (!this.idleManager) { - const idleOptions = this._createOptions?.idleOptions; + const idleOptions = this._createOptions?.idleOptions; + // create the idle manager on a successful login if we haven't disabled it + // and it doesn't already exist. + if (!this.idleManager && !idleOptions?.disableIdle) { this.idleManager = IdleManager.create(idleOptions); - - if (!idleOptions?.onIdle && !idleOptions?.disableDefaultIdleCallback) { - this.idleManager?.registerCallback(() => { - this.logout(); - location.reload(); - }); - } + this._registerDefaultIdleCallback(); } + this._removeEventListener(); delete this._idpWindow;