@turnkey/react-wallet-kit 1.0.0 → 1.1.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 (178) hide show
  1. package/dist/_virtual/_rollupPluginBabelHelpers.mjs +95 -256
  2. package/dist/_virtual/_rollupPluginBabelHelpers.mjs.map +1 -1
  3. package/dist/components/auth/Action.js.map +1 -1
  4. package/dist/components/auth/Action.mjs +18 -18
  5. package/dist/components/auth/Action.mjs.map +1 -1
  6. package/dist/components/auth/Email.js.map +1 -1
  7. package/dist/components/auth/Email.mjs +13 -14
  8. package/dist/components/auth/Email.mjs.map +1 -1
  9. package/dist/components/auth/OAuth.js.map +1 -1
  10. package/dist/components/auth/OAuth.mjs.map +1 -1
  11. package/dist/components/auth/OTP.d.ts +2 -2
  12. package/dist/components/auth/OTP.d.ts.map +1 -1
  13. package/dist/components/auth/OTP.js +1 -1
  14. package/dist/components/auth/OTP.js.map +1 -1
  15. package/dist/components/auth/OTP.mjs +46 -48
  16. package/dist/components/auth/OTP.mjs.map +1 -1
  17. package/dist/components/auth/OrSeparator.js.map +1 -1
  18. package/dist/components/auth/OrSeparator.mjs.map +1 -1
  19. package/dist/components/auth/Passkey.js.map +1 -1
  20. package/dist/components/auth/Passkey.mjs.map +1 -1
  21. package/dist/components/auth/Phone.js.map +1 -1
  22. package/dist/components/auth/Phone.mjs +13 -14
  23. package/dist/components/auth/Phone.mjs.map +1 -1
  24. package/dist/components/auth/Wallet.d.ts +2 -2
  25. package/dist/components/auth/Wallet.d.ts.map +1 -1
  26. package/dist/components/auth/Wallet.js +89 -49
  27. package/dist/components/auth/Wallet.js.map +1 -1
  28. package/dist/components/auth/Wallet.mjs +154 -116
  29. package/dist/components/auth/Wallet.mjs.map +1 -1
  30. package/dist/components/auth/index.d.ts +4 -1
  31. package/dist/components/auth/index.d.ts.map +1 -1
  32. package/dist/components/auth/index.js +44 -7
  33. package/dist/components/auth/index.js.map +1 -1
  34. package/dist/components/auth/index.mjs +160 -138
  35. package/dist/components/auth/index.mjs.map +1 -1
  36. package/dist/components/design/Buttons.js.map +1 -1
  37. package/dist/components/design/Buttons.mjs.map +1 -1
  38. package/dist/components/design/Failure.js.map +1 -1
  39. package/dist/components/design/Failure.mjs.map +1 -1
  40. package/dist/components/design/Inputs.js.map +1 -1
  41. package/dist/components/design/Inputs.mjs.map +1 -1
  42. package/dist/components/design/Lottie/Dynamic.mjs +8 -11
  43. package/dist/components/design/Lottie/Dynamic.mjs.map +1 -1
  44. package/dist/components/design/Lottie/Wrapper.js.map +1 -1
  45. package/dist/components/design/Lottie/Wrapper.mjs +9 -10
  46. package/dist/components/design/Lottie/Wrapper.mjs.map +1 -1
  47. package/dist/components/design/Spinners.js.map +1 -1
  48. package/dist/components/design/Spinners.mjs.map +1 -1
  49. package/dist/components/design/Success.js.map +1 -1
  50. package/dist/components/design/Success.mjs.map +1 -1
  51. package/dist/components/design/Svg.js.map +1 -1
  52. package/dist/components/design/Svg.mjs.map +1 -1
  53. package/dist/components/export/Export.d.ts +3 -0
  54. package/dist/components/export/Export.d.ts.map +1 -1
  55. package/dist/components/export/Export.js +12 -4
  56. package/dist/components/export/Export.js.map +1 -1
  57. package/dist/components/export/Export.mjs +35 -29
  58. package/dist/components/export/Export.mjs.map +1 -1
  59. package/dist/components/export/ExportWarning.d.ts +2 -0
  60. package/dist/components/export/ExportWarning.d.ts.map +1 -1
  61. package/dist/components/export/ExportWarning.js +17 -9
  62. package/dist/components/export/ExportWarning.js.map +1 -1
  63. package/dist/components/export/ExportWarning.mjs +64 -63
  64. package/dist/components/export/ExportWarning.mjs.map +1 -1
  65. package/dist/components/import/Import.d.ts +3 -0
  66. package/dist/components/import/Import.d.ts.map +1 -1
  67. package/dist/components/import/Import.js +28 -15
  68. package/dist/components/import/Import.js.map +1 -1
  69. package/dist/components/import/Import.mjs +122 -112
  70. package/dist/components/import/Import.mjs.map +1 -1
  71. package/dist/components/sign/Message.d.ts +1 -0
  72. package/dist/components/sign/Message.d.ts.map +1 -1
  73. package/dist/components/sign/Message.js +4 -0
  74. package/dist/components/sign/Message.js.map +1 -1
  75. package/dist/components/sign/Message.mjs +24 -22
  76. package/dist/components/sign/Message.mjs.map +1 -1
  77. package/dist/components/user/ConnectWallet.d.ts +2 -0
  78. package/dist/components/user/ConnectWallet.d.ts.map +1 -1
  79. package/dist/components/user/ConnectWallet.js +41 -29
  80. package/dist/components/user/ConnectWallet.js.map +1 -1
  81. package/dist/components/user/ConnectWallet.mjs +48 -32
  82. package/dist/components/user/ConnectWallet.mjs.map +1 -1
  83. package/dist/components/user/RemoveEmail.d.ts +1 -0
  84. package/dist/components/user/RemoveEmail.d.ts.map +1 -1
  85. package/dist/components/user/RemoveEmail.js +7 -3
  86. package/dist/components/user/RemoveEmail.js.map +1 -1
  87. package/dist/components/user/RemoveEmail.mjs +27 -25
  88. package/dist/components/user/RemoveEmail.mjs.map +1 -1
  89. package/dist/components/user/RemoveOAuthProvider.d.ts +1 -0
  90. package/dist/components/user/RemoveOAuthProvider.d.ts.map +1 -1
  91. package/dist/components/user/RemoveOAuthProvider.js +7 -3
  92. package/dist/components/user/RemoveOAuthProvider.js.map +1 -1
  93. package/dist/components/user/RemoveOAuthProvider.mjs +27 -25
  94. package/dist/components/user/RemoveOAuthProvider.mjs.map +1 -1
  95. package/dist/components/user/RemovePasskey.d.ts +1 -0
  96. package/dist/components/user/RemovePasskey.d.ts.map +1 -1
  97. package/dist/components/user/RemovePasskey.js +6 -2
  98. package/dist/components/user/RemovePasskey.js.map +1 -1
  99. package/dist/components/user/RemovePasskey.mjs +26 -24
  100. package/dist/components/user/RemovePasskey.mjs.map +1 -1
  101. package/dist/components/user/RemovePhoneNumber.d.ts +1 -0
  102. package/dist/components/user/RemovePhoneNumber.d.ts.map +1 -1
  103. package/dist/components/user/RemovePhoneNumber.js +7 -3
  104. package/dist/components/user/RemovePhoneNumber.js.map +1 -1
  105. package/dist/components/user/RemovePhoneNumber.mjs +27 -25
  106. package/dist/components/user/RemovePhoneNumber.mjs.map +1 -1
  107. package/dist/components/user/UpdateEmail.d.ts +4 -0
  108. package/dist/components/user/UpdateEmail.d.ts.map +1 -1
  109. package/dist/components/user/UpdateEmail.js +13 -3
  110. package/dist/components/user/UpdateEmail.js.map +1 -1
  111. package/dist/components/user/UpdateEmail.mjs +46 -39
  112. package/dist/components/user/UpdateEmail.mjs.map +1 -1
  113. package/dist/components/user/UpdatePhoneNumber.d.ts +4 -0
  114. package/dist/components/user/UpdatePhoneNumber.d.ts.map +1 -1
  115. package/dist/components/user/UpdatePhoneNumber.js +13 -3
  116. package/dist/components/user/UpdatePhoneNumber.js.map +1 -1
  117. package/dist/components/user/UpdatePhoneNumber.mjs +46 -39
  118. package/dist/components/user/UpdatePhoneNumber.mjs.map +1 -1
  119. package/dist/components/user/UpdateUserName.d.ts +2 -0
  120. package/dist/components/user/UpdateUserName.d.ts.map +1 -1
  121. package/dist/components/user/UpdateUserName.js +4 -3
  122. package/dist/components/user/UpdateUserName.js.map +1 -1
  123. package/dist/components/user/UpdateUserName.mjs +25 -25
  124. package/dist/components/user/UpdateUserName.mjs.map +1 -1
  125. package/dist/index.d.ts +2 -1
  126. package/dist/index.d.ts.map +1 -1
  127. package/dist/index.js +16 -3
  128. package/dist/index.js.map +1 -1
  129. package/dist/index.mjs +2 -1
  130. package/dist/index.mjs.map +1 -1
  131. package/dist/providers/TurnkeyProvider.js.map +1 -1
  132. package/dist/providers/TurnkeyProvider.mjs.map +1 -1
  133. package/dist/providers/client/Hook.js.map +1 -1
  134. package/dist/providers/client/Hook.mjs.map +1 -1
  135. package/dist/providers/client/Provider.d.ts.map +1 -1
  136. package/dist/providers/client/Provider.js +630 -300
  137. package/dist/providers/client/Provider.js.map +1 -1
  138. package/dist/providers/client/Provider.mjs +2489 -2103
  139. package/dist/providers/client/Provider.mjs.map +1 -1
  140. package/dist/providers/client/Types.d.ts +100 -171
  141. package/dist/providers/client/Types.d.ts.map +1 -1
  142. package/dist/providers/client/Types.js.map +1 -1
  143. package/dist/providers/client/Types.mjs.map +1 -1
  144. package/dist/providers/index.d.ts +1 -0
  145. package/dist/providers/index.d.ts.map +1 -1
  146. package/dist/providers/modal/Hook.js.map +1 -1
  147. package/dist/providers/modal/Hook.mjs.map +1 -1
  148. package/dist/providers/modal/Provider.d.ts +3 -0
  149. package/dist/providers/modal/Provider.d.ts.map +1 -1
  150. package/dist/providers/modal/Provider.js +10 -1
  151. package/dist/providers/modal/Provider.js.map +1 -1
  152. package/dist/providers/modal/Provider.mjs +15 -1
  153. package/dist/providers/modal/Provider.mjs.map +1 -1
  154. package/dist/providers/modal/Root.d.ts.map +1 -1
  155. package/dist/providers/modal/Root.js +74 -2
  156. package/dist/providers/modal/Root.js.map +1 -1
  157. package/dist/providers/modal/Root.mjs +92 -15
  158. package/dist/providers/modal/Root.mjs.map +1 -1
  159. package/dist/providers/theme/Overrides.js.map +1 -1
  160. package/dist/providers/theme/Overrides.mjs.map +1 -1
  161. package/dist/styles.css +1 -1
  162. package/dist/typedoc-entry/index.d.ts +4 -0
  163. package/dist/typedoc-entry/index.d.ts.map +1 -0
  164. package/dist/types/base.d.ts +8 -1
  165. package/dist/types/base.d.ts.map +1 -1
  166. package/dist/types/base.js.map +1 -1
  167. package/dist/types/base.mjs.map +1 -1
  168. package/dist/types/method-types.d.ts +210 -0
  169. package/dist/types/method-types.d.ts.map +1 -0
  170. package/dist/utils/timers.js.map +1 -1
  171. package/dist/utils/timers.mjs.map +1 -1
  172. package/dist/utils/utils.d.ts +1 -1
  173. package/dist/utils/utils.d.ts.map +1 -1
  174. package/dist/utils/utils.js +15 -5
  175. package/dist/utils/utils.js.map +1 -1
  176. package/dist/utils/utils.mjs +92 -86
  177. package/dist/utils/utils.mjs.map +1 -1
  178. package/package.json +4 -4
