@account-kit/signer 4.6.1 → 4.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/esm/base.d.ts +2 -0
  2. package/dist/esm/base.js +91 -25
  3. package/dist/esm/base.js.map +1 -1
  4. package/dist/esm/client/base.d.ts +6 -2
  5. package/dist/esm/client/base.js.map +1 -1
  6. package/dist/esm/client/index.d.ts +31 -1
  7. package/dist/esm/client/index.js +41 -4
  8. package/dist/esm/client/index.js.map +1 -1
  9. package/dist/esm/client/types.d.ts +21 -1
  10. package/dist/esm/client/types.js.map +1 -1
  11. package/dist/esm/metrics.d.ts +1 -1
  12. package/dist/esm/metrics.js.map +1 -1
  13. package/dist/esm/session/manager.d.ts +7 -6
  14. package/dist/esm/session/manager.js +36 -15
  15. package/dist/esm/session/manager.js.map +1 -1
  16. package/dist/esm/session/types.d.ts +1 -1
  17. package/dist/esm/session/types.js.map +1 -1
  18. package/dist/esm/signer.d.ts +4 -0
  19. package/dist/esm/signer.js.map +1 -1
  20. package/dist/esm/types.d.ts +2 -1
  21. package/dist/esm/types.js +1 -0
  22. package/dist/esm/types.js.map +1 -1
  23. package/dist/esm/version.d.ts +1 -1
  24. package/dist/esm/version.js +1 -1
  25. package/dist/esm/version.js.map +1 -1
  26. package/dist/types/base.d.ts +2 -0
  27. package/dist/types/base.d.ts.map +1 -1
  28. package/dist/types/client/base.d.ts +6 -2
  29. package/dist/types/client/base.d.ts.map +1 -1
  30. package/dist/types/client/index.d.ts +31 -1
  31. package/dist/types/client/index.d.ts.map +1 -1
  32. package/dist/types/client/types.d.ts +21 -1
  33. package/dist/types/client/types.d.ts.map +1 -1
  34. package/dist/types/metrics.d.ts +1 -1
  35. package/dist/types/metrics.d.ts.map +1 -1
  36. package/dist/types/session/manager.d.ts +7 -6
  37. package/dist/types/session/manager.d.ts.map +1 -1
  38. package/dist/types/session/types.d.ts +1 -1
  39. package/dist/types/session/types.d.ts.map +1 -1
  40. package/dist/types/signer.d.ts +4 -0
  41. package/dist/types/signer.d.ts.map +1 -1
  42. package/dist/types/types.d.ts +2 -1
  43. package/dist/types/types.d.ts.map +1 -1
  44. package/dist/types/version.d.ts +1 -1
  45. package/package.json +4 -4
  46. package/src/base.ts +90 -30
  47. package/src/client/base.ts +6 -1
  48. package/src/client/index.ts +45 -4
  49. package/src/client/types.ts +21 -1
  50. package/src/metrics.ts +6 -1
  51. package/src/session/manager.ts +62 -29
  52. package/src/session/types.ts +1 -1
  53. package/src/signer.ts +10 -1
  54. package/src/types.ts +1 -0
  55. package/src/version.ts +1 -1
