From d83c0bcaba9c6f547e5ff9bc4cfaa35028be2400 Mon Sep 17 00:00:00 2001 From: David Philipson Date: Tue, 24 Sep 2024 16:00:55 -0700 Subject: [PATCH] feat: improve OAuth error handling 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. --- account-kit/signer/src/client/index.ts | 30 +++++++++++++++++-- account-kit/signer/src/index.ts | 2 +- .../OauthCancelledError/constructor.mdx | 19 ++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 site/pages/reference/account-kit/signer/classes/OauthCancelledError/constructor.mdx diff --git a/account-kit/signer/src/client/index.ts b/account-kit/signer/src/client/index.ts index 47aff23b37..03374d7d77 100644 --- a/account-kit/signer/src/client/index.ts +++ b/account-kit/signer/src/client/index.ts @@ -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"; @@ -451,7 +451,11 @@ export class AlchemySignerWebClient extends BaseSignerClient 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(); @@ -460,6 +464,10 @@ export class AlchemySignerWebClient extends BaseSignerClient orgId, connectedEventName: "connectedOauth", }).then(resolve, reject); + } else if (alchemyError) { + cleanup(); + popup?.close(); + reject(new Error(alchemyError)); } }; @@ -468,7 +476,7 @@ export class AlchemySignerWebClient extends BaseSignerClient const checkCloseIntervalId = setInterval(() => { if (popup?.closed) { cleanup(); - reject(new Error("OAuth cancelled")); + reject(new OauthCancelledError()); } }, CHECK_CLOSE_INTERVAL); @@ -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"); + } +} diff --git a/account-kit/signer/src/index.ts b/account-kit/signer/src/index.ts index d3d1e22e7b..642c3852a1 100644 --- a/account-kit/signer/src/index.ts +++ b/account-kit/signer/src/index.ts @@ -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"; diff --git a/site/pages/reference/account-kit/signer/classes/OauthCancelledError/constructor.mdx b/site/pages/reference/account-kit/signer/classes/OauthCancelledError/constructor.mdx new file mode 100644 index 0000000000..e538cdea3a --- /dev/null +++ b/site/pages/reference/account-kit/signer/classes/OauthCancelledError/constructor.mdx @@ -0,0 +1,19 @@ +--- +# This file is autogenerated +title: OauthCancelledError +description: Overview of the OauthCancelledError method +--- + +# OauthCancelledError + +Constructor for initializing an error indicating that the OAuth flow was +cancelled. +:::note +`OauthCancelledError` extends `BaseError`, see the docs for BaseError for all supported methods. +::: + +## Import + +```ts +import { OauthCancelledError } from "@account-kit/signer"; +```