Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling #260

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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