Skip to content

Commit

Permalink
feat: improve OAuth error handling
Browse files Browse the repository at this point in the history
Correctly read errors returned from the callback site. Also use a custom
error type, `OauthCancelledError`, when the OAuth flow is cancelled
because a user closed the popup window.
  • Loading branch information
dphilipson committed Sep 24, 2024
1 parent 42a2924 commit d83c0bc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
30 changes: 27 additions & 3 deletions account-kit/signer/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConnectionConfigSchema } from "@aa-sdk/core";
import { BaseError, ConnectionConfigSchema } from "@aa-sdk/core";
import { getWebAuthnAttestation } from "@turnkey/http";
import { IframeStamper } from "@turnkey/iframe-stamper";
import { WebauthnStamper } from "@turnkey/webauthn-stamper";
Expand Down Expand Up @@ -451,7 +451,11 @@ export class AlchemySignerWebClient extends BaseSignerClient<ExportWalletParams>
if (!event.data) {
return;
}
const { alchemyBundle: bundle, alchemyOrgId: orgId } = event.data;
const {
alchemyBundle: bundle,
alchemyOrgId: orgId,
alchemyError,
} = event.data;
if (bundle && orgId) {
cleanup();
popup?.close();
Expand All @@ -460,6 +464,10 @@ export class AlchemySignerWebClient extends BaseSignerClient<ExportWalletParams>
orgId,
connectedEventName: "connectedOauth",
}).then(resolve, reject);
} else if (alchemyError) {
cleanup();
popup?.close();
reject(new Error(alchemyError));
}
};

Expand All @@ -468,7 +476,7 @@ export class AlchemySignerWebClient extends BaseSignerClient<ExportWalletParams>
const checkCloseIntervalId = setInterval(() => {
if (popup?.closed) {
cleanup();
reject(new Error("OAuth cancelled"));
reject(new OauthCancelledError());
}
}, CHECK_CLOSE_INTERVAL);

Expand Down Expand Up @@ -657,3 +665,19 @@ function resolveRelativeUrl(url: string): string {
a.href = url;
return a.href;
}

/**
* This error is thrown when the OAuth flow is cancelled because the auth popup
* window was closed.
*/
export class OauthCancelledError extends BaseError {
override name = "OauthCancelledError";

/**
* Constructor for initializing an error indicating that the OAuth flow was
* cancelled.
*/
constructor() {
super("OAuth cancelled");
}
}
2 changes: 1 addition & 1 deletion account-kit/signer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { BaseAlchemySigner } from "./base.js";
export { BaseSignerClient } from "./client/base.js";
export { AlchemySignerWebClient } from "./client/index.js";
export { AlchemySignerWebClient, OauthCancelledError } from "./client/index.js";
export type * from "./client/types.js";
export { DEFAULT_SESSION_MS } from "./session/manager.js";
export type * from "./signer.js";
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d83c0bc

Please sign in to comment.