From 00e1e84761d75f2e669b6b2b9dca4be1fcfecd00 Mon Sep 17 00:00:00 2001 From: ieow Date: Mon, 4 Nov 2024 16:09:57 +0800 Subject: [PATCH] fix: handle undefined sessionTime fix bug: redirectUrl skipped when sessionManager rehydration failed --- src/mpcCoreKit.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index 21425ab4..0b317d35 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -119,7 +119,9 @@ export class Web3AuthMPCCoreKit implements ICoreKit { } else log.setLevel("error"); if (typeof options.manualSync !== "boolean") options.manualSync = false; if (!options.web3AuthNetwork) options.web3AuthNetwork = WEB3AUTH_NETWORK.MAINNET; - if (!options.sessionTime) options.sessionTime = 86400; + // we accept sessionTime to be 0 which will disable the session manager creation + // if sessionTime is not provided, it is defaulted to 86400 + if (options.sessionTime === undefined) options.sessionTime = 86400; if (!options.serverTimeOffset) options.serverTimeOffset = 0; if (!options.uxMode) options.uxMode = UX_MODE.REDIRECT; if (!options.redirectPathName) options.redirectPathName = "redirect"; @@ -131,6 +133,13 @@ export class Web3AuthMPCCoreKit implements ICoreKit { this.currentStorage = new AsyncStorage(this._storageBaseKey, options.storage); + if (options.sessionTime) { + this.sessionManager = new SessionManager({ + sessionTime: options.sessionTime, + serverTimeOffset: options.serverTimeOffset, + }); + } + this.nodeDetailManager = new NodeDetailManager({ network: this.options.web3AuthNetwork, enableLogging: options.enableLogging, @@ -280,15 +289,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit { this.ready = true; - if (this.options.sessionTime !== 0) { - // setup session Manager during init instead of async constructor - const sessionId = await this.currentStorage.get("sessionId"); - this.sessionManager = new SessionManager({ - sessionTime: this.options.sessionTime, - sessionId, - }); - } - // try handle redirect flow if enabled and return(redirect) from oauth login if ( params.handleRedirectResult && @@ -298,7 +298,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit { // on failed redirect, instance is reseted. await this.handleRedirectResult(); // if not redirect flow try to rehydrate session if available - } else if (params.rehydrate && this.sessionManager?.sessionId) { + } else if (params.rehydrate && this.options.sessionTime !== 0) { + const sessionId = await this.currentStorage.get("sessionId"); + this.sessionManager.sessionId = sessionId; + // swallowed, should not throw on rehydrate timed out session const sessionResult = await this.sessionManager.authorizeSession().catch(async (err) => { log.error("rehydrate session error", err);