@@ -118,11 +118,12 @@ export class AlchemySignerWebClient extends BaseSignerClient {
118
118
  writable: true,
119
119
  value: async (params) => {
120
120
  if (params.type === "email") {
121
- this.eventEmitter.emit("authenticating", { type: "email" });
122
- const { email, expirationSeconds } = params;
121
+ this.eventEmitter.emit("authenticating", { type: "otp" });
122
+ const { email, emailMode, expirationSeconds } = params;
123
123
  const publicKey = await this.initIframeStamper();
124
124
  const response = await this.request("/v1/signup", {
125
125
  email,
126
+ emailMode,
126
127
  targetPublicKey: publicKey,
127
128
  expirationSeconds,
128
129
  redirectParams: params.redirectParams?.toString(),
@@ -178,11 +179,12 @@ export class AlchemySignerWebClient extends BaseSignerClient {
178
179
  configurable: true,
179
180
  writable: true,
180
181
  value: async (params) => {
181
- this.eventEmitter.emit("authenticating", { type: "email" });
182
- const { email, expirationSeconds } = params;
182
+ this.eventEmitter.emit("authenticating", { type: "otp" });
183
+ const { email, emailMode, expirationSeconds } = params;
183
184
  const publicKey = await this.initIframeStamper();
184
185
  return this.request("/v1/auth", {
185
186
  email,
187
+ emailMode,
186
188
  targetPublicKey: publicKey,
187
189
  expirationSeconds,
188
190
  redirectParams: params.redirectParams?.toString(),
@@ -651,6 +653,41 @@ export class AlchemySignerWebClient extends BaseSignerClient {
651
653
  });
652
654
  this.oauthCallbackUrl = oauthCallbackUrl;
653
655
  }
656
+ /**
657
+ * Authenticates using an OTP code which was previously received via email.
658
+ *
659
+ * @example
660
+ * ```ts
661
+ * import { AlchemySignerWebClient } from "@account-kit/signer";
662
+ *
663
+ * const client = new AlchemySignerWebClient({
664
+ * connection: {
665
+ * apiKey: "your-api-key",
666
+ * },
667
+ * iframeConfig: {
668
+ * iframeContainerId: "signer-iframe-container",
669
+ * },
670
+ * });
671
+ *
672
+ * const account = await client.submitOtpCode({
673
+ * orgId: "user-org-id",
674
+ * otpId: "opt-returned-from-initEmailAuth",
675
+ * otpCode: "otp-code-from-email",
676
+ * });
677
+ * ```
678
+ *
679
+ * @param {Omit<OtpParams, "targetPublicKey">} args The parameters for the OTP request, excluding the target public key.
680
+ * @returns {Promise<{ bundle: string }>} A promise that resolves to an object containing the credential bundle.
681
+ */
682
+ async submitOtpCode(args) {
683
+ this.eventEmitter.emit("authenticating", { type: "otpVerify" });
684
+ const targetPublicKey = await this.initIframeStamper();
685
+ const { credentialBundle } = await this.request("/v1/otp", {
686
+ ...args,
687
+ targetPublicKey,
688
+ });
689
+ return { bundle: credentialBundle };
690
+ }
654
691
  }
655
692
  function resolveRelativeUrl(url) {
656
693
  // Funny trick.
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,wBAAwB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEtE,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,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AAG,IAAI,eAAe,CAAC;YACzC,IAAI,EAAE,IAAI,IAAI,MAAM,CAAC,QAAQ,CAAC,QAAQ;SACvC,CAAC,CAAC;QAEH,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;IAC3C,CAAC;CAwkBF;AAED,SAAS,kBAAkB,CAAC,GAAW;IACrC,eAAe;IACf,MAAM,CAAC,GAAG,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;IACtC,CAAC,CAAC,IAAI,GAAG,GAAG,CAAC;IACb,OAAO,CAAC,CAAC,IAAI,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,SAAS,iBAAiB,CAAC,KAAa;IACtC,OAAO,KAAK,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,UAAU,KAAK,EAAE,CAAC;AAC/D,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,mBAAoB,SAAQ,SAAS;IAGhD;;;OAGG;IACH;QACE,KAAK,CAAC,iBAAiB,CAAC,CAAC;QAPlB;;;;mBAAO,qBAAqB;WAAC;IAQtC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,gBAAiB,SAAQ,SAAS;IAA/C;;QACW;;;;mBAAO,kBAAkB;WAAC;IACrC,CAAC;CAAA","sourcesContent":["import { BaseError, ConnectionConfigSchema } from \"@aa-sdk/core\";\nimport { getWebAuthnAttestation } from \"@turnkey/http\";\nimport { IframeStamper } from \"@turnkey/iframe-stamper\";\nimport { WebauthnStamper } from \"@turnkey/webauthn-stamper\";\nimport { z } from \"zod\";\nimport { OAuthProvidersError } from \"../errors.js\";\nimport { getDefaultScopeAndClaims, getOauthNonce } from \"../oauth.js\";\nimport type { AuthParams, OauthMode } from \"../signer.js\";\nimport { base64UrlEncode } from \"../utils/base64UrlEncode.js\";\nimport { generateRandomBuffer } from \"../utils/generateRandomBuffer.js\";\nimport { BaseSignerClient } from \"./base.js\";\nimport type {\n AlchemySignerClientEvents,\n AuthenticatingEventMetadata,\n CreateAccountParams,\n CredentialCreationOptionOverrides,\n EmailAuthParams,\n ExportWalletParams,\n OauthConfig,\n OauthParams,\n User,\n} from \"./types.js\";\n\nconst CHECK_CLOSE_INTERVAL = 500;\n\nexport const AlchemySignerClientParamsSchema = z.object({\n connection: ConnectionConfigSchema,\n iframeConfig: z.object({\n iframeElementId: z.string().default(\"turnkey-iframe\"),\n iframeContainerId: z.string(),\n }),\n rpId: z.string().optional(),\n rootOrgId: z\n .string()\n .optional()\n .default(\"24c1acf5-810f-41e0-a503-d5d13fa8e830\"),\n oauthCallbackUrl: z\n .string()\n .optional()\n .default(\"https://signer.alchemy.com/callback\"),\n enablePopupOauth: z.boolean().optional().default(false),\n});\n\nexport type AlchemySignerClientParams = z.input<\n typeof AlchemySignerClientParamsSchema\n>;\n\ntype OauthState = {\n authProviderId: string;\n isCustomProvider?: boolean;\n requestKey: string;\n turnkeyPublicKey: string;\n expirationSeconds?: number;\n redirectUrl?: string;\n openerOrigin?: string;\n};\n\n/**\n * A lower level client used by the AlchemySigner used to communicate with\n * Alchemy's signer service.\n */\nexport class AlchemySignerWebClient extends BaseSignerClient<ExportWalletParams> {\n private iframeStamper: IframeStamper;\n private webauthnStamper: WebauthnStamper;\n oauthCallbackUrl: string;\n iframeContainerId: string;\n\n /**\n * Initializes a new instance with the given parameters, setting up the connection, iframe configuration, and WebAuthn stamper.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n * ```\n *\n * @param {AlchemySignerClientParams} params the parameters required to initialize the client\n * @param {ConnectionConfig} params.connection The connection details needed to connect to the service\n * @param {{ iframeElementId?: string; iframeContainerId: string }} params.iframeConfig The configuration details for setting up the iframe stamper\n * @param {string} params.rpId The relying party ID, defaulting to the current hostname if not provided\n * @param {string} params.rootOrgId The root organization ID\n */\n constructor(params: AlchemySignerClientParams) {\n const { connection, iframeConfig, rpId, rootOrgId, oauthCallbackUrl } =\n AlchemySignerClientParamsSchema.parse(params);\n\n const iframeStamper = new IframeStamper({\n iframeElementId: iframeConfig.iframeElementId,\n iframeUrl: \"https://auth.turnkey.com\",\n iframeContainer: document.getElementById(iframeConfig.iframeContainerId),\n });\n\n super({\n connection,\n rootOrgId,\n stamper: iframeStamper,\n });\n\n this.iframeStamper = iframeStamper;\n this.iframeContainerId = iframeConfig.iframeContainerId;\n\n this.webauthnStamper = new WebauthnStamper({\n rpId: rpId ?? window.location.hostname,\n });\n\n this.oauthCallbackUrl = oauthCallbackUrl;\n }\n\n /**\n * Authenticates the user by either email or passkey account creation flow. Emits events during the process.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.createAccount({ type: \"email\", email: \"you@mail.com\" });\n * ```\n *\n * @param {CreateAccountParams} params The parameters for creating an account, including the type (email or passkey) and additional details.\n * @returns {Promise<SignupResponse>} A promise that resolves with the response object containing the account creation result.\n */\n public override createAccount = async (params: CreateAccountParams) => {\n if (params.type === \"email\") {\n this.eventEmitter.emit(\"authenticating\", { type: \"email\" });\n const { email, expirationSeconds } = params;\n const publicKey = await this.initIframeStamper();\n\n const response = await this.request(\"/v1/signup\", {\n email,\n targetPublicKey: publicKey,\n expirationSeconds,\n redirectParams: params.redirectParams?.toString(),\n });\n\n return response;\n }\n\n this.eventEmitter.emit(\"authenticating\", { type: \"passkey\" });\n // Passkey account creation flow\n const { attestation, challenge } = await this.getWebAuthnAttestation(\n params.creationOpts,\n { username: \"email\" in params ? params.email : params.username }\n );\n\n const result = await this.request(\"/v1/signup\", {\n passkey: {\n challenge: base64UrlEncode(challenge),\n attestation,\n },\n email: \"email\" in params ? params.email : undefined,\n });\n\n this.user = {\n orgId: result.orgId,\n address: result.address!,\n userId: result.userId!,\n credentialId: attestation.credentialId,\n };\n this.initWebauthnStamper(this.user);\n this.eventEmitter.emit(\"connectedPasskey\", this.user);\n\n return result;\n };\n\n /**\n * Begin authenticating a user with their email and an expiration time for the authentication request. Initializes the iframe stamper to get the target public key.\n * This method sends an email to the user to complete their login\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.initEmailAuth({ email: \"you@mail.com\" });\n * ```\n *\n * @param {Omit<EmailAuthParams, \"targetPublicKey\">} params The parameters for email authentication, excluding the target public key\n * @returns {Promise<any>} The response from the authentication request\n */\n public override initEmailAuth = async (\n params: Omit<EmailAuthParams, \"targetPublicKey\">\n ) => {\n this.eventEmitter.emit(\"authenticating\", { type: \"email\" });\n const { email, expirationSeconds } = params;\n const publicKey = await this.initIframeStamper();\n\n return this.request(\"/v1/auth\", {\n email,\n targetPublicKey: publicKey,\n expirationSeconds,\n redirectParams: params.redirectParams?.toString(),\n });\n };\n\n /**\n * Completes auth for the user by injecting a credential bundle and retrieving\n * the user information based on the provided organization ID. Emits events\n * during the process.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.completeAuthWithBundle({ orgId: \"user-org-id\", bundle: \"bundle-from-email\", connectedEventName: \"connectedEmail\" });\n * ```\n *\n * @param {{ bundle: string; orgId: string, connectedEventName: keyof AlchemySignerClientEvents, idToken?: string }} config\n * The configuration object for the authentication function containing the\n * credential bundle to inject and the organization id associated with the\n * user, as well as the event to be emitted on success and optionally an OIDC\n * ID token with extra user information\n * @returns {Promise<User>} A promise that resolves to the authenticated user\n * information\n */\n public override completeAuthWithBundle = async ({\n bundle,\n orgId,\n connectedEventName,\n idToken,\n authenticatingType,\n }: {\n bundle: string;\n orgId: string;\n connectedEventName: keyof AlchemySignerClientEvents;\n authenticatingType: AuthenticatingEventMetadata[\"type\"];\n idToken?: string;\n }): Promise<User> => {\n this.eventEmitter.emit(\"authenticating\", { type: authenticatingType });\n await this.initIframeStamper();\n\n const result = await this.iframeStamper.injectCredentialBundle(bundle);\n\n if (!result) {\n throw new Error(\"Failed to inject credential bundle\");\n }\n\n const user = await this.whoami(orgId, idToken);\n\n this.eventEmitter.emit(connectedEventName, user, bundle);\n\n return user;\n };\n\n /**\n * Asynchronously handles the authentication process using WebAuthn Stamper. If a user is provided, sets the user and returns it. Otherwise, retrieves the current user and initializes the WebAuthn stamper.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.lookupUserWithPasskey();\n * ```\n *\n * @param {User} [user] An optional user object to authenticate\n * @returns {Promise<User>} A promise that resolves to the authenticated user object\n */\n public override lookupUserWithPasskey = async (\n user: User | undefined = undefined\n ) => {\n this.eventEmitter.emit(\"authenticating\", { type: \"passkey\" });\n await this.initWebauthnStamper(user);\n if (user) {\n this.user = user;\n this.eventEmitter.emit(\"connectedPasskey\", user);\n return user;\n }\n\n const result = await this.whoami(this.rootOrg);\n await this.initWebauthnStamper(result);\n this.eventEmitter.emit(\"connectedPasskey\", result);\n\n return result;\n };\n\n /**\n * Initiates the export of a wallet by creating an iframe stamper and calling the appropriate export function.\n * The export can be based on a seed phrase or a private key.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.exportWallet({\n * iframeContainerId: \"export-iframe-container\",\n * });\n * ```\n *\n * @param {ExportWalletParams} config The parameters for exporting the wallet\n * @param {string} config.iframeContainerId The ID of the container element that will hold the iframe stamper\n * @param {string} [config.iframeElementId] Optional ID for the iframe element\n * @returns {Promise<void>} A promise that resolves when the export process is complete\n */\n public override exportWallet = async ({\n iframeContainerId,\n iframeElementId = \"turnkey-export-iframe\",\n }: ExportWalletParams) => {\n const exportWalletIframeStamper = new IframeStamper({\n iframeContainer: document.getElementById(iframeContainerId),\n iframeElementId: iframeElementId,\n iframeUrl: \"https://export.turnkey.com\",\n });\n await exportWalletIframeStamper.init();\n\n if (this.turnkeyClient.stamper === this.iframeStamper) {\n return this.exportWalletInner({\n exportStamper: exportWalletIframeStamper,\n exportAs: \"SEED_PHRASE\",\n });\n }\n\n return this.exportWalletInner({\n exportStamper: exportWalletIframeStamper,\n exportAs: \"PRIVATE_KEY\",\n });\n };\n\n /**\n * Asynchronous function that clears the user and resets the iframe stamper.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.disconnect();\n * ```\n */\n public override disconnect = async () => {\n this.user = undefined;\n this.iframeStamper.clear();\n await this.iframeStamper.init();\n };\n\n /**\n * Redirects the user to the OAuth provider URL based on the provided arguments. This function will always reject after 1 second if the redirection does not occur.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * await client.oauthWithRedirect({\n * type: \"oauth\",\n * authProviderId: \"google\",\n * mode: \"redirect\",\n * redirectUrl: \"/\",\n * });\n * ```\n *\n * @param {Extract<AuthParams, { type: \"oauth\"; mode: \"redirect\" }>} args The arguments required to obtain the OAuth provider URL\n * @returns {Promise<never>} A promise that will never resolve, only reject if the redirection fails\n */\n public override oauthWithRedirect = async (\n args: Extract<AuthParams, { type: \"oauth\"; mode: \"redirect\" }>\n ): Promise<never> => {\n const providerUrl = await this.getOauthProviderUrl(args);\n window.location.href = providerUrl;\n return new Promise((_, reject) =>\n setTimeout(() => reject(\"Failed to redirect to OAuth provider\"), 1000)\n );\n };\n\n /**\n * Initiates an OAuth authentication flow in a popup window and returns the authenticated user.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const user = await client.oauthWithPopup({\n * type: \"oauth\",\n * authProviderId: \"google\",\n * mode: \"popup\"\n * });\n * ```\n *\n * @param {Extract<AuthParams, { type: \"oauth\"; mode: \"popup\" }>} args The authentication parameters specifying OAuth type and popup mode\n * @returns {Promise<User>} A promise that resolves to a `User` object containing the authenticated user information\n */\n public override oauthWithPopup = async (\n args: Extract<AuthParams, { type: \"oauth\"; mode: \"popup\" }>\n ): Promise<User> => {\n const providerUrl = await this.getOauthProviderUrl(args);\n const popup = window.open(\n providerUrl,\n \"_blank\",\n \"popup,width=500,height=600\"\n );\n const eventEmitter = this.eventEmitter;\n return new Promise((resolve, reject) => {\n const handleMessage = (event: MessageEvent) => {\n if (!event.data) {\n return;\n }\n const {\n alchemyBundle: bundle,\n alchemyOrgId: orgId,\n alchemyIdToken: idToken,\n alchemyIsSignup: isSignup,\n alchemyError,\n } = event.data;\n if (bundle && orgId && idToken) {\n cleanup();\n popup?.close();\n this.completeAuthWithBundle({\n bundle,\n orgId,\n connectedEventName: \"connectedOauth\",\n idToken,\n authenticatingType: \"oauth\",\n }).then((user) => {\n if (isSignup) {\n eventEmitter.emit(\"newUserSignup\");\n }\n\n resolve(user);\n }, reject);\n } else if (alchemyError) {\n cleanup();\n popup?.close();\n reject(new OauthFailedError(alchemyError));\n }\n };\n\n window.addEventListener(\"message\", handleMessage);\n\n const checkCloseIntervalId = setInterval(() => {\n if (popup?.closed) {\n cleanup();\n reject(new OauthCancelledError());\n }\n }, CHECK_CLOSE_INTERVAL);\n\n const cleanup = () => {\n window.removeEventListener(\"message\", handleMessage);\n clearInterval(checkCloseIntervalId);\n };\n });\n };\n\n private getOauthProviderUrl = async (args: OauthParams): Promise<string> => {\n const {\n authProviderId,\n isCustomProvider,\n auth0Connection,\n scope: providedScope,\n claims: providedClaims,\n mode,\n redirectUrl,\n expirationSeconds,\n } = args;\n const { codeChallenge, requestKey, authProviders } =\n await this.getOauthConfigForMode(mode);\n if (!authProviders) {\n throw new OAuthProvidersError();\n }\n const authProvider = authProviders.find(\n (provider) =>\n provider.id === authProviderId &&\n !!provider.isCustomProvider === !!isCustomProvider\n );\n if (!authProvider) {\n throw new Error(`No auth provider found with id ${authProviderId}`);\n }\n let scope: string;\n let claims: string | undefined;\n if (providedScope) {\n scope = addOpenIdIfAbsent(providedScope);\n claims = providedClaims;\n } else {\n if (isCustomProvider) {\n throw new Error(\"scope must be provided for a custom provider\");\n }\n const scopeAndClaims = getDefaultScopeAndClaims(authProviderId);\n if (!scopeAndClaims) {\n throw new Error(\n `Default scope not known for provider ${authProviderId}`\n );\n }\n ({ scope, claims } = scopeAndClaims);\n }\n const { authEndpoint, clientId } = authProvider;\n const turnkeyPublicKey = await this.initIframeStamper();\n const nonce = getOauthNonce(turnkeyPublicKey);\n const stateObject: OauthState = {\n authProviderId,\n isCustomProvider,\n requestKey,\n turnkeyPublicKey,\n expirationSeconds,\n redirectUrl:\n mode === \"redirect\" ? resolveRelativeUrl(redirectUrl) : undefined,\n openerOrigin: mode === \"popup\" ? window.location.origin : undefined,\n };\n const state = base64UrlEncode(\n new TextEncoder().encode(JSON.stringify(stateObject))\n );\n const authUrl = new URL(authEndpoint);\n const params: Record<string, string> = {\n redirect_uri: this.oauthCallbackUrl,\n response_type: \"code\",\n scope,\n state,\n code_challenge: codeChallenge,\n code_challenge_method: \"S256\",\n prompt: \"select_account\",\n client_id: clientId,\n nonce,\n };\n if (claims) {\n params.claims = claims;\n }\n if (auth0Connection) {\n params.connection = auth0Connection;\n }\n authUrl.search = new URLSearchParams(params).toString();\n return authUrl.toString();\n };\n\n private initIframeStamper = async () => {\n if (!this.iframeStamper.publicKey()) {\n await this.iframeStamper.init();\n }\n\n this.setStamper(this.iframeStamper);\n\n return this.iframeStamper.publicKey()!;\n };\n\n private initWebauthnStamper = async (user: User | undefined = this.user) => {\n this.setStamper(this.webauthnStamper);\n if (user && user.credentialId) {\n // The goal here is to allow us to cache the allowed credential, but this doesn't work with hybrid transport :(\n this.webauthnStamper.allowCredentials = [\n {\n id: Buffer.from(user.credentialId, \"base64\"),\n type: \"public-key\",\n transports: [\"internal\", \"hybrid\"],\n },\n ];\n }\n };\n\n protected override getWebAuthnAttestation = async (\n options?: CredentialCreationOptionOverrides,\n userDetails: { username: string } = {\n username: this.user?.email ?? \"anonymous\",\n }\n ) => {\n const challenge = generateRandomBuffer();\n const authenticatorUserId = generateRandomBuffer();\n\n const attestation = await getWebAuthnAttestation({\n publicKey: {\n ...options?.publicKey,\n authenticatorSelection: {\n residentKey: \"preferred\",\n requireResidentKey: false,\n userVerification: \"preferred\",\n ...options?.publicKey?.authenticatorSelection,\n },\n challenge,\n rp: {\n id: window.location.hostname,\n name: window.location.hostname,\n ...options?.publicKey?.rp,\n },\n pubKeyCredParams: [\n {\n type: \"public-key\",\n alg: -7,\n },\n {\n type: \"public-key\",\n alg: -257,\n },\n ],\n user: {\n id: authenticatorUserId,\n name: userDetails.username,\n displayName: userDetails.username,\n ...options?.publicKey?.user,\n },\n },\n signal: options?.signal,\n });\n\n // on iOS sometimes this is returned as empty or null, so handling that here\n if (attestation.transports == null || attestation.transports.length === 0) {\n attestation.transports = [\n \"AUTHENTICATOR_TRANSPORT_INTERNAL\",\n \"AUTHENTICATOR_TRANSPORT_HYBRID\",\n ];\n }\n\n return { challenge, authenticatorUserId, attestation };\n };\n\n protected override getOauthConfig = async (): Promise<OauthConfig> => {\n const currentStamper = this.turnkeyClient.stamper;\n const publicKey = await this.initIframeStamper();\n\n // swap the stamper back in case the user logged in with a different stamper (passkeys)\n this.setStamper(currentStamper);\n const nonce = getOauthNonce(publicKey);\n return this.request(\"/v1/prepare-oauth\", { nonce });\n };\n\n private getOauthConfigForMode = async (\n mode: OauthMode\n ): Promise<OauthConfig> => {\n if (this.oauthConfig) {\n return this.oauthConfig;\n } else if (mode === \"redirect\") {\n return this.initOauth();\n } else {\n throw new Error(\n \"enablePopupOauth must be set in configuration or signer.preparePopupOauth must be called before using popup-based OAuth login\"\n );\n }\n };\n}\n\nfunction resolveRelativeUrl(url: string): string {\n // Funny trick.\n const a = document.createElement(\"a\");\n a.href = url;\n return a.href;\n}\n\n/**\n * \"openid\" is a required scope in the OIDC protocol. Insert it if the user\n * forgot.\n *\n * @param {string} scope scope param which may be missing \"openid\"\n * @returns {string} scope which most definitely contains \"openid\"\n */\nfunction addOpenIdIfAbsent(scope: string): string {\n return scope.match(/\\bopenid\\b/) ? scope : `openid ${scope}`;\n}\n\n/**\n * This error is thrown when the OAuth flow is cancelled because the auth popup\n * window was closed.\n */\nexport class OauthCancelledError extends BaseError {\n override name = \"OauthCancelledError\";\n\n /**\n * Constructor for initializing an error indicating that the OAuth flow was\n * cancelled.\n */\n constructor() {\n super(\"OAuth cancelled\");\n }\n}\n\n/**\n * This error is thrown when an error occurs during the OAuth login flow.\n */\nexport class OauthFailedError extends BaseError {\n override name = \"OauthFailedError\";\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS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,IAAI,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,SAAS,iBAAiB,CAAC,KAAa;IACtC,OAAO,KAAK,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,UAAU,KAAK,EAAE,CAAC;AAC/D,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,mBAAoB,SAAQ,SAAS;IAGhD;;;OAGG;IACH;QACE,KAAK,CAAC,iBAAiB,CAAC,CAAC;QAPlB;;;;mBAAO,qBAAqB;WAAC;IAQtC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,gBAAiB,SAAQ,SAAS;IAA/C;;QACW;;;;mBAAO,kBAAkB;WAAC;IACrC,CAAC;CAAA","sourcesContent":["import { BaseError, ConnectionConfigSchema } from \"@aa-sdk/core\";\nimport { getWebAuthnAttestation } from \"@turnkey/http\";\nimport { IframeStamper } from \"@turnkey/iframe-stamper\";\nimport { WebauthnStamper } from \"@turnkey/webauthn-stamper\";\nimport { z } from \"zod\";\nimport { OAuthProvidersError } from \"../errors.js\";\nimport { getDefaultScopeAndClaims, getOauthNonce } from \"../oauth.js\";\nimport type { AuthParams, OauthMode } from \"../signer.js\";\nimport { base64UrlEncode } from \"../utils/base64UrlEncode.js\";\nimport { generateRandomBuffer } from \"../utils/generateRandomBuffer.js\";\nimport { BaseSignerClient } from \"./base.js\";\nimport type {\n AlchemySignerClientEvents,\n AuthenticatingEventMetadata,\n CreateAccountParams,\n CredentialCreationOptionOverrides,\n EmailAuthParams,\n ExportWalletParams,\n OauthConfig,\n OauthParams,\n OtpParams,\n User,\n} from \"./types.js\";\n\nconst CHECK_CLOSE_INTERVAL = 500;\n\nexport const AlchemySignerClientParamsSchema = z.object({\n connection: ConnectionConfigSchema,\n iframeConfig: z.object({\n iframeElementId: z.string().default(\"turnkey-iframe\"),\n iframeContainerId: z.string(),\n }),\n rpId: z.string().optional(),\n rootOrgId: z\n .string()\n .optional()\n .default(\"24c1acf5-810f-41e0-a503-d5d13fa8e830\"),\n oauthCallbackUrl: z\n .string()\n .optional()\n .default(\"https://signer.alchemy.com/callback\"),\n enablePopupOauth: z.boolean().optional().default(false),\n});\n\nexport type AlchemySignerClientParams = z.input<\n typeof AlchemySignerClientParamsSchema\n>;\n\ntype OauthState = {\n authProviderId: string;\n isCustomProvider?: boolean;\n requestKey: string;\n turnkeyPublicKey: string;\n expirationSeconds?: number;\n redirectUrl?: string;\n openerOrigin?: string;\n};\n\n/**\n * A lower level client used by the AlchemySigner used to communicate with\n * Alchemy's signer service.\n */\nexport class AlchemySignerWebClient extends BaseSignerClient<ExportWalletParams> {\n private iframeStamper: IframeStamper;\n private webauthnStamper: WebauthnStamper;\n oauthCallbackUrl: string;\n iframeContainerId: string;\n\n /**\n * Initializes a new instance with the given parameters, setting up the connection, iframe configuration, and WebAuthn stamper.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n * ```\n *\n * @param {AlchemySignerClientParams} params the parameters required to initialize the client\n * @param {ConnectionConfig} params.connection The connection details needed to connect to the service\n * @param {{ iframeElementId?: string; iframeContainerId: string }} params.iframeConfig The configuration details for setting up the iframe stamper\n * @param {string} params.rpId The relying party ID, defaulting to the current hostname if not provided\n * @param {string} params.rootOrgId The root organization ID\n */\n constructor(params: AlchemySignerClientParams) {\n const { connection, iframeConfig, rpId, rootOrgId, oauthCallbackUrl } =\n AlchemySignerClientParamsSchema.parse(params);\n\n const iframeStamper = new IframeStamper({\n iframeElementId: iframeConfig.iframeElementId,\n iframeUrl: \"https://auth.turnkey.com\",\n iframeContainer: document.getElementById(iframeConfig.iframeContainerId),\n });\n\n super({\n connection,\n rootOrgId,\n stamper: iframeStamper,\n });\n\n this.iframeStamper = iframeStamper;\n this.iframeContainerId = iframeConfig.iframeContainerId;\n\n this.webauthnStamper = new WebauthnStamper({\n rpId: rpId ?? window.location.hostname,\n });\n\n this.oauthCallbackUrl = oauthCallbackUrl;\n }\n\n /**\n * Authenticates the user by either email or passkey account creation flow. Emits events during the process.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.createAccount({ type: \"email\", email: \"you@mail.com\" });\n * ```\n *\n * @param {CreateAccountParams} params The parameters for creating an account, including the type (email or passkey) and additional details.\n * @returns {Promise<SignupResponse>} A promise that resolves with the response object containing the account creation result.\n */\n public override createAccount = async (params: CreateAccountParams) => {\n if (params.type === \"email\") {\n this.eventEmitter.emit(\"authenticating\", { type: \"otp\" });\n const { email, emailMode, expirationSeconds } = params;\n const publicKey = await this.initIframeStamper();\n\n const response = await this.request(\"/v1/signup\", {\n email,\n emailMode,\n targetPublicKey: publicKey,\n expirationSeconds,\n redirectParams: params.redirectParams?.toString(),\n });\n\n return response;\n }\n\n this.eventEmitter.emit(\"authenticating\", { type: \"passkey\" });\n // Passkey account creation flow\n const { attestation, challenge } = await this.getWebAuthnAttestation(\n params.creationOpts,\n { username: \"email\" in params ? params.email : params.username }\n );\n\n const result = await this.request(\"/v1/signup\", {\n passkey: {\n challenge: base64UrlEncode(challenge),\n attestation,\n },\n email: \"email\" in params ? params.email : undefined,\n });\n\n this.user = {\n orgId: result.orgId,\n address: result.address!,\n userId: result.userId!,\n credentialId: attestation.credentialId,\n };\n this.initWebauthnStamper(this.user);\n this.eventEmitter.emit(\"connectedPasskey\", this.user);\n\n return result;\n };\n\n /**\n * Begin authenticating a user with their email and an expiration time for the authentication request. Initializes the iframe stamper to get the target public key.\n * This method sends an email to the user to complete their login\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.initEmailAuth({ email: \"you@mail.com\" });\n * ```\n *\n * @param {Omit<EmailAuthParams, \"targetPublicKey\">} params The parameters for email authentication, excluding the target public key\n * @returns {Promise<any>} The response from the authentication request\n */\n public override initEmailAuth = async (\n params: Omit<EmailAuthParams, \"targetPublicKey\">\n ) => {\n this.eventEmitter.emit(\"authenticating\", { type: \"otp\" });\n const { email, emailMode, expirationSeconds } = params;\n const publicKey = await this.initIframeStamper();\n\n return this.request(\"/v1/auth\", {\n email,\n emailMode,\n targetPublicKey: publicKey,\n expirationSeconds,\n redirectParams: params.redirectParams?.toString(),\n });\n };\n\n /**\n * Authenticates using an OTP code which was previously received via email.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.submitOtpCode({\n * orgId: \"user-org-id\",\n * otpId: \"opt-returned-from-initEmailAuth\",\n * otpCode: \"otp-code-from-email\",\n * });\n * ```\n *\n * @param {Omit<OtpParams, \"targetPublicKey\">} args The parameters for the OTP request, excluding the target public key.\n * @returns {Promise<{ bundle: string }>} A promise that resolves to an object containing the credential bundle.\n */\n public override async submitOtpCode(\n args: Omit<OtpParams, \"targetPublicKey\">\n ): Promise<{ bundle: string }> {\n this.eventEmitter.emit(\"authenticating\", { type: \"otpVerify\" });\n const targetPublicKey = await this.initIframeStamper();\n const { credentialBundle } = await this.request(\"/v1/otp\", {\n ...args,\n targetPublicKey,\n });\n return { bundle: credentialBundle };\n }\n\n /**\n * Completes auth for the user by injecting a credential bundle and retrieving\n * the user information based on the provided organization ID. Emits events\n * during the process.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.completeAuthWithBundle({ orgId: \"user-org-id\", bundle: \"bundle-from-email\", connectedEventName: \"connectedEmail\" });\n * ```\n *\n * @param {{ bundle: string; orgId: string, connectedEventName: keyof AlchemySignerClientEvents, idToken?: string }} config\n * The configuration object for the authentication function containing the\n * credential bundle to inject and the organization id associated with the\n * user, as well as the event to be emitted on success and optionally an OIDC\n * ID token with extra user information\n * @returns {Promise<User>} A promise that resolves to the authenticated user\n * information\n */\n public override completeAuthWithBundle = async ({\n bundle,\n orgId,\n connectedEventName,\n idToken,\n authenticatingType,\n }: {\n bundle: string;\n orgId: string;\n connectedEventName: keyof AlchemySignerClientEvents;\n authenticatingType: AuthenticatingEventMetadata[\"type\"];\n idToken?: string;\n }): Promise<User> => {\n this.eventEmitter.emit(\"authenticating\", { type: authenticatingType });\n await this.initIframeStamper();\n\n const result = await this.iframeStamper.injectCredentialBundle(bundle);\n\n if (!result) {\n throw new Error(\"Failed to inject credential bundle\");\n }\n\n const user = await this.whoami(orgId, idToken);\n\n this.eventEmitter.emit(connectedEventName, user, bundle);\n\n return user;\n };\n\n /**\n * Asynchronously handles the authentication process using WebAuthn Stamper. If a user is provided, sets the user and returns it. Otherwise, retrieves the current user and initializes the WebAuthn stamper.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.lookupUserWithPasskey();\n * ```\n *\n * @param {User} [user] An optional user object to authenticate\n * @returns {Promise<User>} A promise that resolves to the authenticated user object\n */\n public override lookupUserWithPasskey = async (\n user: User | undefined = undefined\n ) => {\n this.eventEmitter.emit(\"authenticating\", { type: \"passkey\" });\n await this.initWebauthnStamper(user);\n if (user) {\n this.user = user;\n this.eventEmitter.emit(\"connectedPasskey\", user);\n return user;\n }\n\n const result = await this.whoami(this.rootOrg);\n await this.initWebauthnStamper(result);\n this.eventEmitter.emit(\"connectedPasskey\", result);\n\n return result;\n };\n\n /**\n * Initiates the export of a wallet by creating an iframe stamper and calling the appropriate export function.\n * The export can be based on a seed phrase or a private key.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.exportWallet({\n * iframeContainerId: \"export-iframe-container\",\n * });\n * ```\n *\n * @param {ExportWalletParams} config The parameters for exporting the wallet\n * @param {string} config.iframeContainerId The ID of the container element that will hold the iframe stamper\n * @param {string} [config.iframeElementId] Optional ID for the iframe element\n * @returns {Promise<void>} A promise that resolves when the export process is complete\n */\n public override exportWallet = async ({\n iframeContainerId,\n iframeElementId = \"turnkey-export-iframe\",\n }: ExportWalletParams) => {\n const exportWalletIframeStamper = new IframeStamper({\n iframeContainer: document.getElementById(iframeContainerId),\n iframeElementId: iframeElementId,\n iframeUrl: \"https://export.turnkey.com\",\n });\n await exportWalletIframeStamper.init();\n\n if (this.turnkeyClient.stamper === this.iframeStamper) {\n return this.exportWalletInner({\n exportStamper: exportWalletIframeStamper,\n exportAs: \"SEED_PHRASE\",\n });\n }\n\n return this.exportWalletInner({\n exportStamper: exportWalletIframeStamper,\n exportAs: \"PRIVATE_KEY\",\n });\n };\n\n /**\n * Asynchronous function that clears the user and resets the iframe stamper.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const account = await client.disconnect();\n * ```\n */\n public override disconnect = async () => {\n this.user = undefined;\n this.iframeStamper.clear();\n await this.iframeStamper.init();\n };\n\n /**\n * Redirects the user to the OAuth provider URL based on the provided arguments. This function will always reject after 1 second if the redirection does not occur.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * await client.oauthWithRedirect({\n * type: \"oauth\",\n * authProviderId: \"google\",\n * mode: \"redirect\",\n * redirectUrl: \"/\",\n * });\n * ```\n *\n * @param {Extract<AuthParams, { type: \"oauth\"; mode: \"redirect\" }>} args The arguments required to obtain the OAuth provider URL\n * @returns {Promise<never>} A promise that will never resolve, only reject if the redirection fails\n */\n public override oauthWithRedirect = async (\n args: Extract<AuthParams, { type: \"oauth\"; mode: \"redirect\" }>\n ): Promise<never> => {\n const providerUrl = await this.getOauthProviderUrl(args);\n window.location.href = providerUrl;\n return new Promise((_, reject) =>\n setTimeout(() => reject(\"Failed to redirect to OAuth provider\"), 1000)\n );\n };\n\n /**\n * Initiates an OAuth authentication flow in a popup window and returns the authenticated user.\n *\n * @example\n * ```ts\n * import { AlchemySignerWebClient } from \"@account-kit/signer\";\n *\n * const client = new AlchemySignerWebClient({\n * connection: {\n * apiKey: \"your-api-key\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"signer-iframe-container\",\n * },\n * });\n *\n * const user = await client.oauthWithPopup({\n * type: \"oauth\",\n * authProviderId: \"google\",\n * mode: \"popup\"\n * });\n * ```\n *\n * @param {Extract<AuthParams, { type: \"oauth\"; mode: \"popup\" }>} args The authentication parameters specifying OAuth type and popup mode\n * @returns {Promise<User>} A promise that resolves to a `User` object containing the authenticated user information\n */\n public override oauthWithPopup = async (\n args: Extract<AuthParams, { type: \"oauth\"; mode: \"popup\" }>\n ): Promise<User> => {\n const providerUrl = await this.getOauthProviderUrl(args);\n const popup = window.open(\n providerUrl,\n \"_blank\",\n \"popup,width=500,height=600\"\n );\n const eventEmitter = this.eventEmitter;\n return new Promise((resolve, reject) => {\n const handleMessage = (event: MessageEvent) => {\n if (!event.data) {\n return;\n }\n const {\n alchemyBundle: bundle,\n alchemyOrgId: orgId,\n alchemyIdToken: idToken,\n alchemyIsSignup: isSignup,\n alchemyError,\n } = event.data;\n if (bundle && orgId && idToken) {\n cleanup();\n popup?.close();\n this.completeAuthWithBundle({\n bundle,\n orgId,\n connectedEventName: \"connectedOauth\",\n idToken,\n authenticatingType: \"oauth\",\n }).then((user) => {\n if (isSignup) {\n eventEmitter.emit(\"newUserSignup\");\n }\n\n resolve(user);\n }, reject);\n } else if (alchemyError) {\n cleanup();\n popup?.close();\n reject(new OauthFailedError(alchemyError));\n }\n };\n\n window.addEventListener(\"message\", handleMessage);\n\n const checkCloseIntervalId = setInterval(() => {\n if (popup?.closed) {\n cleanup();\n reject(new OauthCancelledError());\n }\n }, CHECK_CLOSE_INTERVAL);\n\n const cleanup = () => {\n window.removeEventListener(\"message\", handleMessage);\n clearInterval(checkCloseIntervalId);\n };\n });\n };\n\n private getOauthProviderUrl = async (args: OauthParams): Promise<string> => {\n const {\n authProviderId,\n isCustomProvider,\n auth0Connection,\n scope: providedScope,\n claims: providedClaims,\n mode,\n redirectUrl,\n expirationSeconds,\n } = args;\n const { codeChallenge, requestKey, authProviders } =\n await this.getOauthConfigForMode(mode);\n if (!authProviders) {\n throw new OAuthProvidersError();\n }\n const authProvider = authProviders.find(\n (provider) =>\n provider.id === authProviderId &&\n !!provider.isCustomProvider === !!isCustomProvider\n );\n if (!authProvider) {\n throw new Error(`No auth provider found with id ${authProviderId}`);\n }\n let scope: string;\n let claims: string | undefined;\n if (providedScope) {\n scope = addOpenIdIfAbsent(providedScope);\n claims = providedClaims;\n } else {\n if (isCustomProvider) {\n throw new Error(\"scope must be provided for a custom provider\");\n }\n const scopeAndClaims = getDefaultScopeAndClaims(authProviderId);\n if (!scopeAndClaims) {\n throw new Error(\n `Default scope not known for provider ${authProviderId}`\n );\n }\n ({ scope, claims } = scopeAndClaims);\n }\n const { authEndpoint, clientId } = authProvider;\n const turnkeyPublicKey = await this.initIframeStamper();\n const nonce = getOauthNonce(turnkeyPublicKey);\n const stateObject: OauthState = {\n authProviderId,\n isCustomProvider,\n requestKey,\n turnkeyPublicKey,\n expirationSeconds,\n redirectUrl:\n mode === \"redirect\" ? resolveRelativeUrl(redirectUrl) : undefined,\n openerOrigin: mode === \"popup\" ? window.location.origin : undefined,\n };\n const state = base64UrlEncode(\n new TextEncoder().encode(JSON.stringify(stateObject))\n );\n const authUrl = new URL(authEndpoint);\n const params: Record<string, string> = {\n redirect_uri: this.oauthCallbackUrl,\n response_type: \"code\",\n scope,\n state,\n code_challenge: codeChallenge,\n code_challenge_method: \"S256\",\n prompt: \"select_account\",\n client_id: clientId,\n nonce,\n };\n if (claims) {\n params.claims = claims;\n }\n if (auth0Connection) {\n params.connection = auth0Connection;\n }\n authUrl.search = new URLSearchParams(params).toString();\n return authUrl.toString();\n };\n\n private initIframeStamper = async () => {\n if (!this.iframeStamper.publicKey()) {\n await this.iframeStamper.init();\n }\n\n this.setStamper(this.iframeStamper);\n\n return this.iframeStamper.publicKey()!;\n };\n\n private initWebauthnStamper = async (user: User | undefined = this.user) => {\n this.setStamper(this.webauthnStamper);\n if (user && user.credentialId) {\n // The goal here is to allow us to cache the allowed credential, but this doesn't work with hybrid transport :(\n this.webauthnStamper.allowCredentials = [\n {\n id: Buffer.from(user.credentialId, \"base64\"),\n type: \"public-key\",\n transports: [\"internal\", \"hybrid\"],\n },\n ];\n }\n };\n\n protected override getWebAuthnAttestation = async (\n options?: CredentialCreationOptionOverrides,\n userDetails: { username: string } = {\n username: this.user?.email ?? \"anonymous\",\n }\n ) => {\n const challenge = generateRandomBuffer();\n const authenticatorUserId = generateRandomBuffer();\n\n const attestation = await getWebAuthnAttestation({\n publicKey: {\n ...options?.publicKey,\n authenticatorSelection: {\n residentKey: \"preferred\",\n requireResidentKey: false,\n userVerification: \"preferred\",\n ...options?.publicKey?.authenticatorSelection,\n },\n challenge,\n rp: {\n id: window.location.hostname,\n name: window.location.hostname,\n ...options?.publicKey?.rp,\n },\n pubKeyCredParams: [\n {\n type: \"public-key\",\n alg: -7,\n },\n {\n type: \"public-key\",\n alg: -257,\n },\n ],\n user: {\n id: authenticatorUserId,\n name: userDetails.username,\n displayName: userDetails.username,\n ...options?.publicKey?.user,\n },\n },\n signal: options?.signal,\n });\n\n // on iOS sometimes this is returned as empty or null, so handling that here\n if (attestation.transports == null || attestation.transports.length === 0) {\n attestation.transports = [\n \"AUTHENTICATOR_TRANSPORT_INTERNAL\",\n \"AUTHENTICATOR_TRANSPORT_HYBRID\",\n ];\n }\n\n return { challenge, authenticatorUserId, attestation };\n };\n\n protected override getOauthConfig = async (): Promise<OauthConfig> => {\n const currentStamper = this.turnkeyClient.stamper;\n const publicKey = await this.initIframeStamper();\n\n // swap the stamper back in case the user logged in with a different stamper (passkeys)\n this.setStamper(currentStamper);\n const nonce = getOauthNonce(publicKey);\n return this.request(\"/v1/prepare-oauth\", { nonce });\n };\n\n private getOauthConfigForMode = async (\n mode: OauthMode\n ): Promise<OauthConfig> => {\n if (this.oauthConfig) {\n return this.oauthConfig;\n } else if (mode === \"redirect\") {\n return this.initOauth();\n } else {\n throw new Error(\n \"enablePopupOauth must be set in configuration or signer.preparePopupOauth must be called before using popup-based OAuth login\"\n );\n }\n };\n}\n\nfunction resolveRelativeUrl(url: string): string {\n // Funny trick.\n const a = document.createElement(\"a\");\n a.href = url;\n return a.href;\n}\n\n/**\n * \"openid\" is a required scope in the OIDC protocol. Insert it if the user\n * forgot.\n *\n * @param {string} scope scope param which may be missing \"openid\"\n * @returns {string} scope which most definitely contains \"openid\"\n */\nfunction addOpenIdIfAbsent(scope: string): string {\n return scope.match(/\\bopenid\\b/) ? scope : `openid ${scope}`;\n}\n\n/**\n * This error is thrown when the OAuth flow is cancelled because the auth popup\n * window was closed.\n */\nexport class OauthCancelledError extends BaseError {\n override name = \"OauthCancelledError\";\n\n /**\n * Constructor for initializing an error indicating that the OAuth flow was\n * cancelled.\n */\n constructor() {\n super(\"OAuth cancelled\");\n }\n}\n\n/**\n * This error is thrown when an error occurs during the OAuth login flow.\n */\nexport class OauthFailedError extends BaseError {\n override name = \"OauthFailedError\";\n}\n"]}
@@ -21,6 +21,7 @@ export type ExportWalletParams = {
21
21
  export type CreateAccountParams = {
22
22
  type: "email";
23
23
  email: string;
24
+ emailMode?: EmailType;
24
25
  expirationSeconds?: number;
25
26
  redirectParams?: URLSearchParams;
26
27
  } | {
@@ -32,8 +33,10 @@ export type CreateAccountParams = {
32
33
  username: string;
33
34
  creationOpts?: CredentialCreationOptionOverrides;
34
35
  };
36
+ export type EmailType = "magicLink" | "otp";
35
37
  export type EmailAuthParams = {
36
38
  email: string;
39
+ emailMode?: EmailType;
37
40
  expirationSeconds?: number;
38
41
  targetPublicKey: string;
39
42
  redirectParams?: URLSearchParams;
@@ -43,10 +46,18 @@ export type OauthParams = Extract<AuthParams, {
43
46
  }> & {
44
47
  expirationSeconds?: number;
45
48
  };
49
+ export type OtpParams = {
50
+ orgId: string;
51
+ otpId: string;
52
+ otpCode: string;
53
+ targetPublicKey: string;
54
+ expirationSeconds?: number;
55
+ };
46
56
  export type SignupResponse = {
47
57
  orgId: string;
48
58
  userId?: string;
49
59
  address?: Address;
60
+ otpId?: string;
50
61
  };
51
62
  export type OauthConfig = {
52
63
  codeChallenge: string;
@@ -93,6 +104,7 @@ export type SignerEndpoints = [
93
104
  };
94
105
  Response: {
95
106
  orgId: string;
107
+ otpId?: string;
96
108
  };
97
109
  },
98
110
  {
@@ -119,10 +131,17 @@ export type SignerEndpoints = [
119
131
  nonce: string;
120
132
  };
121
133
  Response: OauthConfig;
134
+ },
135
+ {
136
+ Route: "/v1/otp";
137
+ Body: OtpParams;
138
+ Response: {
139
+ credentialBundle: string;
140
+ };
122
141
  }
123
142
  ];
124
143
  export type AuthenticatingEventMetadata = {
125
- type: "email" | "passkey" | "oauth";
144
+ type: "email" | "passkey" | "oauth" | "otp" | "otpVerify";
126
145
  };
127
146
  export type AlchemySignerClientEvents = {
128
147
  connected(user: User): void;
@@ -131,6 +150,7 @@ export type AlchemySignerClientEvents = {
131
150
  connectedEmail(user: User, bundle: string): void;
132
151
  connectedPasskey(user: User): void;
133
152
  connectedOauth(user: User, bundle: string): void;
153
+ connectedOtp(user: User, bundle: string): void;
134
154
  disconnected(): void;
135
155
  };
136
156
  export type AlchemySignerClientEvent = keyof AlchemySignerClientEvents;
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/client/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Address } from \"@aa-sdk/core\";\nimport type { TSignedRequest, getWebAuthnAttestation } from \"@turnkey/http\";\nimport type { Hex } from \"viem\";\nimport type { AuthParams } from \"../signer\";\n\nexport type CredentialCreationOptionOverrides = {\n publicKey?: Partial<CredentialCreationOptions[\"publicKey\"]>;\n} & Pick<CredentialCreationOptions, \"signal\">;\n\n// [!region User]\nexport type User = {\n email?: string;\n orgId: string;\n userId: string;\n address: Address;\n credentialId?: string;\n idToken?: string;\n claims?: Record<string, unknown>;\n};\n// [!endregion User]\n\nexport type ExportWalletParams = {\n iframeContainerId: string;\n iframeElementId?: string;\n};\n\nexport type CreateAccountParams =\n | {\n type: \"email\";\n email: string;\n expirationSeconds?: number;\n redirectParams?: URLSearchParams;\n }\n | {\n type: \"passkey\";\n email: string;\n creationOpts?: CredentialCreationOptionOverrides;\n }\n | {\n type: \"passkey\";\n username: string;\n creationOpts?: CredentialCreationOptionOverrides;\n };\n\nexport type EmailAuthParams = {\n email: string;\n expirationSeconds?: number;\n targetPublicKey: string;\n redirectParams?: URLSearchParams;\n};\n\nexport type OauthParams = Extract<AuthParams, { type: \"oauth\" }> & {\n expirationSeconds?: number;\n};\n\nexport type SignupResponse = {\n orgId: string;\n userId?: string;\n address?: Address;\n};\n\nexport type OauthConfig = {\n codeChallenge: string;\n requestKey: string;\n authProviders: AuthProviderConfig[];\n};\n\nexport type AuthProviderConfig = {\n id: string;\n isCustomProvider?: boolean;\n clientId: string;\n authEndpoint: string;\n};\n\nexport type SignerRoutes = SignerEndpoints[number][\"Route\"];\nexport type SignerBody<T extends SignerRoutes> = Extract<\n SignerEndpoints[number],\n { Route: T }\n>[\"Body\"];\nexport type SignerResponse<T extends SignerRoutes> = Extract<\n SignerEndpoints[number],\n { Route: T }\n>[\"Response\"];\n\nexport type SignerEndpoints = [\n {\n Route: \"/v1/signup\";\n Body:\n | (Omit<EmailAuthParams, \"redirectParams\"> & { redirectParams?: string })\n | {\n passkey: {\n challenge: string;\n attestation: Awaited<ReturnType<typeof getWebAuthnAttestation>>;\n };\n };\n Response: SignupResponse;\n },\n {\n Route: \"/v1/whoami\";\n Body: {\n stampedRequest: TSignedRequest;\n };\n Response: User;\n },\n {\n Route: \"/v1/auth\";\n Body: Omit<EmailAuthParams, \"redirectParams\"> & { redirectParams?: string };\n Response: {\n orgId: string;\n };\n },\n {\n Route: \"/v1/lookup\";\n Body: {\n email: string;\n };\n Response: {\n orgId: string | null;\n };\n },\n {\n Route: \"/v1/sign-payload\";\n Body: {\n stampedRequest: TSignedRequest;\n };\n Response: {\n signature: Hex;\n };\n },\n {\n Route: \"/v1/prepare-oauth\";\n Body: {\n nonce: string;\n };\n Response: OauthConfig;\n }\n];\n\nexport type AuthenticatingEventMetadata = {\n type: \"email\" | \"passkey\" | \"oauth\";\n};\n\nexport type AlchemySignerClientEvents = {\n connected(user: User): void;\n newUserSignup(): void;\n authenticating(data: AuthenticatingEventMetadata): void;\n connectedEmail(user: User, bundle: string): void;\n connectedPasskey(user: User): void;\n connectedOauth(user: User, bundle: string): void;\n disconnected(): void;\n};\n\nexport type AlchemySignerClientEvent = keyof AlchemySignerClientEvents;\n\nexport type GetWebAuthnAttestationResult = {\n attestation: Awaited<ReturnType<typeof getWebAuthnAttestation>>;\n challenge: ArrayBuffer;\n authenticatorUserId: ArrayBuffer;\n};\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/client/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Address } from \"@aa-sdk/core\";\nimport type { TSignedRequest, getWebAuthnAttestation } from \"@turnkey/http\";\nimport type { Hex } from \"viem\";\nimport type { AuthParams } from \"../signer\";\n\nexport type CredentialCreationOptionOverrides = {\n publicKey?: Partial<CredentialCreationOptions[\"publicKey\"]>;\n} & Pick<CredentialCreationOptions, \"signal\">;\n\n// [!region User]\nexport type User = {\n email?: string;\n orgId: string;\n userId: string;\n address: Address;\n credentialId?: string;\n idToken?: string;\n claims?: Record<string, unknown>;\n};\n// [!endregion User]\n\nexport type ExportWalletParams = {\n iframeContainerId: string;\n iframeElementId?: string;\n};\n\nexport type CreateAccountParams =\n | {\n type: \"email\";\n email: string;\n emailMode?: EmailType;\n expirationSeconds?: number;\n redirectParams?: URLSearchParams;\n }\n | {\n type: \"passkey\";\n email: string;\n creationOpts?: CredentialCreationOptionOverrides;\n }\n | {\n type: \"passkey\";\n username: string;\n creationOpts?: CredentialCreationOptionOverrides;\n };\n\nexport type EmailType = \"magicLink\" | \"otp\";\n\nexport type EmailAuthParams = {\n email: string;\n emailMode?: EmailType;\n expirationSeconds?: number;\n targetPublicKey: string;\n redirectParams?: URLSearchParams;\n};\n\nexport type OauthParams = Extract<AuthParams, { type: \"oauth\" }> & {\n expirationSeconds?: number;\n};\n\nexport type OtpParams = {\n orgId: string;\n otpId: string;\n otpCode: string;\n targetPublicKey: string;\n expirationSeconds?: number;\n};\n\nexport type SignupResponse = {\n orgId: string;\n userId?: string;\n address?: Address;\n otpId?: string;\n};\n\nexport type OauthConfig = {\n codeChallenge: string;\n requestKey: string;\n authProviders: AuthProviderConfig[];\n};\n\nexport type AuthProviderConfig = {\n id: string;\n isCustomProvider?: boolean;\n clientId: string;\n authEndpoint: string;\n};\n\nexport type SignerRoutes = SignerEndpoints[number][\"Route\"];\nexport type SignerBody<T extends SignerRoutes> = Extract<\n SignerEndpoints[number],\n { Route: T }\n>[\"Body\"];\nexport type SignerResponse<T extends SignerRoutes> = Extract<\n SignerEndpoints[number],\n { Route: T }\n>[\"Response\"];\n\nexport type SignerEndpoints = [\n {\n Route: \"/v1/signup\";\n Body:\n | (Omit<EmailAuthParams, \"redirectParams\"> & { redirectParams?: string })\n | {\n passkey: {\n challenge: string;\n attestation: Awaited<ReturnType<typeof getWebAuthnAttestation>>;\n };\n };\n Response: SignupResponse;\n },\n {\n Route: \"/v1/whoami\";\n Body: {\n stampedRequest: TSignedRequest;\n };\n Response: User;\n },\n {\n Route: \"/v1/auth\";\n Body: Omit<EmailAuthParams, \"redirectParams\"> & { redirectParams?: string };\n Response: {\n orgId: string;\n otpId?: string;\n };\n },\n {\n Route: \"/v1/lookup\";\n Body: {\n email: string;\n };\n Response: {\n orgId: string | null;\n };\n },\n {\n Route: \"/v1/sign-payload\";\n Body: {\n stampedRequest: TSignedRequest;\n };\n Response: {\n signature: Hex;\n };\n },\n {\n Route: \"/v1/prepare-oauth\";\n Body: {\n nonce: string;\n };\n Response: OauthConfig;\n },\n {\n Route: \"/v1/otp\";\n Body: OtpParams;\n Response: { credentialBundle: string };\n }\n];\n\nexport type AuthenticatingEventMetadata = {\n type: \"email\" | \"passkey\" | \"oauth\" | \"otp\" | \"otpVerify\";\n};\n\nexport type AlchemySignerClientEvents = {\n connected(user: User): void;\n newUserSignup(): void;\n authenticating(data: AuthenticatingEventMetadata): void;\n connectedEmail(user: User, bundle: string): void;\n connectedPasskey(user: User): void;\n connectedOauth(user: User, bundle: string): void;\n connectedOtp(user: User, bundle: string): void;\n disconnected(): void;\n};\n\nexport type AlchemySignerClientEvent = keyof AlchemySignerClientEvents;\n\nexport type GetWebAuthnAttestationResult = {\n attestation: Awaited<ReturnType<typeof getWebAuthnAttestation>>;\n challenge: ArrayBuffer;\n authenticatorUserId: ArrayBuffer;\n};\n"]}
@@ -2,7 +2,7 @@ export type SignerEventsSchema = [
2
2
  {
3
3
  EventName: "signer_authnticate";
4
4
  EventData: {
5
- authType: "email" | "passkey_anon" | "passkey_email" | "oauthReturn";
5
+ authType: "email" | "passkey_anon" | "passkey_email" | "otp" | "oauthReturn";
6
6
  provider?: never;
7
7
  } | {
8
8
  authType: "oauth";
@@ -1 +1 @@
1
- {"version":3,"file":"metrics.js","sourceRoot":"","sources":["../../src/metrics.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAkBvC,MAAM,CAAC,MAAM,YAAY,GAAG,YAAY,CAAqB;IAC3D,OAAO,EAAE,qBAAqB;IAC9B,OAAO,EAAE,OAAO;CACjB,CAAC,CAAC","sourcesContent":["import { createLogger } from \"@account-kit/logging\";\nimport { VERSION } from \"./version.js\";\n\nexport type SignerEventsSchema = [\n {\n EventName: \"signer_authnticate\";\n EventData:\n | {\n authType: \"email\" | \"passkey_anon\" | \"passkey_email\" | \"oauthReturn\";\n provider?: never;\n }\n | { authType: \"oauth\"; provider: string };\n },\n {\n EventName: \"signer_sign_message\";\n EventData: undefined;\n }\n];\n\nexport const SignerLogger = createLogger<SignerEventsSchema>({\n package: \"@account-kit/signer\",\n version: VERSION,\n});\n"]}
1
+ {"version":3,"file":"metrics.js","sourceRoot":"","sources":["../../src/metrics.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAuBvC,MAAM,CAAC,MAAM,YAAY,GAAG,YAAY,CAAqB;IAC3D,OAAO,EAAE,qBAAqB;IAC9B,OAAO,EAAE,OAAO;CACjB,CAAC,CAAC","sourcesContent":["import { createLogger } from \"@account-kit/logging\";\nimport { VERSION } from \"./version.js\";\n\nexport type SignerEventsSchema = [\n {\n EventName: \"signer_authnticate\";\n EventData:\n | {\n authType:\n | \"email\"\n | \"passkey_anon\"\n | \"passkey_email\"\n | \"otp\"\n | \"oauthReturn\";\n provider?: never;\n }\n | { authType: \"oauth\"; provider: string };\n },\n {\n EventName: \"signer_sign_message\";\n EventData: undefined;\n }\n];\n\nexport const SignerLogger = createLogger<SignerEventsSchema>({\n package: \"@account-kit/signer\",\n version: VERSION,\n});\n"]}
@@ -21,6 +21,10 @@ export declare const SessionManagerParamsSchema: z.ZodObject<{
21
21
  expirationTimeMs?: number | undefined;
22
22
  }>;
23
23
  export type SessionManagerParams = z.input<typeof SessionManagerParamsSchema>;
24
+ type TemporarySession = {
25
+ orgId: string;
26
+ isNewUser?: boolean;
27
+ };
24
28
  export declare class SessionManager {
25
29
  private sessionKey;
26
30
  private client;
@@ -31,12 +35,8 @@ export declare class SessionManager {
31
35
  constructor(params: SessionManagerParams);
32
36
  getSessionUser: () => Promise<User | null>;
33
37
  clearSession: () => void;
34
- setTemporarySession: (session: {
35
- orgId: string;
36
- }) => void;
37
- getTemporarySession: () => {
38
- orgId: string;
39
- } | null;
38
+ setTemporarySession: (session: TemporarySession) => void;
39
+ getTemporarySession: () => TemporarySession | null;
40
40
  on: <E extends keyof SessionManagerEvents>(event: E, listener: SessionManagerEvents[E]) => () => EventEmitter<SessionManagerEvents, any>;
41
41
  private getSession;
42
42
  private setSession;
@@ -46,3 +46,4 @@ export declare class SessionManager {
46
46
  private registerSessionExpirationHandler;
47
47
  private setSessionWithUserAndBundle;
48
48
  }
49
+ export {};
@@ -65,10 +65,18 @@ export class SessionManager {
65
65
  }
66
66
  switch (existingSession.type) {
67
67
  case "email":
68
- case "oauth": {
69
- const connectedEventName = existingSession.type === "email"
70
- ? "connectedEmail"
71
- : "connectedOauth";
68
+ case "oauth":
69
+ case "otp": {
70
+ const connectedEventName = (() => {
71
+ switch (existingSession.type) {
72
+ case "email":
73
+ return "connectedEmail";
74
+ case "oauth":
75
+ return "connectedOauth";
76
+ case "otp":
77
+ return "connectedOtp";
78
+ }
79
+ })();
72
80
  const result = await this.client
73
81
  .completeAuthWithBundle({
74
82
  bundle: existingSession.bundle,
@@ -191,18 +199,31 @@ export class SessionManager {
191
199
  this.eventEmitter.emit("disconnected");
192
200
  }
193
201
  });
194
- this.client.on("disconnected", () => this.clearSession());
195
- this.client.on("connectedEmail", (user, bundle) => this.setSessionWithUserAndBundle({ type: "email", user, bundle }));
196
- this.client.on("connectedPasskey", (user) => {
197
- const existingSession = this.getSession();
198
- if (existingSession != null &&
199
- existingSession.type === "passkey" &&
200
- existingSession.user.userId === user.userId) {
201
- return;
202
+ const listeners = {
203
+ connected: undefined,
204
+ newUserSignup: undefined,
205
+ authenticating: undefined,
206
+ connectedEmail: (user, bundle) => this.setSessionWithUserAndBundle({ type: "email", user, bundle }),
207
+ connectedPasskey: (user) => {
208
+ const existingSession = this.getSession();
209
+ if (existingSession != null &&
210
+ existingSession.type === "passkey" &&
211
+ existingSession.user.userId === user.userId) {
212
+ return;
213
+ }
214
+ this.setSession({ type: "passkey", user });
215
+ },
216
+ connectedOauth: (user, bundle) => this.setSessionWithUserAndBundle({ type: "oauth", user, bundle }),
217
+ connectedOtp: (user, bundle) => {
218
+ this.setSessionWithUserAndBundle({ type: "otp", user, bundle });
219
+ },
220
+ disconnected: () => this.clearSession(),
221
+ };
222
+ for (const [event, listener] of Object.entries(listeners)) {
223
+ if (listener) {
224
+ this.client.on(event, listener);
202
225
  }
203
- this.setSession({ type: "passkey", user });
204
- });
205
- this.client.on("connectedOauth", (user, bundle) => this.setSessionWithUserAndBundle({ type: "oauth", user, bundle }));
226
+ }
206
227
  // sync local state if persisted state has changed from another tab
207
228
  // only do this in the browser
208
229
  // Add a try catch to prevent potential crashes in non-browser environments
@@ -1 +1 @@
1
- {"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../src/session/manager.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,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YAC7B,CAAC,CAAC,WAAW,KAAK,cAAc;gBAC9B,CAAC,CAAC,YAAY;gBACd,CAAC,CAAC,cAAc;YAClB,CAAC,CAAC,WAAW,CAAC;QAClB,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QACzC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,YAAY,GAAG,IAAI,YAAY,EAAwB,CAAC;QAE7D,IAAI,CAAC,KAAK,GAAG,WAAW,CACtB,qBAAqB,CACnB,OAAO,CAAC,IAAI,CAAC,eAAe,EAAE;YAC5B,IAAI,EAAE,IAAI,CAAC,UAAU;YACrB,OAAO,EAAE,iBAAiB,CAAe,GAAG,EAAE,CAAC,OAAO,CAAC;SACxD,CAAC,CACH,CACF,CAAC;QAEF,IAAI,CAAC,sBAAsB,EAAE,CAAC;IAChC,CAAC;IA6HM,UAAU;QACf,IAAI,CAAC,cAAc,EAAE;aAClB,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE;YACb,4FAA4F;YAC5F,IAAI,IAAI;gBAAE,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,UAAU,EAAG,CAAC,CAAC;;gBAC7D,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QAC9C,CAAC,CAAC;aACD,OAAO,CAAC,GAAG,EAAE;YACZ,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;QACxC,CAAC,CAAC,CAAC;IACP,CAAC;IAEO,eAAe;QACrB,OAAO;YACL,OAAO,EAAE,IAAI;SACd,CAAC;IACJ,CAAC;CAiGF","sourcesContent":["import EventEmitter from \"eventemitter3\";\nimport { z } from \"zod\";\nimport {\n createJSONStorage,\n persist,\n subscribeWithSelector,\n} from \"zustand/middleware\";\nimport { createStore, type Mutate, type StoreApi } from \"zustand/vanilla\";\nimport type { BaseSignerClient } from \"../client/base\";\nimport type { User } from \"../client/types\";\nimport { assertNever } from \"../utils/typeAssertions.js\";\nimport type { Session, SessionManagerEvents } from \"./types\";\n\nexport const DEFAULT_SESSION_MS = 15 * 60 * 1000; // 15 minutes\n\nexport const SessionManagerParamsSchema = z.object({\n sessionKey: z.string().default(\"alchemy-signer-session\"),\n storage: z\n .enum([\"localStorage\", \"sessionStorage\"])\n .default(\"localStorage\")\n .or(z.custom<Storage>()),\n expirationTimeMs: z\n .number()\n .default(DEFAULT_SESSION_MS)\n .describe(\n \"The time in milliseconds that a session should last before expiring [default: 15 minutes]\"\n ),\n client: z.custom<BaseSignerClient>(),\n});\n\nexport type SessionManagerParams = z.input<typeof SessionManagerParamsSchema>;\n\ntype SessionState = {\n session: Session | null;\n};\n\ntype Store = Mutate<\n StoreApi<SessionState>,\n [[\"zustand/subscribeWithSelector\", never], [\"zustand/persist\", SessionState]]\n>;\n\nexport class SessionManager {\n private sessionKey: string;\n private client: BaseSignerClient;\n private eventEmitter: EventEmitter<SessionManagerEvents>;\n readonly expirationTimeMs: number;\n private store: Store;\n private clearSessionHandle: NodeJS.Timeout | null = null;\n\n constructor(params: SessionManagerParams) {\n const {\n sessionKey,\n storage: storageType,\n expirationTimeMs,\n client,\n } = SessionManagerParamsSchema.parse(params);\n this.sessionKey = sessionKey;\n const storage =\n typeof storageType === \"string\"\n ? storageType === \"localStorage\"\n ? localStorage\n : sessionStorage\n : storageType;\n this.expirationTimeMs = expirationTimeMs;\n this.client = client;\n this.eventEmitter = new EventEmitter<SessionManagerEvents>();\n\n this.store = createStore(\n subscribeWithSelector(\n persist(this.getInitialState, {\n name: this.sessionKey,\n storage: createJSONStorage<SessionState>(() => storage),\n })\n )\n );\n\n this.registerEventListeners();\n }\n\n public getSessionUser = async (): Promise<User | null> => {\n const existingSession = this.getSession();\n if (existingSession == null) {\n return null;\n }\n\n switch (existingSession.type) {\n case \"email\":\n case \"oauth\": {\n const connectedEventName =\n existingSession.type === \"email\"\n ? \"connectedEmail\"\n : \"connectedOauth\";\n const result = await this.client\n .completeAuthWithBundle({\n bundle: existingSession.bundle,\n orgId: existingSession.user.orgId,\n authenticatingType: existingSession.type,\n connectedEventName,\n idToken: existingSession.user.idToken,\n })\n .catch((e) => {\n console.warn(\"Failed to load user from session\", e);\n return null;\n });\n\n if (!result) {\n this.clearSession();\n return null;\n }\n\n return result;\n }\n case \"passkey\": {\n // we don't need to do much here if we already have a user\n // this will setup the client with the user context, but\n // requests still have to be signed by the user on first request\n // so this is fine\n return this.client.lookupUserWithPasskey(existingSession.user);\n }\n default:\n assertNever(\n existingSession,\n `Unknown session type: ${(existingSession as any).type}`\n );\n }\n };\n\n public clearSession = () => {\n this.store.setState({ session: null });\n\n if (this.clearSessionHandle) {\n clearTimeout(this.clearSessionHandle);\n }\n };\n\n public setTemporarySession = (session: { orgId: string }) => {\n // temporary session must be placed in localStorage so that it can be accessed across tabs\n localStorage.setItem(\n `${this.sessionKey}:temporary`,\n JSON.stringify(session)\n );\n };\n\n public getTemporarySession = (): { orgId: string } | null => {\n // temporary session must be placed in localStorage so that it can be accessed across tabs\n const sessionStr = localStorage.getItem(`${this.sessionKey}:temporary`);\n\n if (!sessionStr) {\n return null;\n }\n\n return JSON.parse(sessionStr);\n };\n\n on = <E extends keyof SessionManagerEvents>(\n event: E,\n listener: SessionManagerEvents[E]\n ) => {\n this.eventEmitter.on(event, listener as any);\n\n return () => this.eventEmitter.removeListener(event, listener as any);\n };\n\n private getSession = (): Session | null => {\n const session = this.store.getState().session;\n\n if (!session) {\n return null;\n }\n\n /**\n * TODO: this isn't really good enough\n * A user's session could be about to expire and we would still return it\n *\n * Instead we should check if a session is about to expire and refresh it\n * We should revisit this later\n */\n if (session.expirationDateMs < Date.now()) {\n this.clearSession();\n return null;\n }\n\n this.registerSessionExpirationHandler(session);\n\n return session;\n };\n\n private setSession = (\n session_:\n | Omit<Extract<Session, { type: \"email\" | \"oauth\" }>, \"expirationDateMs\">\n | Omit<Extract<Session, { type: \"passkey\" }>, \"expirationDateMs\">\n ) => {\n const session = {\n ...session_,\n expirationDateMs: Date.now() + this.expirationTimeMs,\n };\n\n this.registerSessionExpirationHandler(session);\n\n this.store.setState({ session });\n };\n\n public initialize() {\n this.getSessionUser()\n .then((user) => {\n // once we complete auth we can update the state of the session to connected or disconnected\n if (user) this.eventEmitter.emit(\"connected\", this.getSession()!);\n else this.eventEmitter.emit(\"disconnected\");\n })\n .finally(() => {\n this.eventEmitter.emit(\"initialized\");\n });\n }\n\n private getInitialState(): SessionState {\n return {\n session: null,\n };\n }\n\n private registerEventListeners = () => {\n this.store.subscribe(\n ({ session }) => session,\n (session, prevSession) => {\n if (session != null && prevSession == null) {\n this.eventEmitter.emit(\"connected\", session);\n } else if (session == null && prevSession != null) {\n this.eventEmitter.emit(\"disconnected\");\n }\n }\n );\n\n this.client.on(\"disconnected\", () => this.clearSession());\n\n this.client.on(\"connectedEmail\", (user, bundle) =>\n this.setSessionWithUserAndBundle({ type: \"email\", user, bundle })\n );\n\n this.client.on(\"connectedPasskey\", (user) => {\n const existingSession = this.getSession();\n if (\n existingSession != null &&\n existingSession.type === \"passkey\" &&\n existingSession.user.userId === user.userId\n ) {\n return;\n }\n\n this.setSession({ type: \"passkey\", user });\n });\n\n this.client.on(\"connectedOauth\", (user, bundle) =>\n this.setSessionWithUserAndBundle({ type: \"oauth\", user, bundle })\n );\n\n // sync local state if persisted state has changed from another tab\n // only do this in the browser\n // Add a try catch to prevent potential crashes in non-browser environments\n try {\n if (\n typeof window !== \"undefined\" &&\n typeof window.addEventListener !== \"undefined\"\n ) {\n window.addEventListener(\"focus\", () => {\n const oldSession = this.store.getState().session;\n this.store.persist.rehydrate();\n const newSession = this.store.getState().session;\n if (\n (oldSession?.expirationDateMs ?? 0) < Date.now() ||\n oldSession?.user.orgId !== newSession?.user.orgId ||\n oldSession?.user.userId !== newSession?.user.userId\n ) {\n // Initialize if the user has changed.\n this.initialize();\n }\n });\n }\n } catch (e) {\n console.error(\"Error registering event listeners\", e);\n }\n };\n\n private registerSessionExpirationHandler = (session: Session) => {\n if (this.clearSessionHandle) {\n clearTimeout(this.clearSessionHandle);\n }\n\n this.clearSessionHandle = setTimeout(() => {\n this.clearSession();\n }, Math.min(session.expirationDateMs - Date.now(), Math.pow(2, 31) - 1));\n };\n\n private setSessionWithUserAndBundle = ({\n type,\n user,\n bundle,\n }: {\n type: \"email\" | \"oauth\";\n user: User;\n bundle: string;\n }) => {\n const existingSession = this.getSession();\n if (\n existingSession != null &&\n existingSession.type === type &&\n existingSession.user.userId === user.userId &&\n // if the bundle is different, then we've refreshed the session\n // so we need to reset the session\n existingSession.bundle === bundle\n ) {\n return;\n }\n\n this.setSession({ type, user, bundle });\n };\n}\n"]}
1
+ {"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../src/session/manager.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EACL,iBAAiB,EACjB,OAAO,EACP,qBAAqB,GACtB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,WAAW,EAA8B,MAAM,iBAAiB,CAAC;AAO1E,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAGzD,MAAM,CAAC,MAAM,kBAAkB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,aAAa;AAE/D,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,CAAC,MAAM,CAAC;IACjD,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,wBAAwB,CAAC;IACxD,OAAO,EAAE,CAAC;SACP,IAAI,CAAC,CAAC,cAAc,EAAE,gBAAgB,CAAC,CAAC;SACxC,OAAO,CAAC,cAAc,CAAC;SACvB,EAAE,CAAC,CAAC,CAAC,MAAM,EAAW,CAAC;IAC1B,gBAAgB,EAAE,CAAC;SAChB,MAAM,EAAE;SACR,OAAO,CAAC,kBAAkB,CAAC;SAC3B,QAAQ,CACP,2FAA2F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CAAC;QACzC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,YAAY,GAAG,IAAI,YAAY,EAAwB,CAAC;QAE7D,IAAI,CAAC,KAAK,GAAG,WAAW,CACtB,qBAAqB,CACnB,OAAO,CAAC,IAAI,CAAC,eAAe,EAAE;YAC5B,IAAI,EAAE,IAAI,CAAC,UAAU;YACrB,OAAO,EAAE,iBAAiB,CAAe,GAAG,EAAE,CAAC,OAAO,CAAC;SACxD,CAAC,CACH,CACF,CAAC;QAEF,IAAI,CAAC,sBAAsB,EAAE,CAAC;IAChC,CAAC;IAuIM,UAAU;QACf,IAAI,CAAC,cAAc,EAAE;aAClB,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE;YACb,4FAA4F;YAC5F,IAAI,IAAI;gBAAE,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,UAAU,EAAG,CAAC,CAAC;;gBAC7D,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QAC9C,CAAC,CAAC;aACD,OAAO,CAAC,GAAG,EAAE;YACZ,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;QACxC,CAAC,CAAC,CAAC;IACP,CAAC;IAEO,eAAe;QACrB,OAAO;YACL,OAAO,EAAE,IAAI;SACd,CAAC;IACJ,CAAC;CAkHF","sourcesContent":["import EventEmitter from \"eventemitter3\";\nimport { z } from \"zod\";\nimport {\n createJSONStorage,\n persist,\n subscribeWithSelector,\n} from \"zustand/middleware\";\nimport { createStore, type Mutate, type StoreApi } from \"zustand/vanilla\";\nimport type { BaseSignerClient } from \"../client/base\";\nimport type {\n AlchemySignerClientEvent,\n AlchemySignerClientEvents,\n User,\n} from \"../client/types\";\nimport { assertNever } from \"../utils/typeAssertions.js\";\nimport type { Session, SessionManagerEvents } from \"./types\";\n\nexport const DEFAULT_SESSION_MS = 15 * 60 * 1000; // 15 minutes\n\nexport const SessionManagerParamsSchema = z.object({\n sessionKey: z.string().default(\"alchemy-signer-session\"),\n storage: z\n .enum([\"localStorage\", \"sessionStorage\"])\n .default(\"localStorage\")\n .or(z.custom<Storage>()),\n expirationTimeMs: z\n .number()\n .default(DEFAULT_SESSION_MS)\n .describe(\n \"The time in milliseconds that a session should last before expiring [default: 15 minutes]\"\n ),\n client: z.custom<BaseSignerClient>(),\n});\n\nexport type SessionManagerParams = z.input<typeof SessionManagerParamsSchema>;\n\ntype SessionState = {\n session: Session | null;\n};\n\ntype Store = Mutate<\n StoreApi<SessionState>,\n [[\"zustand/subscribeWithSelector\", never], [\"zustand/persist\", SessionState]]\n>;\n\ntype TemporarySession = { orgId: string; isNewUser?: boolean };\n\nexport class SessionManager {\n private sessionKey: string;\n private client: BaseSignerClient;\n private eventEmitter: EventEmitter<SessionManagerEvents>;\n readonly expirationTimeMs: number;\n private store: Store;\n private clearSessionHandle: NodeJS.Timeout | null = null;\n\n constructor(params: SessionManagerParams) {\n const {\n sessionKey,\n storage: storageType,\n expirationTimeMs,\n client,\n } = SessionManagerParamsSchema.parse(params);\n this.sessionKey = sessionKey;\n const storage =\n typeof storageType === \"string\"\n ? storageType === \"localStorage\"\n ? localStorage\n : sessionStorage\n : storageType;\n this.expirationTimeMs = expirationTimeMs;\n this.client = client;\n this.eventEmitter = new EventEmitter<SessionManagerEvents>();\n\n this.store = createStore(\n subscribeWithSelector(\n persist(this.getInitialState, {\n name: this.sessionKey,\n storage: createJSONStorage<SessionState>(() => storage),\n })\n )\n );\n\n this.registerEventListeners();\n }\n\n public getSessionUser = async (): Promise<User | null> => {\n const existingSession = this.getSession();\n if (existingSession == null) {\n return null;\n }\n\n switch (existingSession.type) {\n case \"email\":\n case \"oauth\":\n case \"otp\": {\n const connectedEventName = (() => {\n switch (existingSession.type) {\n case \"email\":\n return \"connectedEmail\";\n case \"oauth\":\n return \"connectedOauth\";\n case \"otp\":\n return \"connectedOtp\";\n }\n })();\n const result = await this.client\n .completeAuthWithBundle({\n bundle: existingSession.bundle,\n orgId: existingSession.user.orgId,\n authenticatingType: existingSession.type,\n connectedEventName,\n idToken: existingSession.user.idToken,\n })\n .catch((e) => {\n console.warn(\"Failed to load user from session\", e);\n return null;\n });\n\n if (!result) {\n this.clearSession();\n return null;\n }\n\n return result;\n }\n case \"passkey\": {\n // we don't need to do much here if we already have a user\n // this will setup the client with the user context, but\n // requests still have to be signed by the user on first request\n // so this is fine\n return this.client.lookupUserWithPasskey(existingSession.user);\n }\n default:\n assertNever(\n existingSession,\n `Unknown session type: ${(existingSession as any).type}`\n );\n }\n };\n\n public clearSession = () => {\n this.store.setState({ session: null });\n\n if (this.clearSessionHandle) {\n clearTimeout(this.clearSessionHandle);\n }\n };\n\n public setTemporarySession = (session: TemporarySession) => {\n // temporary session must be placed in localStorage so that it can be accessed across tabs\n localStorage.setItem(\n `${this.sessionKey}:temporary`,\n JSON.stringify(session)\n );\n };\n\n public getTemporarySession = (): TemporarySession | null => {\n // temporary session must be placed in localStorage so that it can be accessed across tabs\n const sessionStr = localStorage.getItem(`${this.sessionKey}:temporary`);\n\n if (!sessionStr) {\n return null;\n }\n\n return JSON.parse(sessionStr);\n };\n\n on = <E extends keyof SessionManagerEvents>(\n event: E,\n listener: SessionManagerEvents[E]\n ) => {\n this.eventEmitter.on(event, listener as any);\n\n return () => this.eventEmitter.removeListener(event, listener as any);\n };\n\n private getSession = (): Session | null => {\n const session = this.store.getState().session;\n\n if (!session) {\n return null;\n }\n\n /**\n * TODO: this isn't really good enough\n * A user's session could be about to expire and we would still return it\n *\n * Instead we should check if a session is about to expire and refresh it\n * We should revisit this later\n */\n if (session.expirationDateMs < Date.now()) {\n this.clearSession();\n return null;\n }\n\n this.registerSessionExpirationHandler(session);\n\n return session;\n };\n\n private setSession = (\n session_:\n | Omit<\n Extract<Session, { type: \"email\" | \"oauth\" | \"otp\" }>,\n \"expirationDateMs\"\n >\n | Omit<Extract<Session, { type: \"passkey\" }>, \"expirationDateMs\">\n ) => {\n const session = {\n ...session_,\n expirationDateMs: Date.now() + this.expirationTimeMs,\n };\n\n this.registerSessionExpirationHandler(session);\n\n this.store.setState({ session });\n };\n\n public initialize() {\n this.getSessionUser()\n .then((user) => {\n // once we complete auth we can update the state of the session to connected or disconnected\n if (user) this.eventEmitter.emit(\"connected\", this.getSession()!);\n else this.eventEmitter.emit(\"disconnected\");\n })\n .finally(() => {\n this.eventEmitter.emit(\"initialized\");\n });\n }\n\n private getInitialState(): SessionState {\n return {\n session: null,\n };\n }\n\n private registerEventListeners = () => {\n this.store.subscribe(\n ({ session }) => session,\n (session, prevSession) => {\n if (session != null && prevSession == null) {\n this.eventEmitter.emit(\"connected\", session);\n } else if (session == null && prevSession != null) {\n this.eventEmitter.emit(\"disconnected\");\n }\n }\n );\n\n // Helper type to ensure that a listener is either defined or explicitly\n // omitted for every event type.\n type Listeners = {\n [K in keyof AlchemySignerClientEvents]:\n | AlchemySignerClientEvents[K]\n | undefined;\n };\n\n const listeners: Listeners = {\n connected: undefined,\n newUserSignup: undefined,\n authenticating: undefined,\n connectedEmail: (user, bundle) =>\n this.setSessionWithUserAndBundle({ type: \"email\", user, bundle }),\n connectedPasskey: (user) => {\n const existingSession = this.getSession();\n if (\n existingSession != null &&\n existingSession.type === \"passkey\" &&\n existingSession.user.userId === user.userId\n ) {\n return;\n }\n\n this.setSession({ type: \"passkey\", user });\n },\n connectedOauth: (user, bundle) =>\n this.setSessionWithUserAndBundle({ type: \"oauth\", user, bundle }),\n connectedOtp: (user, bundle) => {\n this.setSessionWithUserAndBundle({ type: \"otp\", user, bundle });\n },\n disconnected: () => this.clearSession(),\n };\n\n for (const [event, listener] of Object.entries(listeners)) {\n if (listener) {\n this.client.on(event as AlchemySignerClientEvent, listener);\n }\n }\n\n // sync local state if persisted state has changed from another tab\n // only do this in the browser\n // Add a try catch to prevent potential crashes in non-browser environments\n try {\n if (\n typeof window !== \"undefined\" &&\n typeof window.addEventListener !== \"undefined\"\n ) {\n window.addEventListener(\"focus\", () => {\n const oldSession = this.store.getState().session;\n this.store.persist.rehydrate();\n const newSession = this.store.getState().session;\n if (\n (oldSession?.expirationDateMs ?? 0) < Date.now() ||\n oldSession?.user.orgId !== newSession?.user.orgId ||\n oldSession?.user.userId !== newSession?.user.userId\n ) {\n // Initialize if the user has changed.\n this.initialize();\n }\n });\n }\n } catch (e) {\n console.error(\"Error registering event listeners\", e);\n }\n };\n\n private registerSessionExpirationHandler = (session: Session) => {\n if (this.clearSessionHandle) {\n clearTimeout(this.clearSessionHandle);\n }\n\n this.clearSessionHandle = setTimeout(() => {\n this.clearSession();\n }, Math.min(session.expirationDateMs - Date.now(), Math.pow(2, 31) - 1));\n };\n\n private setSessionWithUserAndBundle = ({\n type,\n user,\n bundle,\n }: {\n type: \"email\" | \"oauth\" | \"otp\";\n user: User;\n bundle: string;\n }) => {\n const existingSession = this.getSession();\n if (\n existingSession != null &&\n existingSession.type === type &&\n existingSession.user.userId === user.userId &&\n // if the bundle is different, then we've refreshed the session\n // so we need to reset the session\n existingSession.bundle === bundle\n ) {\n return;\n }\n\n this.setSession({ type, user, bundle });\n };\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  import type { User } from "../client/types";
2
2
  export type Session = {
3
- type: "email" | "oauth";
3
+ type: "email" | "oauth" | "otp";
4
4
  bundle: string;
5
5
  expirationDateMs: number;
6
6
  user: User;
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/session/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { User } from \"../client/types\";\n\nexport type Session =\n | {\n type: \"email\" | \"oauth\";\n bundle: string;\n expirationDateMs: number;\n user: User;\n }\n | { type: \"passkey\"; user: User; expirationDateMs: number };\n\nexport type SessionManagerEvents = {\n connected(session: Session): void;\n disconnected(): void;\n initialized(): void;\n};\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/session/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { User } from \"../client/types\";\n\nexport type Session =\n | {\n type: \"email\" | \"oauth\" | \"otp\";\n bundle: string;\n expirationDateMs: number;\n user: User;\n }\n | { type: \"passkey\"; user: User; expirationDateMs: number };\n\nexport type SessionManagerEvents = {\n connected(session: Session): void;\n disconnected(): void;\n initialized(): void;\n};\n"]}
@@ -5,6 +5,7 @@ import type { CredentialCreationOptionOverrides } from "./client/types.js";
5
5
  export type AuthParams = {
6
6
  type: "email";
7
7
  email: string;
8
+ emailMode?: "magicLink" | "otp";
8
9
  redirectParams?: URLSearchParams;
9
10
  } | {
10
11
  type: "email";
@@ -33,6 +34,9 @@ export type AuthParams = {
33
34
  orgId: string;
34
35
  idToken: string;
35
36
  isNewUser?: boolean;
37
+ } | {
38
+ type: "otp";
39
+ otpCode: string;
36
40
  };
37
41
  export type OauthProviderConfig = {
38
42
  authProviderId: "auth0";