Skip to content

Commit

Permalink
fix: handle undefined sessionTime
Browse files Browse the repository at this point in the history
fix bug: redirectUrl skipped when sessionManager rehydration failed
  • Loading branch information
ieow committed Nov 4, 2024
1 parent 5d872a1 commit 00e1e84
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/mpcCoreKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -131,6 +133,13 @@ export class Web3AuthMPCCoreKit implements ICoreKit {

this.currentStorage = new AsyncStorage(this._storageBaseKey, options.storage);

if (options.sessionTime) {
this.sessionManager = new SessionManager<SessionData>({
sessionTime: options.sessionTime,
serverTimeOffset: options.serverTimeOffset,
});
}

this.nodeDetailManager = new NodeDetailManager({
network: this.options.web3AuthNetwork,
enableLogging: options.enableLogging,
Expand Down Expand Up @@ -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<string>("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 &&
Expand All @@ -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<string>("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);
Expand Down

0 comments on commit 00e1e84

Please sign in to comment.