Skip to content

Commit

Permalink
Merge pull request #260 from torusresearch/fix/error-handling
Browse files Browse the repository at this point in the history
Error handling
  • Loading branch information
chaitanyapotti authored Sep 26, 2023
2 parents 7dc7ac2 + db63322 commit bbdf03d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
11 changes: 11 additions & 0 deletions packages/openlogin/src/OpenLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}`;
Expand Down Expand Up @@ -128,6 +134,7 @@ class OpenLogin {
}

if (params.error) {
this.dappState = params.state;
throw LoginError.loginFailed(params.error);
}

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions packages/openlogin/src/PopupHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,16 +73,16 @@ class PopupHandler extends EventEmitter {
}

async listenOnChannel(loginId: string): Promise<PopupResponse> {
return new Promise<PopupResponse>((resolve, reject) => {
const bc = new BroadcastChannel<{ error?: string; data?: PopupResponse }>(loginId, {
return new Promise<PopupResponse>((resolve, _reject) => {
const bc = new BroadcastChannel<{ error?: string; data?: PopupResponse; state?: string }>(loginId, {
webWorkerSupport: false,
type: "server",
});
bc.addEventListener("message", (ev) => {
this.close();
bc.close();
if (ev.error) {
reject(new Error(ev.error));
resolve({ error: ev.error, state: ev.state });
} else {
resolve(ev.data);
}
Expand Down
1 change: 1 addition & 0 deletions packages/openlogin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type HashQueryParamResult = {
sessionId?: string;
sessionNamespace?: string;
error?: string;
state?: string;
};

export function getHashQueryParams(replaceUrl = false): HashQueryParamResult {
Expand Down

0 comments on commit bbdf03d

Please sign in to comment.