@@ -362,11 +362,29 @@ const ClientProvider = ({
362
362
  const oauthOrder = config.auth?.oauthOrder ?? ["google", "apple", "x", "discord", "facebook"].filter(provider => resolvedMethods[`${provider}OauthEnabled`]);
363
363
  // Set a default ordering for the overall auth methods
364
364
  const methodOrder = config.auth?.methodOrder ?? [oauthOrder.length > 0 ? "socials" : null, resolvedMethods.emailOtpAuthEnabled ? "email" : null, resolvedMethods.smsOtpAuthEnabled ? "sms" : null, resolvedMethods.passkeyAuthEnabled ? "passkey" : null, resolvedMethods.walletAuthEnabled ? "wallet" : null].filter(Boolean);
365
+ // Warn if they are trying to set auth proxy only settings directly
366
+ if (config.auth?.sessionExpirationSeconds) {
367
+ console.warn("Turnkey SDK warning. You have set sessionExpirationSeconds directly in the TurnkeyProvider. This setting will be ignored because you are using an auth proxy. Please configure session expiration in the Turnkey dashboard.");
368
+ }
369
+ if (config.auth?.otpAlphanumeric !== undefined) {
370
+ console.warn("Turnkey SDK warning. You have set otpAlphanumeric directly in the TurnkeyProvider. This setting will be ignored because you are using an auth proxy. Please configure OTP settings in the Turnkey dashboard.");
371
+ }
372
+ if (config.auth?.otpLength) {
373
+ console.warn("Turnkey SDK warning. You have set otpLength directly in the TurnkeyProvider. This setting will be ignored because you are using an auth proxy. Please configure OTP settings in the Turnkey dashboard.");
374
+ }
375
+ // These are settings that can only be set via the auth proxy config
376
+ const authProxyOnlySettings = {
377
+ sessionExpirationSeconds: proxyAuthConfig?.sessionExpirationSeconds,
378
+ otpAlphanumeric: proxyAuthConfig?.otpAlphanumeric ?? true,
379
+ // This fallback will never be hit. This is purely for the tests to pass before mono is released
380
+ otpLength: proxyAuthConfig?.otpLength ?? "6" // This fallback will never be hit. This is purely for the tests to pass before mono is released
381
+ };
365
382
  return {
366
383
  ...config,
367
384
  // Overrides:
368
385
  auth: {
369
386
  ...config.auth,
387
+ ...authProxyOnlySettings,
370
388
  methods: resolvedMethods,
371
389
  oauthConfig: {
372
390
  ...config.auth?.oauthConfig,
@@ -376,7 +394,6 @@ const ClientProvider = ({
376
394
  // (e.g. Safari) block popups
377
395
  openOauthInPage: isMobile ? true : config.auth?.oauthConfig?.openOauthInPage
378
396
  },
379
- sessionExpirationSeconds: proxyAuthConfig?.sessionExpirationSeconds,
380
397
  methodOrder,
381
398
  oauthOrder,
382
399
  autoRefreshSession: config.auth?.autoRefreshSession ?? true
@@ -508,36 +525,47 @@ const ClientProvider = ({
508
525
  * @internal
509
526
  * Attach listeners for connected wallet providers so we can refresh state on changes.
510
527
  *
511
- * - Ethereum: listens for disconnect and chain switches to trigger a refresh.
528
+ * - Ethereum: listens for disconnect and chain/account changes to trigger a refresh.
512
529
  * - Solana: listens for disconnect via Wallet Standard `change` events to trigger a refresh.
513
- * - WalletConnect: listens for disconnect via our custom wrapper’s `change` event to trigger a refresh.
530
+ * - WalletConnect:
531
+ * - listens for disconnect and other state changes via its unified `change` event
532
+ * - additionally listens for `pairingExpired` events (proposal expiration), in which
533
+ * case we re-fetch providers to refresh the pairing URI for the UI.
514
534
  *
515
535
  * Notes:
516
- * - Only providers that are connected are bound.
517
- * - WalletConnect is excluded from the “native” paths and handled via its unified `change` event.
536
+ * - Only connected providers are bound, except WalletConnect which must always register
537
+ * to handle proposal expiration even if no active session exists.
538
+ * - Since all WalletConnect providers share the same session, we only attach to one.
518
539
  *
519
540
  * @param walletProviders - Discovered providers; only connected ones are bound.
520
- * @param onWalletsChanged - Invoked when a relevant provider event occurs.
541
+ * @param onUpdateState - Invoked when a relevant provider event occurs.
521
542
  * @returns Cleanup function that removes all listeners registered by this call.
522
543
  */
523
- async function initializeWalletProviderListeners(walletProviders, onWalletsChanged) {
544
+ async function initializeWalletProviderListeners(walletProviders, onUpdateState) {
524
545
  if (walletProviders.length === 0) return () => {};
525
546
  const cleanups = [];
526
547
  // we only want to initialize these listeners for connected walletProviders
527
548
  const nativeOnly = provider => provider.interfaceType !== core.WalletInterfaceType.WalletConnect;
528
549
  const ethProviders = masterConfig?.walletConfig?.chains.ethereum?.native ? walletProviders.filter(provider => provider.chainInfo.namespace === core.Chain.Ethereum && nativeOnly(provider) && provider.connectedAddresses.length > 0) : [];
529
550
  const solProviders = masterConfig?.walletConfig?.chains.solana?.native ? walletProviders.filter(provider => provider.chainInfo.namespace === core.Chain.Solana && nativeOnly(provider) && provider.connectedAddresses.length > 0) : [];
530
- // we exclude WalletConnect from the native event wiring
531
- // this is because WC is handled separately with a custom wrapper’s
532
- // `change` event
533
- const wcProviders = walletProviders.filter(p => p.interfaceType === core.WalletInterfaceType.WalletConnect && p.connectedAddresses.length > 0);
534
- function attachEthereumListeners(provider, onWalletsChanged) {
551
+ // WalletConnect is excluded from native event wiring. Instead,
552
+ // it uses a unified `change` event exposed by our custom wrapper
553
+ //
554
+ // unlike native providers, we register listeners for WalletConnect
555
+ // even if it’s not currently “connected”. This is required so we
556
+ // can detect proposal expiration events and display the new regenerated
557
+ // URI for the UI
558
+ const wcProviders = walletProviders.filter(p => p.interfaceType === core.WalletInterfaceType.WalletConnect);
559
+ // since all WalletConnect providers share the same underlying session
560
+ // and emit identical events, we only attach listeners to a single provider
561
+ const wcProvider = wcProviders.find(p => p.interfaceType === core.WalletInterfaceType.WalletConnect);
562
+ function attachEthereumListeners(provider, onUpdateState) {
535
563
  if (typeof provider.on !== "function") return;
536
- const handleChainChanged = _chainId => onWalletsChanged();
537
- const handleAccountsChanged = accounts => {
538
- if (accounts.length === 0) onWalletsChanged();
564
+ const handleChainChanged = async _chainId => onUpdateState();
565
+ const handleAccountsChanged = async accounts => {
566
+ if (accounts.length === 0) onUpdateState();
539
567
  };
540
- const handleDisconnect = () => onWalletsChanged();
568
+ const handleDisconnect = () => onUpdateState();
541
569
  provider.on("chainChanged", handleChainChanged);
542
570
  provider.on("accountsChanged", handleAccountsChanged);
543
571
  provider.on("disconnect", handleDisconnect);
@@ -547,32 +575,43 @@ const ClientProvider = ({
547
575
  provider.removeListener("disconnect", handleDisconnect);
548
576
  };
549
577
  }
550
- function attachSolanaListeners(provider, onWalletsChanged) {
578
+ function attachSolanaListeners(provider, onUpdateState) {
551
579
  const cleanups = [];
552
580
  const walletEvents = provider?.features?.["standard:events"];
553
581
  if (walletEvents?.on) {
554
- const offChange = walletEvents.on("change", _evt => {
555
- onWalletsChanged();
582
+ const offChange = walletEvents.on("change", async _evt => {
583
+ await onUpdateState();
556
584
  });
557
585
  cleanups.push(offChange);
558
586
  }
559
587
  return () => cleanups.forEach(fn => fn());
560
588
  }
561
589
  ethProviders.forEach(p => {
562
- const cleanup = attachEthereumListeners(p.provider, onWalletsChanged);
590
+ const cleanup = attachEthereumListeners(p.provider, onUpdateState);
563
591
  if (cleanup) cleanups.push(cleanup);
564
592
  });
565
593
  solProviders.forEach(p => {
566
- const cleanup = attachSolanaListeners(p.provider, onWalletsChanged);
594
+ const cleanup = attachSolanaListeners(p.provider, onUpdateState);
567
595
  if (cleanup) cleanups.push(cleanup);
568
596
  });
569
- wcProviders.forEach(p => {
570
- const standardEvents = p.provider?.features?.["standard:events"];
597
+ if (wcProvider) {
598
+ const standardEvents = wcProvider.provider?.features?.["standard:events"];
571
599
  if (standardEvents?.on) {
572
- const unsubscribe = standardEvents.on("change", onWalletsChanged);
573
- cleanups.push(unsubscribe);
600
+ if (standardEvents?.on) {
601
+ const unsubscribe = standardEvents.on("change", async evt => {
602
+ // if the event is a proposalExpired, we want to re-fetch the providers
603
+ // to refresh the uri, there is no need to refresh the wallets state
604
+ if (evt?.type === "pairingExpired") {
605
+ debouncedFetchWalletProviders();
606
+ }
607
+ // any other event (disconnect, chain switch, accounts changed)
608
+ // we refresh the wallets state
609
+ await onUpdateState();
610
+ });
611
+ cleanups.push(unsubscribe);
612
+ }
574
613
  }
575
- });
614
+ }
576
615
  return () => {
577
616
  cleanups.forEach(remove => remove());
578
617
  };
@@ -782,7 +821,7 @@ const ClientProvider = ({
782
821
  }
783
822
  return utils.withTurnkeyErrorHandling(() => client.createPasskey({
784
823
  ...params
785
- }), callbacks, "Failed to create passkey");
824
+ }), () => logout(), callbacks, "Failed to create passkey");
786
825
  }, [client, callbacks]);
787
826
  const logout = react.useCallback(async params => {
788
827
  if (!client) {
@@ -795,7 +834,7 @@ const ClientProvider = ({
795
834
  await client.logout(params);
796
835
  // We only handle post logout if the sessionKey is defined since that means we actually logged out of a session.
797
836
  if (sessionKey) handlePostLogout(sessionKey);
798
- }, callbacks, "Failed to logout");
837
+ }, () => logout(), callbacks, "Failed to logout");
799
838
  return;
800
839
  }, [client, callbacks]);
801
840
  const loginWithPasskey = react.useCallback(async params => {
@@ -806,7 +845,7 @@ const ClientProvider = ({
806
845
  const res = await utils.withTurnkeyErrorHandling(() => client.loginWithPasskey({
807
846
  ...params,
808
847
  expirationSeconds
809
- }), callbacks, "Failed to login with passkey");
848
+ }), () => logout(), callbacks, "Failed to login with passkey");
810
849
  if (res) {
811
850
  await handlePostAuth({
812
851
  method: base.AuthMethod.Passkey,
@@ -845,7 +884,7 @@ const ClientProvider = ({
845
884
  ...params,
846
885
  passkeyDisplayName: passkeyName,
847
886
  expirationSeconds
848
- }), callbacks, "Failed to sign up with passkey");
887
+ }), () => logout(), callbacks, "Failed to sign up with passkey");
849
888
  if (res) {
850
889
  await handlePostAuth({
851
890
  method: base.AuthMethod.Passkey,
@@ -871,26 +910,34 @@ const ClientProvider = ({
871
910
  if (!client) {
872
911
  throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
873
912
  }
874
- await client.connectWalletAccount(walletProvider);
875
- // this will update our walletProvider state
876
- await refreshWallets();
913
+ const address = await client.connectWalletAccount(walletProvider);
914
+ let wallets;
915
+ const s = await getSession();
916
+ if (s) {
917
+ // this will update our walletProvider state
918
+ wallets = await refreshWallets();
919
+ } else {
920
+ wallets = await fetchWallets({
921
+ connectedOnly: true
922
+ });
923
+ }
924
+ // we narrow to only connected wallets
925
+ // because we know the account must come from one of them
926
+ const connectedWallets = wallets.filter(w => w.source === core.WalletSource.Connected);
927
+ // find the matching account
928
+ const matchedAccount = connectedWallets.flatMap(w => w.accounts).find(a => a.address === address);
929
+ if (!matchedAccount) {
930
+ throw new sdkTypes.TurnkeyError(`No connected wallet account found for address: ${address}`, sdkTypes.TurnkeyErrorCodes.NO_WALLET_FOUND);
931
+ }
932
+ return matchedAccount;
877
933
  }, [client, callbacks]);
878
934
  const disconnectWalletAccount = react.useCallback(async walletProvider => {
879
935
  if (!client) {
880
936
  throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
881
937
  }
882
938
  await client.disconnectWalletAccount(walletProvider);
883
- // we only refresh the wallets if:
884
- // 1. there is an active session. This is needed because for WalletConnect
885
- // you can disconnect a wallet before actually being logged in
886
- //
887
- // 2. it was a WalletConnect provider that we just disconnected. Since
888
- // native providers emit a disconnect event which will already refresh
889
- // the wallets. This event is triggered in `initializeWalletProviderListeners()`
890
- if (session && walletProvider.interfaceType === core.WalletInterfaceType.WalletConnect) {
891
- // this will update our walletProvider state
892
- await refreshWallets();
893
- }
939
+ // no need here to call `refreshWallets()` because the provider emits a disconnect event which
940
+ // will trigger a refresh via the listener we set up in `initializeWalletProviderListeners()`
894
941
  }, [client, callbacks]);
895
942
  const switchWalletAccountChain = react.useCallback(async params => {
896
943
  if (!client) {
@@ -909,7 +956,7 @@ const ClientProvider = ({
909
956
  const res = await utils.withTurnkeyErrorHandling(() => client.loginWithWallet({
910
957
  ...params,
911
958
  expirationSeconds
912
- }), callbacks, "Failed to login with wallet");
959
+ }), () => logout(), callbacks, "Failed to login with wallet");
913
960
  if (res) {
914
961
  await handlePostAuth({
915
962
  method: base.AuthMethod.Wallet,
@@ -938,7 +985,7 @@ const ClientProvider = ({
938
985
  const res = await utils.withTurnkeyErrorHandling(() => client.signUpWithWallet({
939
986
  ...params,
940
987
  expirationSeconds
941
- }), callbacks, "Failed to sign up with wallet");
988
+ }), () => logout(), callbacks, "Failed to sign up with wallet");
942
989
  if (res) {
943
990
  await handlePostAuth({
944
991
  method: base.AuthMethod.Wallet,
@@ -967,7 +1014,7 @@ const ClientProvider = ({
967
1014
  const res = await utils.withTurnkeyErrorHandling(() => client.loginOrSignupWithWallet({
968
1015
  ...params,
969
1016
  expirationSeconds
970
- }), callbacks, "Failed to login or sign up with wallet");
1017
+ }), () => logout(), callbacks, "Failed to login or sign up with wallet");
971
1018
  if (res) {
972
1019
  await handlePostAuth({
973
1020
  method: base.AuthMethod.Wallet,
@@ -981,19 +1028,19 @@ const ClientProvider = ({
981
1028
  if (!client) {
982
1029
  throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
983
1030
  }
984
- return utils.withTurnkeyErrorHandling(() => client.initOtp(params), callbacks, "Failed to initialize OTP");
1031
+ return utils.withTurnkeyErrorHandling(() => client.initOtp(params), () => logout(), callbacks, "Failed to initialize OTP");
985
1032
  }, [client, callbacks]);
986
1033
  const verifyOtp = react.useCallback(async params => {
987
1034
  if (!client) {
988
1035
  throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
989
1036
  }
990
- return utils.withTurnkeyErrorHandling(() => client.verifyOtp(params), callbacks, "Failed to verify OTP");
1037
+ return utils.withTurnkeyErrorHandling(() => client.verifyOtp(params), () => logout(), callbacks, "Failed to verify OTP");
991
1038
  }, [client, callbacks]);
992
1039
  const loginWithOtp = react.useCallback(async params => {
993
1040
  if (!client) {
994
1041
  throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
995
1042
  }
996
- const res = await utils.withTurnkeyErrorHandling(() => client.loginWithOtp(params), callbacks, "Failed to login with OTP");
1043
+ const res = await utils.withTurnkeyErrorHandling(() => client.loginWithOtp(params), () => logout(), callbacks, "Failed to login with OTP");
997
1044
  if (res) {
998
1045
  await handlePostAuth({
999
1046
  method: base.AuthMethod.Otp,
@@ -1025,7 +1072,7 @@ const ClientProvider = ({
1025
1072
  } : {
1026
1073
  ...params
1027
1074
  };
1028
- const res = await utils.withTurnkeyErrorHandling(() => client.signUpWithOtp(params), callbacks, "Failed to sign up with OTP");
1075
+ const res = await utils.withTurnkeyErrorHandling(() => client.signUpWithOtp(params), () => logout(), callbacks, "Failed to sign up with OTP");
1029
1076
  if (res) {
1030
1077
  await handlePostAuth({
1031
1078
  method: base.AuthMethod.Otp,
@@ -1057,7 +1104,7 @@ const ClientProvider = ({
1057
1104
  } : {
1058
1105
  ...params
1059
1106
  };
1060
- const res = await utils.withTurnkeyErrorHandling(() => client.completeOtp(params), callbacks, "Failed to complete OTP");
1107
+ const res = await utils.withTurnkeyErrorHandling(() => client.completeOtp(params), () => logout(), callbacks, "Failed to complete OTP");
1061
1108
  if (res) {
1062
1109
  await handlePostAuth({
1063
1110
  method: base.AuthMethod.Otp,
@@ -1071,7 +1118,7 @@ const ClientProvider = ({
1071
1118
  if (!client) {
1072
1119
  throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1073
1120
  }
1074
- const res = await utils.withTurnkeyErrorHandling(() => client.loginWithOauth(params), callbacks, "Failed to login with OAuth");
1121
+ const res = await utils.withTurnkeyErrorHandling(() => client.loginWithOauth(params), () => logout(), callbacks, "Failed to login with OAuth");
1075
1122
  if (res) {
1076
1123
  await handlePostAuth({
1077
1124
  method: base.AuthMethod.Oauth,
@@ -1096,7 +1143,7 @@ const ClientProvider = ({
1096
1143
  } : {
1097
1144
  ...params
1098
1145
  };
1099
- const res = await utils.withTurnkeyErrorHandling(() => client.signUpWithOauth(params), callbacks, "Failed to sign up with OAuth");
1146
+ const res = await utils.withTurnkeyErrorHandling(() => client.signUpWithOauth(params), () => logout(), callbacks, "Failed to sign up with OAuth");
1100
1147
  if (res) {
1101
1148
  await handlePostAuth({
1102
1149
  method: base.AuthMethod.Oauth,
@@ -1121,7 +1168,7 @@ const ClientProvider = ({
1121
1168
  } : {
1122
1169
  ...params
1123
1170
  };
1124
- const res = await utils.withTurnkeyErrorHandling(() => client.completeOauth(params), callbacks, "Failed to complete OAuth");
1171
+ const res = await utils.withTurnkeyErrorHandling(() => client.completeOauth(params), () => logout(), callbacks, "Failed to complete OAuth");
1125
1172
  if (res) {
1126
1173
  await handlePostAuth({
1127
1174
  method: base.AuthMethod.Oauth,
@@ -1135,23 +1182,23 @@ const ClientProvider = ({
1135
1182
  if (!client) {
1136
1183
  throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1137
1184
  }
1138
- return utils.withTurnkeyErrorHandling(() => client.fetchWallets(params), callbacks, "Failed to fetch wallets");
1185
+ return utils.withTurnkeyErrorHandling(() => client.fetchWallets(params), () => logout(), callbacks, "Failed to fetch wallets");
1139
1186
  }, [client, callbacks]);
1140
1187
  const fetchWalletAccounts = react.useCallback(async params => {
1141
1188
  if (!client) {
1142
1189
  throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1143
1190
  }
1144
- return utils.withTurnkeyErrorHandling(() => client.fetchWalletAccounts(params), callbacks, "Failed to fetch wallet accounts");
1191
+ return utils.withTurnkeyErrorHandling(() => client.fetchWalletAccounts(params), () => logout(), callbacks, "Failed to fetch wallet accounts");
1145
1192
  }, [client, callbacks]);
1146
1193
  const fetchPrivateKeys = react.useCallback(async params => {
1147
1194
  if (!client) {
1148
1195
  throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1149
1196
  }
1150
- return utils.withTurnkeyErrorHandling(() => client.fetchPrivateKeys(params), callbacks, "Failed to fetch private keys");
1197
+ return utils.withTurnkeyErrorHandling(() => client.fetchPrivateKeys(params), () => logout(), callbacks, "Failed to fetch private keys");
1151
1198
  }, [client, callbacks]);
1152
1199
  const signMessage = react.useCallback(async params => {
1153
1200
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1154
- return utils.withTurnkeyErrorHandling(() => client.signMessage(params), callbacks, "Failed to sign message");
1201
+ return utils.withTurnkeyErrorHandling(() => client.signMessage(params), () => logout(), callbacks, "Failed to sign message");
1155
1202
  }, [client, callbacks]);
1156
1203
  const handleSignMessage = react.useCallback(async params => {
1157
1204
  const {
@@ -1181,6 +1228,9 @@ const ClientProvider = ({
1181
1228
  }),
1182
1229
  ...(params?.addEthereumPrefix && {
1183
1230
  addEthereumPrefix: params.addEthereumPrefix
1231
+ }),
1232
+ ...(params?.organizationId && {
1233
+ organizationId: params.organizationId
1184
1234
  })
1185
1235
  })
1186
1236
  });
@@ -1188,153 +1238,230 @@ const ClientProvider = ({
1188
1238
  }, [client, callbacks]);
1189
1239
  const signTransaction = react.useCallback(async params => {
1190
1240
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1191
- return utils.withTurnkeyErrorHandling(() => client.signTransaction(params), callbacks, "Failed to sign transaction");
1241
+ return utils.withTurnkeyErrorHandling(() => client.signTransaction(params), () => logout(), callbacks, "Failed to sign transaction");
1192
1242
  }, [client, callbacks]);
1193
1243
  const signAndSendTransaction = react.useCallback(async params => {
1194
1244
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1195
- return utils.withTurnkeyErrorHandling(() => client.signAndSendTransaction(params), callbacks, "Failed to sign transaction");
1245
+ return utils.withTurnkeyErrorHandling(() => client.signAndSendTransaction(params), () => logout(), callbacks, "Failed to sign transaction");
1196
1246
  }, [client, callbacks]);
1197
1247
  const fetchUser = react.useCallback(async params => {
1198
1248
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1199
- return utils.withTurnkeyErrorHandling(() => client.fetchUser(params), callbacks, "Failed to fetch user");
1249
+ return utils.withTurnkeyErrorHandling(() => client.fetchUser(params), () => logout(), callbacks, "Failed to fetch user");
1200
1250
  }, [client, callbacks]);
1201
1251
  const fetchOrCreateP256ApiKeyUser = react.useCallback(async params => {
1202
1252
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1203
- return utils.withTurnkeyErrorHandling(() => client.fetchOrCreateP256ApiKeyUser(params), callbacks, "Failed to fetch or create delegated access user");
1253
+ return utils.withTurnkeyErrorHandling(() => client.fetchOrCreateP256ApiKeyUser(params), () => logout(), callbacks, "Failed to fetch or create delegated access user");
1204
1254
  }, [client, callbacks]);
1205
1255
  const fetchOrCreatePolicies = react.useCallback(async params => {
1206
1256
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1207
- return utils.withTurnkeyErrorHandling(() => client.fetchOrCreatePolicies(params), callbacks, "Failed to fetch or create delegated access user");
1257
+ return utils.withTurnkeyErrorHandling(() => client.fetchOrCreatePolicies(params), () => logout(), callbacks, "Failed to fetch or create delegated access user");
1208
1258
  }, [client, callbacks]);
1209
1259
  const updateUserEmail = react.useCallback(async params => {
1210
1260
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1211
- const res = await utils.withTurnkeyErrorHandling(() => client.updateUserEmail(params), callbacks, "Failed to update user email");
1261
+ const res = await utils.withTurnkeyErrorHandling(() => client.updateUserEmail(params), () => logout(), callbacks, "Failed to update user email");
1212
1262
  if (res) await refreshUser({
1213
- stampWith: params?.stampWith
1263
+ stampWith: params?.stampWith,
1264
+ ...(params?.organizationId && {
1265
+ organizationId: params.organizationId
1266
+ }),
1267
+ ...(params?.userId && {
1268
+ userId: params.userId
1269
+ })
1214
1270
  });
1215
1271
  return res;
1216
1272
  }, [client, callbacks]);
1217
1273
  const removeUserEmail = react.useCallback(async params => {
1218
1274
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1219
- const res = await utils.withTurnkeyErrorHandling(() => client.removeUserEmail(params), callbacks, "Failed to remove user email");
1275
+ const res = await utils.withTurnkeyErrorHandling(() => client.removeUserEmail(params), () => logout(), callbacks, "Failed to remove user email");
1220
1276
  if (res) await refreshUser({
1221
- stampWith: params?.stampWith
1277
+ stampWith: params?.stampWith,
1278
+ ...(params?.organizationId && {
1279
+ organizationId: params.organizationId
1280
+ }),
1281
+ ...(params?.userId && {
1282
+ userId: params.userId
1283
+ })
1222
1284
  });
1223
1285
  return res;
1224
1286
  }, [client, callbacks]);
1225
1287
  const updateUserPhoneNumber = react.useCallback(async params => {
1226
1288
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1227
- const res = await utils.withTurnkeyErrorHandling(() => client.updateUserPhoneNumber(params), callbacks, "Failed to update user phone number");
1289
+ const res = await utils.withTurnkeyErrorHandling(() => client.updateUserPhoneNumber(params), () => logout(), callbacks, "Failed to update user phone number");
1228
1290
  if (res) await refreshUser({
1229
- stampWith: params?.stampWith
1291
+ stampWith: params?.stampWith,
1292
+ ...(params?.organizationId && {
1293
+ organizationId: params.organizationId
1294
+ }),
1295
+ ...(params?.userId && {
1296
+ userId: params.userId
1297
+ })
1230
1298
  });
1231
1299
  return res;
1232
1300
  }, [client, callbacks]);
1233
1301
  const removeUserPhoneNumber = react.useCallback(async params => {
1234
1302
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1235
- const res = await utils.withTurnkeyErrorHandling(() => client.removeUserPhoneNumber(params), callbacks, "Failed to remove user phone number");
1303
+ const res = await utils.withTurnkeyErrorHandling(() => client.removeUserPhoneNumber(params), () => logout(), callbacks, "Failed to remove user phone number");
1236
1304
  if (res) await refreshUser({
1237
- stampWith: params?.stampWith
1305
+ stampWith: params?.stampWith,
1306
+ ...(params?.organizationId && {
1307
+ organizationId: params.organizationId
1308
+ }),
1309
+ ...(params?.userId && {
1310
+ userId: params.userId
1311
+ })
1238
1312
  });
1239
1313
  return res;
1240
1314
  }, [client, callbacks]);
1241
1315
  const updateUserName = react.useCallback(async params => {
1242
1316
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1243
- const res = await utils.withTurnkeyErrorHandling(() => client.updateUserName(params), callbacks, "Failed to update user name");
1317
+ const res = await utils.withTurnkeyErrorHandling(() => client.updateUserName(params), () => logout(), callbacks, "Failed to update user name");
1244
1318
  if (res) await refreshUser({
1245
- stampWith: params?.stampWith
1319
+ stampWith: params?.stampWith,
1320
+ ...(params?.organizationId && {
1321
+ organizationId: params.organizationId
1322
+ }),
1323
+ ...(params?.userId && {
1324
+ userId: params.userId
1325
+ })
1246
1326
  });
1247
1327
  return res;
1248
1328
  }, [client, callbacks]);
1249
1329
  const addOauthProvider = react.useCallback(async params => {
1250
1330
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1251
- const res = await utils.withTurnkeyErrorHandling(() => client.addOauthProvider(params), callbacks, "Failed to add OAuth provider");
1331
+ const res = await utils.withTurnkeyErrorHandling(() => client.addOauthProvider(params), () => logout(), callbacks, "Failed to add OAuth provider");
1252
1332
  if (res) await refreshUser({
1253
- stampWith: params?.stampWith
1333
+ stampWith: params?.stampWith,
1334
+ ...(params?.organizationId && {
1335
+ organizationId: params.organizationId
1336
+ }),
1337
+ ...(params?.userId && {
1338
+ userId: params.userId
1339
+ })
1254
1340
  });
1255
1341
  return res;
1256
1342
  }, [client, callbacks]);
1257
1343
  const removeOauthProviders = react.useCallback(async params => {
1258
1344
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1259
- const res = await utils.withTurnkeyErrorHandling(() => client.removeOauthProviders(params), callbacks, "Failed to remove OAuth providers");
1345
+ const res = await utils.withTurnkeyErrorHandling(() => client.removeOauthProviders(params), () => logout(), callbacks, "Failed to remove OAuth providers");
1260
1346
  if (res) await refreshUser({
1261
- stampWith: params?.stampWith
1347
+ stampWith: params?.stampWith,
1348
+ ...(params?.organizationId && {
1349
+ organizationId: params.organizationId
1350
+ }),
1351
+ ...(params?.userId && {
1352
+ userId: params.userId
1353
+ })
1262
1354
  });
1263
1355
  return res;
1264
1356
  }, [client, callbacks]);
1265
1357
  const addPasskey = react.useCallback(async params => {
1266
1358
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1267
- const res = await utils.withTurnkeyErrorHandling(() => client.addPasskey(params), callbacks, "Failed to add passkey");
1359
+ const res = await utils.withTurnkeyErrorHandling(() => client.addPasskey(params), () => logout(), callbacks, "Failed to add passkey");
1268
1360
  if (res) await refreshUser({
1269
- stampWith: params?.stampWith
1361
+ stampWith: params?.stampWith,
1362
+ ...(params?.organizationId && {
1363
+ organizationId: params.organizationId
1364
+ }),
1365
+ ...(params?.userId && {
1366
+ userId: params.userId
1367
+ })
1270
1368
  });
1271
1369
  return res;
1272
1370
  }, [client, callbacks]);
1273
1371
  const removePasskeys = react.useCallback(async params => {
1274
1372
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1275
- const res = await utils.withTurnkeyErrorHandling(() => client.removePasskeys(params), callbacks, "Failed to remove passkeys");
1373
+ const res = await utils.withTurnkeyErrorHandling(() => client.removePasskeys(params), () => logout(), callbacks, "Failed to remove passkeys");
1276
1374
  if (res) await refreshUser({
1277
- stampWith: params?.stampWith
1375
+ stampWith: params?.stampWith,
1376
+ ...(params?.organizationId && {
1377
+ organizationId: params.organizationId
1378
+ }),
1379
+ ...(params?.userId && {
1380
+ userId: params.userId
1381
+ })
1278
1382
  });
1279
1383
  return res;
1280
1384
  }, [client, callbacks]);
1281
1385
  const createWallet = react.useCallback(async params => {
1282
1386
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1283
- const res = await utils.withTurnkeyErrorHandling(() => client.createWallet(params), callbacks, "Failed to create wallet");
1284
- if (res) await refreshWallets({
1285
- stampWith: params?.stampWith
1387
+ const res = await utils.withTurnkeyErrorHandling(() => client.createWallet(params), () => logout(), callbacks, "Failed to create wallet");
1388
+ const s = await getSession();
1389
+ if (res && s) await refreshWallets({
1390
+ stampWith: params?.stampWith,
1391
+ ...(params?.organizationId && {
1392
+ organizationId: params.organizationId
1393
+ })
1286
1394
  });
1287
1395
  return res;
1288
- }, [client, callbacks]);
1396
+ }, [client, session, callbacks]);
1289
1397
  const createWalletAccounts = react.useCallback(async params => {
1290
1398
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1291
- const res = await utils.withTurnkeyErrorHandling(() => client.createWalletAccounts(params), callbacks, "Failed to create wallet accounts");
1292
- if (res) await refreshWallets({
1293
- stampWith: params?.stampWith
1399
+ const res = await utils.withTurnkeyErrorHandling(() => client.createWalletAccounts(params), () => logout(), callbacks, "Failed to create wallet accounts");
1400
+ const s = await getSession();
1401
+ if (res && s) await refreshWallets({
1402
+ stampWith: params?.stampWith,
1403
+ ...(params?.organizationId && {
1404
+ organizationId: params.organizationId
1405
+ })
1294
1406
  });
1295
1407
  return res;
1296
- }, [client, callbacks]);
1408
+ }, [client, session, callbacks]);
1297
1409
  const exportWallet = react.useCallback(async params => {
1298
1410
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1299
- const res = await utils.withTurnkeyErrorHandling(() => client.exportWallet(params), callbacks, "Failed to export wallet");
1300
- if (res) await refreshWallets({
1301
- stampWith: params?.stampWith
1411
+ const res = await utils.withTurnkeyErrorHandling(() => client.exportWallet(params), () => logout(), callbacks, "Failed to export wallet");
1412
+ const s = await getSession();
1413
+ if (res && s) await refreshWallets({
1414
+ stampWith: params?.stampWith,
1415
+ ...(params?.organizationId && {
1416
+ organizationId: params.organizationId
1417
+ })
1302
1418
  });
1303
1419
  return res;
1304
- }, [client, callbacks]);
1420
+ }, [client, session, callbacks]);
1305
1421
  const exportPrivateKey = react.useCallback(async params => {
1306
1422
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1307
- const res = await utils.withTurnkeyErrorHandling(() => client.exportPrivateKey(params), callbacks, "Failed to export private key");
1423
+ const res = await utils.withTurnkeyErrorHandling(() => client.exportPrivateKey(params), () => logout(), callbacks, "Failed to export private key");
1308
1424
  return res;
1309
1425
  }, [client, callbacks]);
1310
1426
  const exportWalletAccount = react.useCallback(async params => {
1311
1427
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1312
- const res = await utils.withTurnkeyErrorHandling(() => client.exportWalletAccount(params), callbacks, "Failed to export wallet accounts");
1313
- if (res) await refreshWallets({
1314
- stampWith: params?.stampWith
1428
+ const res = await utils.withTurnkeyErrorHandling(() => client.exportWalletAccount(params), () => logout(), callbacks, "Failed to export wallet accounts");
1429
+ const s = await getSession();
1430
+ if (res && s) await refreshWallets({
1431
+ stampWith: params?.stampWith,
1432
+ ...(params?.organizationId && {
1433
+ organizationId: params.organizationId
1434
+ })
1315
1435
  });
1316
1436
  return res;
1317
1437
  }, [client, callbacks, masterConfig, session, user]);
1318
1438
  const importWallet = react.useCallback(async params => {
1319
1439
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1320
- const res = await utils.withTurnkeyErrorHandling(() => client.importWallet(params), callbacks, "Failed to import wallet");
1321
- if (res) await refreshWallets({
1322
- stampWith: params?.stampWith
1440
+ const res = await utils.withTurnkeyErrorHandling(() => client.importWallet(params), () => logout(), callbacks, "Failed to import wallet");
1441
+ const s = await getSession();
1442
+ if (res && s) await refreshWallets({
1443
+ stampWith: params?.stampWith,
1444
+ ...(params?.organizationId && {
1445
+ organizationId: params.organizationId
1446
+ }),
1447
+ ...(params?.userId && {
1448
+ userId: params.userId
1449
+ })
1323
1450
  });
1324
1451
  return res;
1325
1452
  }, [client, callbacks, masterConfig, session, user]);
1326
1453
  const importPrivateKey = react.useCallback(async params => {
1327
1454
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1328
- const res = await utils.withTurnkeyErrorHandling(() => client.importPrivateKey(params), callbacks, "Failed to import private key");
1455
+ const res = await utils.withTurnkeyErrorHandling(() => client.importPrivateKey(params), () => logout(), callbacks, "Failed to import private key");
1329
1456
  return res;
1330
1457
  }, [client, callbacks, masterConfig, session, user]);
1331
1458
  const deleteSubOrganization = react.useCallback(async params => {
1332
1459
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1333
- return utils.withTurnkeyErrorHandling(() => client.deleteSubOrganization(params), callbacks, "Failed to delete sub-organization");
1460
+ return utils.withTurnkeyErrorHandling(() => client.deleteSubOrganization(params), () => logout(), callbacks, "Failed to delete sub-organization");
1334
1461
  }, [client, callbacks, masterConfig, session, user]);
1335
1462
  const storeSession = react.useCallback(async params => {
1336
1463
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1337
- await utils.withTurnkeyErrorHandling(() => client.storeSession(params), callbacks, "Failed to store session");
1464
+ await utils.withTurnkeyErrorHandling(() => client.storeSession(params), () => logout(), callbacks, "Failed to store session");
1338
1465
  const sessionKey = await getActiveSessionKey();
1339
1466
  const session = await getSession({
1340
1467
  ...(sessionKey && {
@@ -1352,18 +1479,29 @@ const ClientProvider = ({
1352
1479
  }, [client, callbacks, masterConfig, session, user]);
1353
1480
  const clearSession = react.useCallback(async params => {
1354
1481
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1355
- await utils.withTurnkeyErrorHandling(() => client.clearSession(params), callbacks, "Failed to clear session");
1356
- const session = await getSession();
1357
- const allSessions = await getAllSessions();
1358
- setSession(session);
1359
- setAllSessions(allSessions);
1482
+ await utils.withTurnkeyErrorHandling(async () => client.clearSession(params), () => logout(), callbacks, "Failed to clear session");
1483
+ const sessionKey = params?.sessionKey ?? (await getActiveSessionKey());
1484
+ if (!sessionKey) return;
1485
+ if (!params?.sessionKey) {
1486
+ setSession(undefined);
1487
+ }
1488
+ clearSessionTimeouts([sessionKey]);
1489
+ // clear only the cleared session from allSessions
1490
+ const newAllSessions = {
1491
+ ...allSessions
1492
+ };
1493
+ if (newAllSessions) {
1494
+ delete newAllSessions[sessionKey];
1495
+ }
1496
+ setAllSessions(newAllSessions);
1360
1497
  return;
1361
1498
  }, [client, callbacks, session, user, masterConfig]);
1362
1499
  const clearAllSessions = react.useCallback(async () => {
1363
1500
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1364
1501
  setSession(undefined);
1365
1502
  setAllSessions(undefined);
1366
- return await utils.withTurnkeyErrorHandling(() => client.clearAllSessions(), callbacks, "Failed to clear all sessions");
1503
+ clearSessionTimeouts();
1504
+ return await utils.withTurnkeyErrorHandling(() => client.clearAllSessions(), () => logout(), callbacks, "Failed to clear all sessions");
1367
1505
  }, [client, callbacks, session, user, masterConfig]);
1368
1506
  const refreshSession = react.useCallback(async params => {
1369
1507
  if (!client) {
@@ -1376,7 +1514,7 @@ const ClientProvider = ({
1376
1514
  const sessionKey = params?.sessionKey ?? activeSessionKey;
1377
1515
  const res = await utils.withTurnkeyErrorHandling(() => client.refreshSession({
1378
1516
  ...params
1379
- }), callbacks, "Failed to refresh session");
1517
+ }), () => logout(), callbacks, "Failed to refresh session");
1380
1518
  const session = await getSession({
1381
1519
  sessionKey
1382
1520
  });
@@ -1396,71 +1534,89 @@ const ClientProvider = ({
1396
1534
  }, [client, callbacks, scheduleSessionExpiration, session, user, masterConfig]);
1397
1535
  const getSession = react.useCallback(async params => {
1398
1536
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1399
- return utils.withTurnkeyErrorHandling(() => client.getSession(params), callbacks, "Failed to get session");
1537
+ return utils.withTurnkeyErrorHandling(() => client.getSession(params), () => logout(), callbacks, "Failed to get session");
1400
1538
  }, [client, callbacks, masterConfig, session, user]);
1401
1539
  const getAllSessions = react.useCallback(async () => {
1402
1540
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1403
- return utils.withTurnkeyErrorHandling(() => client.getAllSessions(), callbacks, "Failed to get all sessions");
1541
+ return utils.withTurnkeyErrorHandling(() => client.getAllSessions(), () => logout(), callbacks, "Failed to get all sessions");
1404
1542
  }, [client, callbacks, masterConfig, session, user]);
1405
1543
  const setActiveSession = react.useCallback(async params => {
1406
1544
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1407
1545
  const session = await utils.withTurnkeyErrorHandling(() => client.getSession({
1408
1546
  sessionKey: params.sessionKey
1409
- }), callbacks, "Failed to get session");
1410
- if (!session) {
1547
+ }), () => logout(), callbacks, "Failed to get session");
1548
+ const s = await getSession();
1549
+ if (!s) {
1411
1550
  throw new sdkTypes.TurnkeyError("Session not found.", sdkTypes.TurnkeyErrorCodes.NOT_FOUND);
1412
1551
  }
1413
- await utils.withTurnkeyErrorHandling(() => client.setActiveSession(params), callbacks, "Failed to set active session");
1552
+ await utils.withTurnkeyErrorHandling(() => client.setActiveSession(params), () => logout(), callbacks, "Failed to set active session");
1414
1553
  setSession(session);
1415
1554
  await utils.withTurnkeyErrorHandling(async () => {
1416
1555
  await refreshWallets();
1417
1556
  await refreshUser();
1418
- }, callbacks, "Failed to refresh data after setting active session");
1557
+ }, () => logout(), callbacks, "Failed to refresh data after setting active session");
1419
1558
  return;
1420
1559
  }, [client, callbacks, session, user, masterConfig]);
1421
1560
  const getActiveSessionKey = react.useCallback(async () => {
1422
1561
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1423
- return utils.withTurnkeyErrorHandling(() => client.getActiveSessionKey(), callbacks, "Failed to get active session key");
1562
+ return utils.withTurnkeyErrorHandling(() => client.getActiveSessionKey(), () => logout(), callbacks, "Failed to get active session key");
1424
1563
  }, [client, callbacks, masterConfig, session, user]);
1425
1564
  const clearUnusedKeyPairs = react.useCallback(async () => {
1426
1565
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1427
- return utils.withTurnkeyErrorHandling(() => client.clearUnusedKeyPairs(), callbacks, "Failed to clear unused key pairs");
1566
+ return utils.withTurnkeyErrorHandling(() => client.clearUnusedKeyPairs(), () => logout(), callbacks, "Failed to clear unused key pairs");
1428
1567
  }, [client, callbacks, masterConfig, session, user]);
1429
1568
  const createApiKeyPair = react.useCallback(async params => {
1430
1569
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1431
- return utils.withTurnkeyErrorHandling(() => client.createApiKeyPair(params), callbacks, "Failed to create API key pair");
1570
+ return utils.withTurnkeyErrorHandling(() => client.createApiKeyPair(params), () => logout(), callbacks, "Failed to create API key pair");
1432
1571
  }, [client, callbacks, session, user, masterConfig]);
1433
1572
  const getProxyAuthConfig = react.useCallback(async () => {
1434
1573
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1435
- return utils.withTurnkeyErrorHandling(() => client.getProxyAuthConfig(), callbacks, "Failed to get proxy auth config");
1574
+ return utils.withTurnkeyErrorHandling(() => client.getProxyAuthConfig(), () => logout(), callbacks, "Failed to get proxy auth config");
1436
1575
  }, [client, callbacks, masterConfig, session, user]);
1437
1576
  const refreshUser = react.useCallback(async params => {
1438
1577
  if (!masterConfig?.autoRefreshManagedState) return;
1439
1578
  const {
1440
- stampWith
1579
+ stampWith,
1580
+ organizationId,
1581
+ userId
1441
1582
  } = params || {};
1442
1583
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1443
1584
  const user = await utils.withTurnkeyErrorHandling(() => fetchUser({
1444
- stampWith
1445
- }), callbacks, "Failed to refresh user");
1585
+ stampWith,
1586
+ ...(organizationId && {
1587
+ organizationId
1588
+ }),
1589
+ ...(userId && {
1590
+ userId
1591
+ })
1592
+ }), () => logout(), callbacks, "Failed to refresh user");
1446
1593
  if (user) {
1447
1594
  setUser(user);
1448
1595
  }
1449
1596
  }, [client, callbacks, fetchUser, masterConfig, session, user]);
1450
1597
  const refreshWallets = react.useCallback(async params => {
1451
- if (!masterConfig?.autoRefreshManagedState) return;
1598
+ if (!masterConfig?.autoRefreshManagedState) return [];
1452
1599
  const {
1453
- stampWith
1600
+ stampWith,
1601
+ organizationId,
1602
+ userId
1454
1603
  } = params || {};
1455
1604
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
1456
- const walletProviders = await utils.withTurnkeyErrorHandling(() => fetchWalletProviders(), callbacks, "Failed to refresh wallets");
1605
+ const walletProviders = await utils.withTurnkeyErrorHandling(() => fetchWalletProviders(), () => logout(), callbacks, "Failed to refresh wallets");
1457
1606
  const wallets = await utils.withTurnkeyErrorHandling(() => fetchWallets({
1458
1607
  stampWith,
1459
- walletProviders
1460
- }), callbacks, "Failed to refresh wallets");
1608
+ walletProviders,
1609
+ ...(organizationId && {
1610
+ organizationId
1611
+ }),
1612
+ ...(userId && {
1613
+ userId
1614
+ })
1615
+ }), () => logout(), callbacks, "Failed to refresh wallets");
1461
1616
  if (wallets) {
1462
1617
  setWallets(wallets);
1463
1618
  }
1619
+ return wallets;
1464
1620
  }, [client, callbacks, fetchWalletProviders, fetchWallets, masterConfig, session, user]);
1465
1621
  const handleDiscordOauth = react.useCallback(async params => {
1466
1622
  const {
@@ -2135,86 +2291,112 @@ const ClientProvider = ({
2135
2291
  }
2136
2292
  }, [client, callbacks, masterConfig, client, session, user]);
2137
2293
  const handleLogin = react.useCallback(async params => {
2294
+ const logo = masterConfig?.ui?.darkMode ? params?.logoDark ?? masterConfig?.ui?.logoDark : params?.logoLight ?? masterConfig?.ui?.logoLight;
2138
2295
  pushPage({
2139
- key: "Log in or sign up",
2296
+ key: params?.title ?? "Log in or sign up",
2140
2297
  content: jsxRuntime.jsx(index.AuthComponent, {
2141
- sessionKey: params?.sessionKey
2142
- })
2298
+ sessionKey: params?.sessionKey,
2299
+ logo: logo,
2300
+ logoClassName: params?.logoClassName,
2301
+ title: params?.title
2302
+ }),
2303
+ showTitle: logo ? false : true
2143
2304
  });
2144
2305
  }, [pushPage, masterConfig, client, session, user]);
2145
2306
  const handleExportWallet = react.useCallback(async params => {
2146
2307
  const {
2147
2308
  walletId,
2148
2309
  targetPublicKey,
2149
- stampWith
2310
+ stampWith,
2311
+ organizationId
2150
2312
  } = params;
2151
- pushPage({
2313
+ return new Promise((resolve, reject) => pushPage({
2152
2314
  key: "Export wallet",
2153
2315
  content: jsxRuntime.jsx(Export.ExportComponent, {
2154
2316
  target: walletId,
2155
2317
  exportType: base.ExportType.Wallet,
2156
- ...(targetPublicKey !== undefined ? {
2318
+ ...(targetPublicKey !== undefined && {
2157
2319
  targetPublicKey
2158
- } : {}),
2159
- ...(stampWith !== undefined ? {
2320
+ }),
2321
+ ...(stampWith !== undefined && {
2160
2322
  stampWith
2161
- } : {})
2323
+ }),
2324
+ ...(organizationId !== undefined && {
2325
+ organizationId
2326
+ }),
2327
+ onSuccess: () => resolve(),
2328
+ onError: error => reject(error)
2162
2329
  })
2163
- });
2330
+ }));
2164
2331
  }, [pushPage, masterConfig, client, session, user]);
2165
2332
  const handleExportPrivateKey = react.useCallback(async params => {
2166
2333
  const {
2167
2334
  privateKeyId,
2168
2335
  targetPublicKey,
2169
2336
  keyFormat,
2170
- stampWith
2337
+ stampWith,
2338
+ organizationId
2171
2339
  } = params;
2172
- pushPage({
2340
+ return new Promise((resolve, reject) => pushPage({
2173
2341
  key: "Export private key",
2174
2342
  content: jsxRuntime.jsx(Export.ExportComponent, {
2175
2343
  target: privateKeyId,
2176
2344
  exportType: base.ExportType.PrivateKey,
2177
- ...(keyFormat !== undefined ? {
2345
+ ...(keyFormat !== undefined && {
2178
2346
  keyFormat
2179
- } : {}),
2180
- ...(targetPublicKey !== undefined ? {
2347
+ }),
2348
+ ...(targetPublicKey !== undefined && {
2181
2349
  targetPublicKey
2182
- } : {}),
2183
- ...(stampWith !== undefined ? {
2350
+ }),
2351
+ ...(stampWith !== undefined && {
2184
2352
  stampWith
2185
- } : {})
2353
+ }),
2354
+ ...(organizationId !== undefined && {
2355
+ organizationId
2356
+ }),
2357
+ onSuccess: () => resolve(),
2358
+ onError: error => reject(error)
2186
2359
  })
2187
- });
2360
+ }));
2188
2361
  }, [pushPage, masterConfig, client, session, user]);
2189
2362
  const handleExportWalletAccount = react.useCallback(async params => {
2190
2363
  const {
2191
2364
  address,
2192
2365
  targetPublicKey,
2193
2366
  keyFormat,
2194
- stampWith
2367
+ stampWith,
2368
+ organizationId
2195
2369
  } = params;
2196
- pushPage({
2370
+ return new Promise((resolve, reject) => pushPage({
2197
2371
  key: "Export wallet account",
2198
2372
  content: jsxRuntime.jsx(Export.ExportComponent, {
2199
2373
  target: address,
2200
2374
  exportType: base.ExportType.WalletAccount,
2201
- ...(keyFormat !== undefined ? {
2375
+ ...(keyFormat !== undefined && {
2202
2376
  keyFormat
2203
- } : {}),
2204
- ...(targetPublicKey !== undefined ? {
2377
+ }),
2378
+ ...(targetPublicKey !== undefined && {
2205
2379
  targetPublicKey
2206
- } : {}),
2207
- ...(stampWith !== undefined ? {
2380
+ }),
2381
+ ...(stampWith !== undefined && {
2208
2382
  stampWith
2209
- } : {})
2383
+ }),
2384
+ ...(organizationId !== undefined && {
2385
+ organizationId
2386
+ }),
2387
+ onSuccess: () => resolve(),
2388
+ onError: error => reject(error)
2210
2389
  })
2211
- });
2390
+ }));
2212
2391
  }, [pushPage, masterConfig, client, session, user]);
2213
2392
  const handleImportWallet = react.useCallback(async params => {
2214
2393
  const {
2215
2394
  defaultWalletAccounts,
2216
2395
  successPageDuration = 2000,
2217
- stampWith
2396
+ stampWith,
2397
+ walletName,
2398
+ organizationId,
2399
+ userId
2218
2400
  } = params || {};
2219
2401
  try {
2220
2402
  return utils.withTurnkeyErrorHandling(() => new Promise((resolve, reject) => pushPage({
@@ -2233,9 +2415,18 @@ const ClientProvider = ({
2233
2415
  }),
2234
2416
  ...(stampWith !== undefined && {
2235
2417
  stampWith
2418
+ }),
2419
+ ...(walletName !== undefined && {
2420
+ name: walletName
2421
+ }),
2422
+ ...(organizationId !== undefined && {
2423
+ organizationId
2424
+ }),
2425
+ ...(userId !== undefined && {
2426
+ userId
2236
2427
  })
2237
2428
  })
2238
- })));
2429
+ })), () => logout());
2239
2430
  } catch (error) {
2240
2431
  if (error instanceof sdkTypes.TurnkeyError) {
2241
2432
  throw error;
@@ -2248,7 +2439,9 @@ const ClientProvider = ({
2248
2439
  curve,
2249
2440
  addressFormats,
2250
2441
  successPageDuration = 2000,
2251
- stampWith
2442
+ stampWith,
2443
+ keyName,
2444
+ organizationId
2252
2445
  } = params || {};
2253
2446
  try {
2254
2447
  return utils.withTurnkeyErrorHandling(() => new Promise((resolve, reject) => pushPage({
@@ -2266,9 +2459,15 @@ const ClientProvider = ({
2266
2459
  }),
2267
2460
  ...(stampWith !== undefined && {
2268
2461
  stampWith
2462
+ }),
2463
+ ...(keyName !== undefined && {
2464
+ name: keyName
2465
+ }),
2466
+ ...(organizationId !== undefined && {
2467
+ organizationId
2269
2468
  })
2270
2469
  })
2271
- })));
2470
+ })), () => logout());
2272
2471
  } catch (error) {
2273
2472
  if (error instanceof sdkTypes.TurnkeyError) {
2274
2473
  throw error;
@@ -2284,8 +2483,14 @@ const ClientProvider = ({
2284
2483
  stampWith
2285
2484
  } = params || {};
2286
2485
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
2287
- if (!session) {
2288
- throw new sdkTypes.TurnkeyError("No active session found.", sdkTypes.TurnkeyErrorCodes.NO_SESSION_FOUND);
2486
+ const s = await getSession();
2487
+ const organizationId = params?.organizationId || s?.organizationId;
2488
+ if (!organizationId) {
2489
+ throw new sdkTypes.TurnkeyError("A session or passed in organization ID is required.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2490
+ }
2491
+ const userId = params?.userId || user?.userId;
2492
+ if (!userId) {
2493
+ throw new sdkTypes.TurnkeyError("A user ID is required to update a user name.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2289
2494
  }
2290
2495
  const onSuccess = () => {
2291
2496
  if (!successPageDuration) return;
@@ -2316,21 +2521,26 @@ const ClientProvider = ({
2316
2521
  },
2317
2522
  successPageDuration: successPageDuration,
2318
2523
  stampWith: stampWith,
2319
- ...(title !== undefined ? {
2524
+ ...(title !== undefined && {
2320
2525
  title
2321
- } : {}),
2322
- ...(subTitle !== undefined ? {
2526
+ }),
2527
+ ...(subTitle !== undefined && {
2323
2528
  subTitle
2324
- } : {})
2529
+ }),
2530
+ organizationId: organizationId,
2531
+ userId: userId
2325
2532
  }),
2326
2533
  showTitle: false
2327
2534
  });
2328
- }));
2535
+ }), () => logout());
2329
2536
  } else {
2330
2537
  const res = await updateUserName({
2331
2538
  userName: params.userName,
2332
2539
  userId: user.userId,
2333
- stampWith
2540
+ stampWith,
2541
+ ...(organizationId !== undefined && {
2542
+ organizationId
2543
+ })
2334
2544
  });
2335
2545
  onSuccess();
2336
2546
  return res;
@@ -2346,18 +2556,25 @@ const ClientProvider = ({
2346
2556
  const {
2347
2557
  successPageDuration = 2000,
2348
2558
  subTitle,
2349
- title
2559
+ title,
2560
+ stampWith
2350
2561
  } = params || {};
2351
2562
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
2352
- if (!session) {
2353
- throw new sdkTypes.TurnkeyError("No active session found.", sdkTypes.TurnkeyErrorCodes.NO_SESSION_FOUND);
2354
- }
2355
2563
  if (!masterConfig) {
2356
2564
  throw new sdkTypes.TurnkeyError("Config is not ready yet!", sdkTypes.TurnkeyErrorCodes.CONFIG_NOT_INITIALIZED);
2357
2565
  }
2358
2566
  if (!masterConfig.auth?.methods?.smsOtpAuthEnabled) {
2359
2567
  throw new sdkTypes.TurnkeyError("SMS OTP authentication is not enabled in the configuration.", sdkTypes.TurnkeyErrorCodes.AUTH_METHOD_NOT_ENABLED);
2360
2568
  }
2569
+ const s = await getSession();
2570
+ const organizationId = params?.organizationId || s?.organizationId;
2571
+ if (!organizationId) {
2572
+ throw new sdkTypes.TurnkeyError("A session or passed in organization ID is required.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2573
+ }
2574
+ const userId = params?.userId || s?.userId;
2575
+ if (!userId) {
2576
+ throw new sdkTypes.TurnkeyError("A user ID is required to update a phone number.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2577
+ }
2361
2578
  const onSuccess = () => {
2362
2579
  if (!successPageDuration) return;
2363
2580
  pushPage({
@@ -2382,16 +2599,21 @@ const ClientProvider = ({
2382
2599
  successPageDuration: successPageDuration,
2383
2600
  onSuccess: userId => resolve(userId),
2384
2601
  onError: error => reject(error),
2385
- ...(title !== undefined ? {
2602
+ organizationId: organizationId,
2603
+ userId: userId,
2604
+ ...(title !== undefined && {
2386
2605
  title
2387
- } : {}),
2388
- ...(subTitle !== undefined ? {
2606
+ }),
2607
+ ...(subTitle !== undefined && {
2389
2608
  subTitle
2390
- } : {})
2609
+ }),
2610
+ ...(stampWith !== undefined && {
2611
+ stampWith
2612
+ })
2391
2613
  }),
2392
2614
  showTitle: false
2393
2615
  });
2394
- }));
2616
+ }), () => logout());
2395
2617
  } else {
2396
2618
  const otpId = await initOtp({
2397
2619
  otpType: core.OtpType.Sms,
@@ -2404,6 +2626,8 @@ const ClientProvider = ({
2404
2626
  otpType: core.OtpType.Sms,
2405
2627
  contact: params.phoneNumber,
2406
2628
  otpId: otpId,
2629
+ otpLength: masterConfig.auth?.otpLength !== undefined ? Number(masterConfig.auth.otpLength) : undefined,
2630
+ alphanumeric: masterConfig.auth?.otpAlphanumeric,
2407
2631
  onContinue: async otpCode => {
2408
2632
  try {
2409
2633
  const {
@@ -2417,7 +2641,11 @@ const ClientProvider = ({
2417
2641
  const res = await updateUserPhoneNumber({
2418
2642
  phoneNumber: params.phoneNumber,
2419
2643
  verificationToken,
2420
- userId: user.userId
2644
+ userId,
2645
+ organizationId,
2646
+ ...(stampWith !== undefined && {
2647
+ stampWith
2648
+ })
2421
2649
  });
2422
2650
  onSuccess();
2423
2651
  resolve(res);
@@ -2437,7 +2665,7 @@ const ClientProvider = ({
2437
2665
  }),
2438
2666
  showTitle: false
2439
2667
  });
2440
- }), callbacks, "Failed to update phone number");
2668
+ }), () => logout(), callbacks, "Failed to update phone number");
2441
2669
  }
2442
2670
  } catch (error) {
2443
2671
  if (error instanceof sdkTypes.TurnkeyError) {
@@ -2450,11 +2678,18 @@ const ClientProvider = ({
2450
2678
  const {
2451
2679
  successPageDuration = 2000,
2452
2680
  subTitle,
2453
- title
2681
+ title,
2682
+ stampWith
2454
2683
  } = params || {};
2455
2684
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
2456
- if (!session) {
2457
- throw new sdkTypes.TurnkeyError("No active session found.", sdkTypes.TurnkeyErrorCodes.NO_SESSION_FOUND);
2685
+ const s = await getSession();
2686
+ const organizationId = params?.organizationId || s?.organizationId;
2687
+ if (!organizationId) {
2688
+ throw new sdkTypes.TurnkeyError("A session or passed in organization ID is required.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2689
+ }
2690
+ const userId = params?.userId || s?.userId;
2691
+ if (!userId) {
2692
+ throw new sdkTypes.TurnkeyError("A user ID is required to update an email.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2458
2693
  }
2459
2694
  const onSuccess = () => {
2460
2695
  if (!successPageDuration) return;
@@ -2482,16 +2717,21 @@ const ClientProvider = ({
2482
2717
  resolve(userId);
2483
2718
  },
2484
2719
  onError: error => reject(error),
2485
- ...(title !== undefined ? {
2720
+ organizationId: organizationId,
2721
+ userId: userId,
2722
+ ...(title !== undefined && {
2486
2723
  title
2487
- } : {}),
2488
- ...(subTitle !== undefined ? {
2724
+ }),
2725
+ ...(subTitle !== undefined && {
2489
2726
  subTitle
2490
- } : {})
2727
+ }),
2728
+ ...(stampWith !== undefined && {
2729
+ stampWith
2730
+ })
2491
2731
  }),
2492
2732
  showTitle: false
2493
2733
  });
2494
- }));
2734
+ }), () => logout());
2495
2735
  } else {
2496
2736
  const otpId = await initOtp({
2497
2737
  otpType: core.OtpType.Email,
@@ -2504,6 +2744,8 @@ const ClientProvider = ({
2504
2744
  otpType: core.OtpType.Email,
2505
2745
  contact: params.email,
2506
2746
  otpId: otpId,
2747
+ otpLength: masterConfig?.auth?.otpLength !== undefined ? Number(masterConfig.auth.otpLength) : undefined,
2748
+ alphanumeric: masterConfig?.auth?.otpAlphanumeric,
2507
2749
  onContinue: async otpCode => {
2508
2750
  try {
2509
2751
  const {
@@ -2517,7 +2759,11 @@ const ClientProvider = ({
2517
2759
  const res = await updateUserEmail({
2518
2760
  email: params.email,
2519
2761
  verificationToken,
2520
- userId: user.userId
2762
+ userId,
2763
+ organizationId,
2764
+ ...(stampWith !== undefined && {
2765
+ stampWith
2766
+ })
2521
2767
  });
2522
2768
  onSuccess();
2523
2769
  resolve(res);
@@ -2534,7 +2780,7 @@ const ClientProvider = ({
2534
2780
  }),
2535
2781
  showTitle: false
2536
2782
  });
2537
- }), callbacks, "Failed to update email");
2783
+ }), () => logout(), callbacks, "Failed to update email");
2538
2784
  }
2539
2785
  } catch (error) {
2540
2786
  if (error instanceof sdkTypes.TurnkeyError) {
@@ -2547,13 +2793,20 @@ const ClientProvider = ({
2547
2793
  const {
2548
2794
  successPageDuration = 2000,
2549
2795
  subTitle,
2550
- title
2796
+ title,
2797
+ stampWith
2551
2798
  } = params || {};
2552
2799
  if (!client) {
2553
2800
  throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
2554
2801
  }
2555
- if (!session) {
2556
- throw new sdkTypes.TurnkeyError("No active session found.", sdkTypes.TurnkeyErrorCodes.NO_SESSION_FOUND);
2802
+ const s = await getSession();
2803
+ const organizationId = params?.organizationId || s?.organizationId;
2804
+ if (!organizationId) {
2805
+ throw new sdkTypes.TurnkeyError("A session or passed in organization ID is required.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2806
+ }
2807
+ const userId = params?.userId || s?.userId;
2808
+ if (!userId) {
2809
+ throw new sdkTypes.TurnkeyError("A user ID is required to add an email.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2557
2810
  }
2558
2811
  const onSuccess = () => {
2559
2812
  if (!successPageDuration) return;
@@ -2586,11 +2839,14 @@ const ClientProvider = ({
2586
2839
  } : {}),
2587
2840
  ...(subTitle !== undefined ? {
2588
2841
  subTitle
2589
- } : {})
2842
+ } : {}),
2843
+ organizationId: organizationId,
2844
+ userId: userId,
2845
+ stampWith: stampWith
2590
2846
  }),
2591
2847
  showTitle: false
2592
2848
  });
2593
- }));
2849
+ }), () => logout());
2594
2850
  } else {
2595
2851
  const otpId = await initOtp({
2596
2852
  otpType: core.OtpType.Email,
@@ -2603,6 +2859,8 @@ const ClientProvider = ({
2603
2859
  otpType: core.OtpType.Email,
2604
2860
  contact: params.email,
2605
2861
  otpId: otpId,
2862
+ otpLength: masterConfig?.auth?.otpLength !== undefined ? Number(masterConfig.auth.otpLength) : undefined,
2863
+ alphanumeric: masterConfig?.auth?.otpAlphanumeric,
2606
2864
  onContinue: async otpCode => {
2607
2865
  try {
2608
2866
  const {
@@ -2616,7 +2874,9 @@ const ClientProvider = ({
2616
2874
  const res = await updateUserEmail({
2617
2875
  email: params.email,
2618
2876
  verificationToken,
2619
- userId: user.userId
2877
+ userId,
2878
+ organizationId,
2879
+ stampWith
2620
2880
  });
2621
2881
  onSuccess();
2622
2882
  resolve(res);
@@ -2633,7 +2893,7 @@ const ClientProvider = ({
2633
2893
  }),
2634
2894
  showTitle: false
2635
2895
  });
2636
- }), callbacks, "Failed to add email");
2896
+ }), () => logout(), callbacks, "Failed to add email");
2637
2897
  }
2638
2898
  } catch (error) {
2639
2899
  if (error instanceof sdkTypes.TurnkeyError) {
@@ -2646,18 +2906,25 @@ const ClientProvider = ({
2646
2906
  const {
2647
2907
  successPageDuration = 2000,
2648
2908
  subTitle,
2649
- title
2909
+ title,
2910
+ stampWith
2650
2911
  } = params || {};
2651
2912
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
2652
- if (!session) {
2653
- throw new sdkTypes.TurnkeyError("No active session found.", sdkTypes.TurnkeyErrorCodes.NO_SESSION_FOUND);
2654
- }
2655
2913
  if (!masterConfig) {
2656
2914
  throw new sdkTypes.TurnkeyError("Config is not ready yet!", sdkTypes.TurnkeyErrorCodes.CONFIG_NOT_INITIALIZED);
2657
2915
  }
2658
2916
  if (!masterConfig.auth?.methods?.smsOtpAuthEnabled) {
2659
2917
  throw new sdkTypes.TurnkeyError("SMS OTP authentication is not enabled in the configuration.", sdkTypes.TurnkeyErrorCodes.AUTH_METHOD_NOT_ENABLED);
2660
2918
  }
2919
+ const s = await getSession();
2920
+ const organizationId = params?.organizationId || s?.organizationId;
2921
+ if (!organizationId) {
2922
+ throw new sdkTypes.TurnkeyError("A session or passed in organization ID is required.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2923
+ }
2924
+ const userId = params?.userId || s?.userId;
2925
+ if (!userId) {
2926
+ throw new sdkTypes.TurnkeyError("A user ID is required to add a phone number.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2927
+ }
2661
2928
  const onSuccess = () => {
2662
2929
  if (!successPageDuration) return;
2663
2930
  pushPage({
@@ -2691,11 +2958,14 @@ const ClientProvider = ({
2691
2958
  }),
2692
2959
  ...(subTitle !== undefined && {
2693
2960
  subTitle
2694
- })
2961
+ }),
2962
+ organizationId: organizationId,
2963
+ userId: userId,
2964
+ stampWith: stampWith
2695
2965
  }),
2696
2966
  showTitle: false
2697
2967
  });
2698
- }), callbacks, "Failed to add phone number");
2968
+ }), () => logout(), callbacks, "Failed to add phone number");
2699
2969
  } else {
2700
2970
  const otpId = await initOtp({
2701
2971
  otpType: core.OtpType.Sms,
@@ -2708,6 +2978,8 @@ const ClientProvider = ({
2708
2978
  otpType: core.OtpType.Sms,
2709
2979
  contact: params.phoneNumber,
2710
2980
  otpId: otpId,
2981
+ otpLength: masterConfig.auth?.otpLength !== undefined ? Number(masterConfig.auth.otpLength) : undefined,
2982
+ alphanumeric: masterConfig?.auth?.otpAlphanumeric,
2711
2983
  onContinue: async otpCode => {
2712
2984
  try {
2713
2985
  const {
@@ -2721,7 +2993,9 @@ const ClientProvider = ({
2721
2993
  const res = await updateUserPhoneNumber({
2722
2994
  phoneNumber: params.phoneNumber,
2723
2995
  verificationToken,
2724
- userId: user.userId
2996
+ userId: user.userId,
2997
+ organizationId,
2998
+ stampWith
2725
2999
  });
2726
3000
  onSuccess();
2727
3001
  resolve(res);
@@ -2741,7 +3015,7 @@ const ClientProvider = ({
2741
3015
  }),
2742
3016
  showTitle: false
2743
3017
  });
2744
- }), callbacks, "Failed to add phone number");
3018
+ }), () => logout(), callbacks, "Failed to add phone number");
2745
3019
  }
2746
3020
  } catch (error) {
2747
3021
  if (error instanceof sdkTypes.TurnkeyError) {
@@ -2760,8 +3034,10 @@ const ClientProvider = ({
2760
3034
  userId
2761
3035
  } = params;
2762
3036
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
2763
- if (!session) {
2764
- throw new sdkTypes.TurnkeyError("No active session found.", sdkTypes.TurnkeyErrorCodes.NO_SESSION_FOUND);
3037
+ const s = await getSession();
3038
+ const organizationId = params?.organizationId || s?.organizationId;
3039
+ if (!organizationId) {
3040
+ throw new sdkTypes.TurnkeyError("A session or passed in organization ID is required.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2765
3041
  }
2766
3042
  return utils.withTurnkeyErrorHandling(() => new Promise((resolve, reject) => {
2767
3043
  pushPage({
@@ -2784,12 +3060,15 @@ const ClientProvider = ({
2784
3060
  }),
2785
3061
  ...(subTitle !== undefined && {
2786
3062
  subTitle
3063
+ }),
3064
+ ...(organizationId !== undefined && {
3065
+ organizationId
2787
3066
  })
2788
3067
  }),
2789
3068
  showTitle: false,
2790
3069
  preventBack: true
2791
3070
  });
2792
- }), callbacks, "Failed to remove passkey");
3071
+ }), () => logout(), callbacks, "Failed to remove passkey");
2793
3072
  }, [pushPage, masterConfig, client, session, user]);
2794
3073
  const handleAddPasskey = react.useCallback(async params => {
2795
3074
  const {
@@ -2799,10 +3078,15 @@ const ClientProvider = ({
2799
3078
  stampWith
2800
3079
  } = params || {};
2801
3080
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
2802
- if (!session) {
2803
- throw new sdkTypes.TurnkeyError("No active session found.", sdkTypes.TurnkeyErrorCodes.NO_SESSION_FOUND);
3081
+ const s = await getSession();
3082
+ const organizationId = params?.organizationId || s?.organizationId;
3083
+ if (!organizationId) {
3084
+ throw new sdkTypes.TurnkeyError("A session or passed in organization ID is required.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
3085
+ }
3086
+ const userId = params?.userId || s?.userId;
3087
+ if (!userId) {
3088
+ throw new sdkTypes.TurnkeyError("A user ID or a valid session is required to add a passkey.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2804
3089
  }
2805
- const userId = params?.userId || session.userId;
2806
3090
  try {
2807
3091
  const resPromise = addPasskey({
2808
3092
  ...(name && {
@@ -2812,7 +3096,10 @@ const ClientProvider = ({
2812
3096
  displayName
2813
3097
  }),
2814
3098
  userId,
2815
- stampWith
3099
+ stampWith,
3100
+ ...(organizationId && {
3101
+ organizationId
3102
+ })
2816
3103
  });
2817
3104
  resPromise.then(() => {
2818
3105
  pushPage({
@@ -2845,8 +3132,10 @@ const ClientProvider = ({
2845
3132
  stampWith
2846
3133
  } = params;
2847
3134
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
2848
- if (!session) {
2849
- throw new sdkTypes.TurnkeyError("No active session found.", sdkTypes.TurnkeyErrorCodes.NO_SESSION_FOUND);
3135
+ const s = await getSession();
3136
+ const organizationId = params?.organizationId || s?.organizationId;
3137
+ if (!organizationId) {
3138
+ throw new sdkTypes.TurnkeyError("A session or passed in organization ID is required.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2850
3139
  }
2851
3140
  try {
2852
3141
  return new Promise((resolve, reject) => {
@@ -2867,6 +3156,9 @@ const ClientProvider = ({
2867
3156
  }),
2868
3157
  ...(subTitle !== undefined && {
2869
3158
  subTitle
3159
+ }),
3160
+ ...(organizationId !== undefined && {
3161
+ organizationId
2870
3162
  })
2871
3163
  }),
2872
3164
  showTitle: false,
@@ -2882,97 +3174,113 @@ const ClientProvider = ({
2882
3174
  }, [pushPage, masterConfig, client, session, user]);
2883
3175
  const handleAddOauthProvider = react.useCallback(async params => {
2884
3176
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
2885
- if (!session) {
2886
- throw new sdkTypes.TurnkeyError("No active session found.", sdkTypes.TurnkeyErrorCodes.NO_SESSION_FOUND);
2887
- }
2888
3177
  const {
2889
3178
  providerName,
2890
- stampWith
3179
+ stampWith,
3180
+ successPageDuration = 2000
2891
3181
  } = params;
2892
- const onOauthSuccess = async params => {
2893
- await addOauthProvider({
2894
- providerName: params.providerName,
2895
- oidcToken: params.oidcToken,
2896
- stampWith
2897
- });
2898
- pushPage({
2899
- key: "OAuth Provider Added",
2900
- content: jsxRuntime.jsx(Success.SuccessPage, {
2901
- text: `Successfully added ${params.providerName} OAuth provider!`,
2902
- duration: 3000,
2903
- onComplete: () => {
2904
- closeModal();
2905
- }
2906
- }),
2907
- preventBack: true,
2908
- showTitle: false
2909
- });
2910
- };
2911
- switch (providerName) {
2912
- case sdkTypes.OAuthProviders.DISCORD:
2913
- {
2914
- await handleDiscordOauth({
2915
- openInPage: false,
2916
- onOauthSuccess
2917
- });
2918
- break;
2919
- }
2920
- case sdkTypes.OAuthProviders.X:
2921
- {
2922
- await handleXOauth({
2923
- openInPage: false,
2924
- onOauthSuccess
2925
- });
2926
- break;
2927
- }
2928
- case sdkTypes.OAuthProviders.GOOGLE:
2929
- {
2930
- await handleGoogleOauth({
2931
- openInPage: false,
2932
- onOauthSuccess
2933
- });
2934
- break;
2935
- }
2936
- case sdkTypes.OAuthProviders.APPLE:
2937
- {
2938
- await handleAppleOauth({
2939
- openInPage: false,
2940
- onOauthSuccess
3182
+ const s = await getSession();
3183
+ const organizationId = params?.organizationId || s?.organizationId;
3184
+ if (!organizationId) {
3185
+ throw new sdkTypes.TurnkeyError("A session or passed in organization ID is required.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
3186
+ }
3187
+ const userId = params?.userId || s?.userId;
3188
+ if (!userId) {
3189
+ throw new sdkTypes.TurnkeyError("A user ID is required to add an OAuth provider.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
3190
+ }
3191
+ return new Promise(async (resolve, reject) => {
3192
+ try {
3193
+ const onOauthSuccess = async params => {
3194
+ await addOauthProvider({
3195
+ providerName: params.providerName,
3196
+ oidcToken: params.oidcToken,
3197
+ stampWith,
3198
+ organizationId,
3199
+ userId
2941
3200
  });
2942
- break;
2943
- }
2944
- case sdkTypes.OAuthProviders.FACEBOOK:
2945
- {
2946
- await handleFacebookOauth({
2947
- openInPage: false,
2948
- onOauthSuccess
3201
+ resolve();
3202
+ pushPage({
3203
+ key: "OAuth Provider Added",
3204
+ content: jsxRuntime.jsx(Success.SuccessPage, {
3205
+ text: `Successfully added ${params.providerName} OAuth provider!`,
3206
+ duration: successPageDuration,
3207
+ onComplete: () => {
3208
+ closeModal();
3209
+ }
3210
+ }),
3211
+ preventBack: true,
3212
+ showTitle: false
2949
3213
  });
2950
- break;
2951
- }
2952
- default:
2953
- {
2954
- throw new sdkTypes.TurnkeyError(`Unsupported OAuth provider: ${providerName}`, sdkTypes.TurnkeyErrorCodes.NOT_FOUND);
3214
+ };
3215
+ switch (providerName) {
3216
+ case sdkTypes.OAuthProviders.DISCORD:
3217
+ {
3218
+ return await handleDiscordOauth({
3219
+ openInPage: false,
3220
+ onOauthSuccess
3221
+ });
3222
+ }
3223
+ case sdkTypes.OAuthProviders.X:
3224
+ {
3225
+ return await handleXOauth({
3226
+ openInPage: false,
3227
+ onOauthSuccess
3228
+ });
3229
+ }
3230
+ case sdkTypes.OAuthProviders.GOOGLE:
3231
+ {
3232
+ return await handleGoogleOauth({
3233
+ openInPage: false,
3234
+ onOauthSuccess
3235
+ });
3236
+ }
3237
+ case sdkTypes.OAuthProviders.APPLE:
3238
+ {
3239
+ return await handleAppleOauth({
3240
+ openInPage: false,
3241
+ onOauthSuccess
3242
+ });
3243
+ }
3244
+ case sdkTypes.OAuthProviders.FACEBOOK:
3245
+ {
3246
+ return await handleFacebookOauth({
3247
+ openInPage: false,
3248
+ onOauthSuccess
3249
+ });
3250
+ }
3251
+ default:
3252
+ {
3253
+ throw new sdkTypes.TurnkeyError(`Unsupported OAuth provider: ${providerName}`, sdkTypes.TurnkeyErrorCodes.NOT_FOUND);
3254
+ }
2955
3255
  }
2956
- }
3256
+ } catch (error) {
3257
+ reject(error);
3258
+ }
3259
+ });
2957
3260
  }, [pushPage, masterConfig, client, session, user]);
2958
3261
  const handleConnectExternalWallet = react.useCallback(async params => {
2959
3262
  const {
2960
3263
  successPageDuration = 2000
2961
3264
  } = params || {};
2962
3265
  if (!client) throw new sdkTypes.TurnkeyError("Client is not initialized.", sdkTypes.TurnkeyErrorCodes.CLIENT_NOT_INITIALIZED);
2963
- if (!session) {
2964
- throw new sdkTypes.TurnkeyError("No active session found.", sdkTypes.TurnkeyErrorCodes.NO_SESSION_FOUND);
2965
- }
2966
3266
  if (!masterConfig?.walletConfig?.features?.connecting) {
2967
3267
  throw new sdkTypes.TurnkeyError("Wallet connecting is not enabled.", sdkTypes.TurnkeyErrorCodes.FEATURE_NOT_ENABLED);
2968
3268
  }
2969
3269
  const providers = await fetchWalletProviders();
2970
- pushPage({
2971
- key: "Connect wallet",
2972
- content: jsxRuntime.jsx(ConnectWallet.ConnectWalletModal, {
2973
- providers: providers,
2974
- successPageDuration: successPageDuration
2975
- })
3270
+ return new Promise((resolve, reject) => {
3271
+ pushPage({
3272
+ key: "Connect wallet",
3273
+ content: jsxRuntime.jsx(ConnectWallet.ConnectWalletModal, {
3274
+ providers: providers,
3275
+ successPageDuration: successPageDuration,
3276
+ onSuccess: () => {
3277
+ resolve();
3278
+ },
3279
+ onError: error => {
3280
+ reject(error);
3281
+ }
3282
+ })
3283
+ });
2976
3284
  });
2977
3285
  }, [pushPage, masterConfig, client, session, user]);
2978
3286
  const handleRemoveUserEmail = react.useCallback(async params => {
@@ -2981,8 +3289,10 @@ const ClientProvider = ({
2981
3289
  stampWith,
2982
3290
  userId
2983
3291
  } = params || {};
2984
- if (!session) {
2985
- throw new sdkTypes.TurnkeyError("No active session found.", sdkTypes.TurnkeyErrorCodes.NO_SESSION_FOUND);
3292
+ const s = await getSession();
3293
+ const organizationId = params?.organizationId || s?.organizationId;
3294
+ if (!organizationId) {
3295
+ throw new sdkTypes.TurnkeyError("A session or passed in organization ID is required.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
2986
3296
  }
2987
3297
  try {
2988
3298
  return new Promise((resolve, reject) => {
@@ -3001,7 +3311,10 @@ const ClientProvider = ({
3001
3311
  },
3002
3312
  onError: error => {
3003
3313
  reject(error);
3004
- }
3314
+ },
3315
+ ...(organizationId !== undefined && {
3316
+ organizationId
3317
+ })
3005
3318
  }),
3006
3319
  showTitle: false,
3007
3320
  preventBack: true
@@ -3020,8 +3333,10 @@ const ClientProvider = ({
3020
3333
  stampWith,
3021
3334
  userId
3022
3335
  } = params || {};
3023
- if (!session) {
3024
- throw new sdkTypes.TurnkeyError("No active session found.", sdkTypes.TurnkeyErrorCodes.NO_SESSION_FOUND);
3336
+ const s = await getSession();
3337
+ const organizationId = params?.organizationId || s?.organizationId;
3338
+ if (!organizationId) {
3339
+ throw new sdkTypes.TurnkeyError("A session or passed in organization ID is required.", sdkTypes.TurnkeyErrorCodes.INVALID_REQUEST);
3025
3340
  }
3026
3341
  try {
3027
3342
  return new Promise((resolve, reject) => {
@@ -3040,7 +3355,10 @@ const ClientProvider = ({
3040
3355
  },
3041
3356
  onError: error => {
3042
3357
  reject(error);
3043
- }
3358
+ },
3359
+ ...(organizationId !== undefined && {
3360
+ organizationId
3361
+ })
3044
3362
  }),
3045
3363
  showTitle: false,
3046
3364
  preventBack: true
@@ -3089,23 +3407,29 @@ const ClientProvider = ({
3089
3407
  * only trigger `refreshWallets()` once.
3090
3408
  *
3091
3409
  * Defining the debounced function outside of the `useEffect` ensures all event
3092
- * listeners in `initializeProviders` share the same instance, instead of creating
3410
+ * listeners in `initializeWalletProviderListeners` share the same instance, instead of creating
3093
3411
  * a new one on every render.
3094
3412
  */
3095
3413
  const debouncedRefreshWallets = utils.useDebouncedCallback(refreshWallets, 100);
3414
+ const debouncedFetchWalletProviders = utils.useDebouncedCallback(fetchWalletProviders, 100);
3096
3415
  react.useEffect(() => {
3097
3416
  if (!client) return;
3098
- const handleRefreshWallets = async () => {
3417
+ const handleUpdateState = async () => {
3099
3418
  // we only refresh the wallets if there is an active session
3100
3419
  // this is needed because a disconnect event can occur
3101
3420
  // while the user is unauthenticated
3421
+ //
3422
+ // WalletProviders state is updated regardless of session state
3102
3423
  if (session) {
3103
- // this will update our walletProvider state
3424
+ // this updates both the wallets and walletProviders state
3104
3425
  await debouncedRefreshWallets();
3426
+ } else {
3427
+ // this updates only the walletProviders state
3428
+ await debouncedFetchWalletProviders();
3105
3429
  }
3106
3430
  };
3107
3431
  let cleanup = () => {};
3108
- initializeWalletProviderListeners(walletProviders, handleRefreshWallets).then(fn => {
3432
+ initializeWalletProviderListeners(walletProviders, handleUpdateState).then(fn => {
3109
3433
  cleanup = fn;
3110
3434
  }).catch(err => {
3111
3435
  console.error("Failed to init providers:", err);
@@ -3113,7 +3437,7 @@ const ClientProvider = ({
3113
3437
  return () => {
3114
3438
  cleanup();
3115
3439
  };
3116
- }, [client, walletProviders]);
3440
+ }, [client, walletProviders, session]);
3117
3441
  react.useEffect(() => {
3118
3442
  // authState must be consistent with session state. We found during testing that there are cases where the session and authState can be out of sync in very rare edge cases.
3119
3443
  // This will ensure that they are always in sync and remove the need to setAuthState manually in other places.
@@ -3128,6 +3452,11 @@ const ClientProvider = ({
3128
3452
  if (!client || !masterConfig) return;
3129
3453
  completeRedirectOauth().finally(() => {
3130
3454
  clearSessionTimeouts();
3455
+ // if auth or wallet connecting features are enabled, we want to fetch
3456
+ // the wallet providers to set the state
3457
+ if (masterConfig.walletConfig?.features?.auth || masterConfig.walletConfig?.features?.connecting) {
3458
+ fetchWalletProviders();
3459
+ }
3131
3460
  initializeSessions().finally(() => {
3132
3461
  // Set the client state to ready only after all initializations are done.
3133
3462
  setClientState(base.ClientState.Ready);
@@ -3145,6 +3474,7 @@ const ClientProvider = ({
3145
3474
  authState,
3146
3475
  user,
3147
3476
  wallets,
3477
+ walletProviders,
3148
3478
  config: masterConfig,
3149
3479
  httpClient: client?.httpClient,
3150
3480
  createPasskey,