diff --git a/packages/openlogin/src/OpenLogin.ts b/packages/openlogin/src/OpenLogin.ts index 49c359f2..b93d244f 100644 --- a/packages/openlogin/src/OpenLogin.ts +++ b/packages/openlogin/src/OpenLogin.ts @@ -44,6 +44,8 @@ export type OpenLoginState = { loginConfig: LoginConfig; storageServerUrl: string; sessionNamespace: string; + enableSessionRefresh: boolean; + sessionTime?: number; }; class OpenLogin { @@ -89,6 +91,8 @@ class OpenLogin { _storageServerUrl: options._storageServerUrl ?? "https://broadcast-server.tor.us", storageKey: options.storageKey === "session" ? "session" : "local", _sessionNamespace: options._sessionNamespace ?? "", + enableSessionRefresh: options.enableSessionRefresh ?? false, + sessionTime: options.sessionTime, }); } @@ -113,6 +117,8 @@ class OpenLogin { whiteLabel: options.whiteLabel, storageServerUrl: options._storageServerUrl, sessionNamespace: options._sessionNamespace, + enableSessionRefresh: options.enableSessionRefresh, + sessionTime: options.sessionTime, }; } @@ -191,6 +197,7 @@ class OpenLogin { const loginParams: LoginParams = { loginProvider: params.loginProvider, + sessionTime: this.state.sessionTime, ...defaultParams, ...params, }; @@ -406,11 +413,24 @@ class OpenLogin { } async _getData(): Promise> { - return this.request>({ + const newSessionId = randomId(); + const data = await this.request>({ allowedInteractions: [ALLOWED_INTERACTIONS.JRPC], method: OPENLOGIN_METHOD.GET_DATA, - params: [{}], + params: [ + this.state.enableSessionRefresh + ? { + sessionRefreshId: newSessionId, + sessionTime: this.state.sessionTime, + } + : {}, + ], }); + + if (this.state.enableSessionRefresh) { + this.state.store.set("sessionId", newSessionId); + } + return data; } _syncState(newState: Record): void { diff --git a/packages/openlogin/src/interfaces.ts b/packages/openlogin/src/interfaces.ts index 416875a0..2f65e79e 100644 --- a/packages/openlogin/src/interfaces.ts +++ b/packages/openlogin/src/interfaces.ts @@ -193,6 +193,24 @@ export type OpenLoginOptions = { * @internal */ _sessionNamespace?: string; + + /** + * How long should a login session last at a minimum in seconds + * + * @defaultValue 86400 seconds + * @remarks Max value of sessionTime can be 7 * 86400 (7 days) + */ + sessionTime?: number; + + /** + * By default user will be logged out after the sessionTime expires. + * You can set this option to true if you want to generate a new session on every login before sessionTime expires. + * If user doesn't logs in within sessionTime, user will be logged out. + * + * @defaultValue false + * + */ + enableSessionRefresh?: boolean; }; export type LoginParams = BaseRedirectParams & {