diff --git a/packages/openlogin/src/OpenLogin.ts b/packages/openlogin/src/OpenLogin.ts index f6004906..136f428e 100644 --- a/packages/openlogin/src/OpenLogin.ts +++ b/packages/openlogin/src/OpenLogin.ts @@ -32,6 +32,8 @@ class OpenLogin { private _storageBaseKey = "openlogin_store"; + private dappState: string; + constructor(options: OpenLoginOptions) { if (!options.clientId) throw InitializationError.invalidParams("clientId is required"); if (!options.network) options.network = OPENLOGIN_NETWORK.SAPPHIRE_MAINNET; @@ -89,6 +91,10 @@ class OpenLogin { return this.options.sessionNamespace || ""; } + get appState(): string { + return this.state.userInfo.appState || this.dappState || ""; + } + private get baseUrl(): string { // testing and develop don't have versioning if (this.options.buildEnv === BUILD_ENV.DEVELOPMENT || this.options.buildEnv === BUILD_ENV.TESTING) return `${this.options.sdkUrl}`; @@ -128,6 +134,7 @@ class OpenLogin { } if (params.error) { + this.dappState = params.state; throw LoginError.loginFailed(params.error); } @@ -171,6 +178,10 @@ class OpenLogin { const result = await this.openloginHandler(`${this.baseUrl}/start`, dataObject, getTimeout(params.loginProvider)); if (this.options.uxMode === UX_MODE.REDIRECT) return undefined; + if (result.error) { + this.dappState = result.state; + throw LoginError.loginFailed(result.error); + } this.sessionManager.sessionId = result.sessionId; this.options.sessionNamespace = result.sessionNamespace; this.currentStorage.set("sessionId", result.sessionId); diff --git a/packages/openlogin/src/PopupHandler.ts b/packages/openlogin/src/PopupHandler.ts index 76260e45..5621306e 100644 --- a/packages/openlogin/src/PopupHandler.ts +++ b/packages/openlogin/src/PopupHandler.ts @@ -6,6 +6,8 @@ import { getPopupFeatures } from "./utils"; export interface PopupResponse { sessionId?: string; sessionNamespace?: string; + state?: string; + error?: string; } class PopupHandler extends EventEmitter { url: string; @@ -71,8 +73,8 @@ class PopupHandler extends EventEmitter { } async listenOnChannel(loginId: string): Promise { - return new Promise((resolve, reject) => { - const bc = new BroadcastChannel<{ error?: string; data?: PopupResponse }>(loginId, { + return new Promise((resolve, _reject) => { + const bc = new BroadcastChannel<{ error?: string; data?: PopupResponse; state?: string }>(loginId, { webWorkerSupport: false, type: "server", }); @@ -80,7 +82,7 @@ class PopupHandler extends EventEmitter { this.close(); bc.close(); if (ev.error) { - reject(new Error(ev.error)); + resolve({ error: ev.error, state: ev.state }); } else { resolve(ev.data); } diff --git a/packages/openlogin/src/utils.ts b/packages/openlogin/src/utils.ts index 4d951ede..bdefb198 100644 --- a/packages/openlogin/src/utils.ts +++ b/packages/openlogin/src/utils.ts @@ -19,6 +19,7 @@ export type HashQueryParamResult = { sessionId?: string; sessionNamespace?: string; error?: string; + state?: string; }; export function getHashQueryParams(replaceUrl = false): HashQueryParamResult {