@openfort/react 0.0.1 → 0.0.3

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 (29) hide show
  1. package/README.md +18 -30
  2. package/build/components/{OpenfortKit → Openfort}/OpenfortKit.d.ts +6 -6
  3. package/build/components/{OpenfortKit → Openfort}/context.d.ts +2 -2
  4. package/build/components/{OpenfortKit → Openfort}/types.d.ts +4 -4
  5. package/build/components/Pages/Providers/index.d.ts +1 -1
  6. package/build/hooks/openfort/auth/status.d.ts +3 -3
  7. package/build/hooks/openfort/auth/useAuthCallback.d.ts +17 -32
  8. package/build/hooks/openfort/auth/useCreateWalletPostAuth.d.ts +13 -2
  9. package/build/hooks/openfort/auth/useEmailAuth.d.ts +16 -6
  10. package/build/hooks/openfort/auth/useGuestAuth.d.ts +5 -4
  11. package/build/hooks/openfort/auth/useOAuth.d.ts +21 -7
  12. package/build/hooks/openfort/auth/useSignOut.d.ts +2 -2
  13. package/build/hooks/openfort/auth/useWalletAuth.d.ts +4 -4
  14. package/build/hooks/openfort/hookConsistency.d.ts +3 -3
  15. package/build/hooks/openfort/useProviders.d.ts +1 -1
  16. package/build/hooks/openfort/useStatus.d.ts +6 -9
  17. package/build/hooks/openfort/useUser.d.ts +1 -0
  18. package/build/hooks/openfort/useWallet.d.ts +1 -0
  19. package/build/hooks/openfort/useWallets.d.ts +34 -12
  20. package/build/index.d.ts +5 -4
  21. package/build/index.es.js +615 -433
  22. package/build/index.es.js.map +1 -1
  23. package/build/openfort/OpenfortProvider.d.ts +2 -2
  24. package/build/openfort/useOpenfort.d.ts +1 -1
  25. package/build/types.d.ts +10 -9
  26. package/build/version.d.ts +1 -1
  27. package/package.json +3 -3
  28. package/build/openfort/OpenfortProvider_old.d.ts +0 -0
  29. /package/build/components/{OpenfortKit → Openfort}/useOpenfortKit.d.ts +0 -0
package/build/index.es.js CHANGED
@@ -5,7 +5,7 @@ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
5
5
  import React, { createContext, useEffect, useState, useMemo, useRef, useCallback, createElement, useLayoutEffect } from 'react';
6
6
  import { detect } from 'detect-browser';
7
7
  import { Buffer } from 'buffer';
8
- import { Openfort as Openfort$1, RecoveryMethod, EmbeddedState, AuthActionRequiredActions, OAuthProvider, ShieldAuthType, MissingRecoveryPasswordError } from '@openfort/openfort-js';
8
+ import { Openfort as Openfort$1, RecoveryMethod, EmbeddedState, AuthActionRequiredActions, OAuthProvider, ShieldAuthType, MissingRecoveryPasswordError, AccountTypeEnum, ChainTypeEnum } from '@openfort/openfort-js';
9
9
  export { RecoveryMethod } from '@openfort/openfort-js';
10
10
  import { polygonAmoy } from 'viem/chains';
11
11
  import { motion, AnimatePresence, MotionConfig } from 'framer-motion';
@@ -20,17 +20,18 @@ import { normalize } from 'viem/ens';
20
20
  import { AxiosError } from 'axios';
21
21
  import { createSiweMessage } from 'viem/siwe';
22
22
  import { signMessage } from '@wagmi/core';
23
- import { useQueryClient } from '@tanstack/react-query';
23
+ import { useQuery, useQueryClient } from '@tanstack/react-query';
24
24
 
25
- const OPENFORTKIT_VERSION = '0.0.1';
25
+ const OPENFORTKIT_VERSION = '0.0.3';
26
26
 
27
- var OpenfortKitErrorType;
28
- (function (OpenfortKitErrorType) {
29
- OpenfortKitErrorType["AUTHENTICATION_ERROR"] = "AUTHENTICATION_ERROR";
30
- })(OpenfortKitErrorType || (OpenfortKitErrorType = {}));
31
- class OpenfortKitError extends Error {
27
+ var OpenfortErrorType;
28
+ (function (OpenfortErrorType) {
29
+ OpenfortErrorType["AUTHENTICATION_ERROR"] = "AUTHENTICATION_ERROR";
30
+ OpenfortErrorType["WALLET_ERROR"] = "WALLET_ERROR";
31
+ })(OpenfortErrorType || (OpenfortErrorType = {}));
32
+ class OpenfortError extends Error {
32
33
  constructor(message, type, data) {
33
- if ((data === null || data === void 0 ? void 0 : data.error) instanceof OpenfortKitError) {
34
+ if ((data === null || data === void 0 ? void 0 : data.error) instanceof OpenfortError) {
34
35
  super(data.error.message);
35
36
  this.data = data.error.data;
36
37
  this.type = data.error.type;
@@ -45,7 +46,7 @@ class OpenfortKitError extends Error {
45
46
  }
46
47
  this.type = type;
47
48
  this.data = data || {};
48
- this.name = 'OpenfortKitError';
49
+ this.name = 'OpenfortError';
49
50
  }
50
51
  }
51
52
 
@@ -108,7 +109,7 @@ const defaultConfig = ({ appName = 'OpenfortKit', appIcon, appDescription, appUr
108
109
  walletConnectProjectId,
109
110
  coinbaseWalletPreference,
110
111
  });
111
- console.log("OPENFORTKIT CHAINS", chains, transports);
112
+ // console.log("OPENFORTKIT CHAINS", chains, transports);
112
113
  const config = {
113
114
  ...props,
114
115
  chains,
@@ -918,10 +919,10 @@ function useConnect({ ...props } = {}) {
918
919
 
919
920
  const Context$1 = createContext(null);
920
921
 
921
- const useOpenfort = () => {
922
+ const useOpenfortCore = () => {
922
923
  const context = React.useContext(Context$1);
923
924
  if (!context)
924
- throw Error('useOpenfortContext Hook must be inside OpenfortProvider.');
925
+ throw Error('useOpenfortContext Hook must be inside CoreOpenfortProvider.');
925
926
  return context;
926
927
  };
927
928
 
@@ -932,7 +933,7 @@ function useIsMounted() {
932
933
  }
933
934
 
934
935
  const useConnectCallback = ({ onConnect, onDisconnect, }) => {
935
- const { user } = useOpenfort();
936
+ const { user } = useOpenfortCore();
936
937
  const { address, connector } = useAccount();
937
938
  const hasAddress = !!address;
938
939
  const [isConnected, setIsConnected] = useState(false);
@@ -990,7 +991,7 @@ const ConnectCallback = ({ onConnect, onDisconnect }) => {
990
991
  });
991
992
  return null;
992
993
  };
993
- const OpenfortProvider = ({ children, debugMode, onConnect, onDisconnect, ...openfortProps }) => {
994
+ const CoreOpenfortProvider = ({ children, debugMode, onConnect, onDisconnect, ...openfortProps }) => {
994
995
  const log = debugMode ? console.log : () => { };
995
996
  const { connectors, connect, reset } = useConnect();
996
997
  const { address } = useAccount();
@@ -1002,7 +1003,7 @@ const OpenfortProvider = ({ children, debugMode, onConnect, onDisconnect, ...ope
1002
1003
  const openfort = useMemo(() => {
1003
1004
  log('Creating Openfort instance.');
1004
1005
  if (!openfortProps.baseConfiguration.publishableKey)
1005
- throw Error('OpenfortProvider requires a publishableKey to be set in the baseConfiguration.');
1006
+ throw Error('CoreOpenfortProvider requires a publishableKey to be set in the baseConfiguration.');
1006
1007
  const newClient = createOpenfortClient(openfortProps);
1007
1008
  return newClient;
1008
1009
  }, []);
@@ -4477,7 +4478,7 @@ function usePrevious(value, initial) {
4477
4478
  return ref.current.previous;
4478
4479
  }
4479
4480
 
4480
- const useWallet = (id) => {
4481
+ const useWallet$1 = (id) => {
4481
4482
  const wallets = useWallets$1();
4482
4483
  const wallet = wallets.find((c) => c.id === id);
4483
4484
  if (!wallet)
@@ -4774,7 +4775,7 @@ const Modal = ({ open, pages, pageId, positionInside, inline, demo, onClose, onB
4774
4775
  const context = useOpenfortKit();
4775
4776
  const themeContext = useThemeContext();
4776
4777
  const mobile = isMobile();
4777
- const wallet = useWallet((_a = context.connector) === null || _a === void 0 ? void 0 : _a.id);
4778
+ const wallet = useWallet$1((_a = context.connector) === null || _a === void 0 ? void 0 : _a.id);
4778
4779
  const walletInfo = {
4779
4780
  name: wallet === null || wallet === void 0 ? void 0 : wallet.name,
4780
4781
  shortName: (_b = wallet === null || wallet === void 0 ? void 0 : wallet.shortName) !== null && _b !== void 0 ? _b : wallet === null || wallet === void 0 ? void 0 : wallet.name,
@@ -7265,7 +7266,7 @@ CustomQRCode.displayName = 'CustomQRCode';
7265
7266
  const DownloadApp = () => {
7266
7267
  var _a, _b, _c;
7267
7268
  const context = useOpenfortKit();
7268
- const wallet = useWallet(context.connector.id);
7269
+ const wallet = useWallet$1(context.connector.id);
7269
7270
  const locales = useLocales({
7270
7271
  CONNECTORNAME: wallet === null || wallet === void 0 ? void 0 : wallet.name,
7271
7272
  });
@@ -9199,7 +9200,7 @@ var Wallet = ({ ...props }) => {
9199
9200
  };
9200
9201
 
9201
9202
  function useProviders() {
9202
- const { user } = useOpenfort();
9203
+ const { user } = useOpenfortCore();
9203
9204
  const { uiConfig: options } = useOpenfortKit();
9204
9205
  const allProviders = (options === null || options === void 0 ? void 0 : options.authProviders) || [];
9205
9206
  const providers = allProviders.filter(p => p !== AuthProvider.GUEST) || [];
@@ -9314,7 +9315,7 @@ const AddLinkedProviderButton = () => {
9314
9315
  return (jsx(LinkedProviderButton, { disabled: unlinkedProviders.length === 0, onClick: () => setRoute(routes.PROVIDERS), children: "+" }));
9315
9316
  };
9316
9317
  const LinkedProviders = () => {
9317
- const { user } = useOpenfort();
9318
+ const { user } = useOpenfortCore();
9318
9319
  if (!user || !user.linkedAccounts) {
9319
9320
  return (jsx("div", { children: jsx("p", { children: "No linked providers" }) }));
9320
9321
  }
@@ -9338,7 +9339,7 @@ const Profile = ({ closeModal }) => {
9338
9339
  //watch: true,
9339
9340
  });
9340
9341
  const [shouldDisconnect, setShouldDisconnect] = useState(false);
9341
- const { logout, user } = useOpenfort();
9342
+ const { logout, user } = useOpenfortCore();
9342
9343
  useEffect(() => {
9343
9344
  // if (!isConnected) context.setOpen(false);
9344
9345
  }, [isConnected]);
@@ -9747,7 +9748,7 @@ const createSIWEMessage = (address, nonce, chainId) => createSiweMessage({
9747
9748
  });
9748
9749
 
9749
9750
  function useConnectWithSiwe() {
9750
- const { client, user, updateUser } = useOpenfort();
9751
+ const { client, user, updateUser } = useOpenfortCore();
9751
9752
  const { log } = useOpenfortKit();
9752
9753
  const { address } = useAccount();
9753
9754
  const chainId = useChainId();
@@ -9797,7 +9798,7 @@ function useConnectWithSiwe() {
9797
9798
  onError && onError("Failed to connect with SIWE");
9798
9799
  }
9799
9800
  }
9800
- }, [client, user, updateUser, log]);
9801
+ }, [client, user, updateUser, log, address, chainId, config, connector]);
9801
9802
  return connectWithSiwe;
9802
9803
  }
9803
9804
 
@@ -9841,7 +9842,7 @@ const contentVariants$1 = {
9841
9842
  };
9842
9843
  const ConnectWithInjector = ({ switchConnectMethod, forceState }) => {
9843
9844
  var _a, _b, _c, _d, _e, _f, _g, _h;
9844
- const openfort = useOpenfort();
9845
+ const openfort = useOpenfortCore();
9845
9846
  const { log, setOpen } = useOpenfortKit();
9846
9847
  const { isConnected } = useAccount();
9847
9848
  const { disconnect } = useDisconnect();
@@ -9927,7 +9928,7 @@ const ConnectWithInjector = ({ switchConnectMethod, forceState }) => {
9927
9928
  });
9928
9929
  const { triggerResize, connector: c } = useOpenfortKit();
9929
9930
  const id = c.id;
9930
- const wallet = useWallet(id);
9931
+ const wallet = useWallet$1(id);
9931
9932
  const walletInfo = {
9932
9933
  name: wallet === null || wallet === void 0 ? void 0 : wallet.name,
9933
9934
  shortName: (_a = wallet === null || wallet === void 0 ? void 0 : wallet.shortName) !== null && _a !== void 0 ? _a : wallet === null || wallet === void 0 ? void 0 : wallet.name,
@@ -10055,7 +10056,7 @@ const ConnectWithQRCode = ({ switchConnectMethod }) => {
10055
10056
  var _a, _b, _c, _d, _e, _f, _g, _h;
10056
10057
  const context = useOpenfortKit();
10057
10058
  const id = context.connector.id;
10058
- const wallet = useWallet(context.connector.id);
10059
+ const wallet = useWallet$1(context.connector.id);
10059
10060
  const { open: openW3M, isOpen: isOpenW3M } = useWalletConnectModal();
10060
10061
  const { connect: { getUri }, } = useWeb3();
10061
10062
  const wcUri = getUri(id);
@@ -10179,11 +10180,23 @@ const Loader = ({ reason, isLoading = true, icon }) => {
10179
10180
  useEffect(() => {
10180
10181
  return () => triggerResize();
10181
10182
  }, []);
10183
+ const renderLogo = () => {
10184
+ if (icon) {
10185
+ return icon;
10186
+ }
10187
+ if (options === null || options === void 0 ? void 0 : options.logo) {
10188
+ if (typeof options.logo === 'string') {
10189
+ return jsx("img", { src: options.logo, alt: "Logo", style: { width: '100%' } });
10190
+ }
10191
+ return options.logo;
10192
+ }
10193
+ return jsx(Logos.Openfort, {});
10194
+ };
10182
10195
  return (jsxs(Fragment, { children: [jsx(ConnectingContainer, { children: jsx(ConnectingAnimation, { "$success": !isLoading, children: jsx(SquircleSpinner, { logo: jsx("div", { style: {
10183
10196
  padding: '12px',
10184
10197
  position: 'relative',
10185
10198
  width: '100%',
10186
- }, children: icon || (options === null || options === void 0 ? void 0 : options.logo) || jsx(Logos.Openfort, {}) }), connecting: isLoading }) }) }), jsx(TextWrapper, { children: isLoading ? (jsxs(Fragment, { children: [jsx(ModalH1, { children: "Loading, please wait" }), jsx(ModalBody, { children: reason })] })) : (jsxs(ModalH1, { "$valid": true, children: [jsx(TickIcon, {}), " ", reason] })) })] }));
10199
+ }, children: renderLogo() }), connecting: isLoading }) }) }), jsx(TextWrapper, { children: isLoading ? (jsxs(Fragment, { children: [jsx(ModalH1, { children: "Loading, please wait" }), jsx(ModalBody, { children: reason })] })) : (jsxs(ModalH1, { "$valid": true, children: [jsx(TickIcon, {}), " ", reason] })) })] }));
10187
10200
  };
10188
10201
 
10189
10202
  const states$1 = {
@@ -10193,7 +10206,7 @@ const states$1 = {
10193
10206
  };
10194
10207
  const ConnectWithOAuth = ({}) => {
10195
10208
  const { connector, setRoute, log } = useOpenfortKit();
10196
- const { client, user } = useOpenfort();
10209
+ const { client, user } = useOpenfortCore();
10197
10210
  const [status, setStatus] = useState(states$1.INIT);
10198
10211
  useEffect(() => {
10199
10212
  (async () => {
@@ -10290,7 +10303,7 @@ const states = {
10290
10303
  };
10291
10304
  const ConnectUsing = () => {
10292
10305
  const context = useOpenfortKit();
10293
- const wallet = useWallet(context.connector.id);
10306
+ const wallet = useWallet$1(context.connector.id);
10294
10307
  // If cannot be scanned, display injector flow, which if extension is not installed will show CTA to install it
10295
10308
  const isQrCode = !(wallet === null || wallet === void 0 ? void 0 : wallet.isInstalled) && (wallet === null || wallet === void 0 ? void 0 : wallet.getWalletConnectDeeplink);
10296
10309
  // For OAuth connectors, we don't need to show the injector flow
@@ -10455,7 +10468,7 @@ const EmailLogin = () => {
10455
10468
  const [email, setEmail] = React.useState("");
10456
10469
  const [password, setPassword] = React.useState("");
10457
10470
  const { setRoute, log } = useOpenfortKit();
10458
- const { client } = useOpenfort();
10471
+ const { client } = useOpenfortCore();
10459
10472
  const [loginLoading, setLoginLoading] = React.useState(false);
10460
10473
  const [loginError, setLoginError] = React.useState(false);
10461
10474
  const handleSubmit = async () => {
@@ -10498,7 +10511,7 @@ const EmailSignup = () => {
10498
10511
  const [password, setPassword] = React.useState("");
10499
10512
  const [username, setUsername] = React.useState("");
10500
10513
  const { setRoute, triggerResize, log, uiConfig: options } = useOpenfortKit();
10501
- const { client } = useOpenfort();
10514
+ const { client } = useOpenfortCore();
10502
10515
  const [signupLoading, setSignupLoading] = React.useState(false);
10503
10516
  const [signupError, setSignupError] = React.useState(false);
10504
10517
  const handleSubmit = async () => {
@@ -10544,7 +10557,7 @@ const EmailSignup = () => {
10544
10557
  };
10545
10558
 
10546
10559
  const EmailVerification = () => {
10547
- const { client } = useOpenfort();
10560
+ const { client } = useOpenfortCore();
10548
10561
  const { setRoute, log } = useOpenfortKit();
10549
10562
  const [loading, setLoading] = useState(true);
10550
10563
  const [shouldSendEmailVerification, setShouldSendEmailVerification] = useState(false);
@@ -10637,7 +10650,7 @@ const EmailVerification = () => {
10637
10650
  const RequestEmail = () => {
10638
10651
  const [email, setEmail] = React.useState("");
10639
10652
  const { log, triggerResize } = useOpenfortKit();
10640
- const { client } = useOpenfort();
10653
+ const { client } = useOpenfortCore();
10641
10654
  const [loading, setLoading] = React.useState(false);
10642
10655
  const [message, setMessage] = React.useState("");
10643
10656
  const [error, setError] = React.useState("");
@@ -10694,7 +10707,7 @@ const ResetPassword = () => {
10694
10707
  const url = new URL(fixedUrl);
10695
10708
  const [password, setPassword] = React.useState("");
10696
10709
  const { setRoute, triggerResize, log } = useOpenfortKit();
10697
- const { client, updateUser } = useOpenfort();
10710
+ const { client, updateUser } = useOpenfortCore();
10698
10711
  const [loading, setLoading] = React.useState(false);
10699
10712
  const email = url.searchParams.get("email");
10700
10713
  const handleSubmit = async () => {
@@ -10769,7 +10782,7 @@ const LinkEmail = () => {
10769
10782
  const [email, setEmail] = React.useState("");
10770
10783
  const [password, setPassword] = React.useState("");
10771
10784
  const { setRoute, triggerResize, log } = useOpenfortKit();
10772
- const { client, updateUser } = useOpenfort();
10785
+ const { client, updateUser } = useOpenfortCore();
10773
10786
  const [loginLoading, setLoginLoading] = React.useState(false);
10774
10787
  const [loginError, setLoginError] = React.useState(false);
10775
10788
  const handleSubmit = async () => {
@@ -10807,7 +10820,7 @@ const LinkEmail = () => {
10807
10820
 
10808
10821
  const Loading = () => {
10809
10822
  const { setRoute, walletConfig } = useOpenfortKit();
10810
- const { isLoading, user, needsRecovery } = useOpenfort();
10823
+ const { isLoading, user, needsRecovery } = useOpenfortCore();
10811
10824
  const { address } = useAccount();
10812
10825
  const [isFirstFrame, setIsFirstFrame] = React.useState(true);
10813
10826
  const [retryCount, setRetryCount] = React.useState(0);
@@ -10851,7 +10864,7 @@ const ProviderButton = ({ children, icon, onClick }) => {
10851
10864
  return (jsx(ProvidersButton, { children: jsxs(Button, { onClick: onClick, children: [jsx(ProviderLabel, { children: children }), jsx(ProviderIcon$1, { children: icon })] }) }));
10852
10865
  };
10853
10866
  const GuestButton = () => {
10854
- const { signUpGuest } = useOpenfort();
10867
+ const { signUpGuest } = useOpenfortCore();
10855
10868
  const { setRoute } = useOpenfortKit();
10856
10869
  const handleClick = () => {
10857
10870
  signUpGuest();
@@ -10865,7 +10878,7 @@ const WalletButton = () => {
10865
10878
  };
10866
10879
  const EmailButton = () => {
10867
10880
  const { setRoute } = useOpenfortKit();
10868
- const { user } = useOpenfort();
10881
+ const { user } = useOpenfortCore();
10869
10882
  return jsx(ProviderButton, { onClick: () => setRoute(user ? routes.LINK_EMAIL : routes.EMAIL_LOGIN), icon: jsx(EmailIcon, {}), children: "Email" });
10870
10883
  };
10871
10884
  const AuthProviderButton = ({ provider, title = provider + " login", icon }) => {
@@ -10896,7 +10909,7 @@ const ProviderButtonSwitch = ({ provider }) => {
10896
10909
  };
10897
10910
  // This accounts for the case where the user has an address but no user, which can happen if the user has not signed up yet, but logged in with a wallet
10898
10911
  const AddressButNoUserCase = () => {
10899
- const { updateUser } = useOpenfort();
10912
+ const { updateUser } = useOpenfortCore();
10900
10913
  const { disconnect } = useDisconnect();
10901
10914
  useEffect(() => {
10902
10915
  updateUser()
@@ -10916,7 +10929,7 @@ const SocialProvidersButton = () => {
10916
10929
  };
10917
10930
  const Providers = () => {
10918
10931
  const maxProviders = 4;
10919
- const { user } = useOpenfort();
10932
+ const { user } = useOpenfortCore();
10920
10933
  const { address } = useAccount();
10921
10934
  const { allProviders, availableProviders } = useProviders();
10922
10935
  if (address && !user) {
@@ -10928,28 +10941,46 @@ const Providers = () => {
10928
10941
  return (jsxs(PageContent, { children: [(filteredProviders).map((auth) => (jsx(ProviderButtonSwitch, { provider: auth }, auth))), hasExcessProviders && jsx(SocialProvidersButton, {}), jsx(PoweredByFooter, { showDisclaimer: true })] }));
10929
10942
  };
10930
10943
 
10931
- const mapStatus = (status) => {
10932
- return {
10933
- isLoading: status.status === 'loading',
10934
- isError: status.status === 'error',
10935
- isSuccess: status.status === 'success',
10936
- error: status.error
10937
- };
10944
+ const onSuccess = ({ hookOptions, options, data, }) => {
10945
+ var _a, _b, _c, _d;
10946
+ (_a = hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.onSuccess) === null || _a === void 0 ? void 0 : _a.call(hookOptions, data);
10947
+ (_b = hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.onSettled) === null || _b === void 0 ? void 0 : _b.call(hookOptions, data, null);
10948
+ (_c = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _c === void 0 ? void 0 : _c.call(options, data);
10949
+ (_d = options === null || options === void 0 ? void 0 : options.onSettled) === null || _d === void 0 ? void 0 : _d.call(options, data, null);
10950
+ return data;
10951
+ };
10952
+ const onError = ({ hookOptions, options, error, }) => {
10953
+ var _a, _b, _c, _d;
10954
+ (_a = hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.onError) === null || _a === void 0 ? void 0 : _a.call(hookOptions, error);
10955
+ (_b = hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.onSettled) === null || _b === void 0 ? void 0 : _b.call(hookOptions, null, error);
10956
+ (_c = options === null || options === void 0 ? void 0 : options.onError) === null || _c === void 0 ? void 0 : _c.call(options, error);
10957
+ (_d = options === null || options === void 0 ? void 0 : options.onSettled) === null || _d === void 0 ? void 0 : _d.call(options, null, error);
10958
+ if ((hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.throwOnError) || (options === null || options === void 0 ? void 0 : options.throwOnError))
10959
+ throw error;
10960
+ return { error };
10938
10961
  };
10939
10962
 
10940
- const createOpenfortWallet = ({ connector, isEmbedded, address, }) => ({
10963
+ const createOpenfortWallet = ({ address, }) => ({
10941
10964
  connectorType: "embedded",
10942
10965
  walletClientType: "openfort",
10943
- address: isEmbedded ? address : undefined,
10966
+ address,
10944
10967
  id: embeddedWalletId,
10945
10968
  isAvailable: true,
10946
- isActive: isEmbedded && (connector === null || connector === void 0 ? void 0 : connector.id) === embeddedWalletId,
10947
10969
  });
10948
- function useWallets() {
10949
- const { user, embeddedState, client } = useOpenfort();
10950
- const { walletConfig, log, setOpen, setRoute, setConnector, uiConfig: openfortKitOptions } = useOpenfortKit();
10970
+ const mapStatus$1 = (status) => {
10971
+ return {
10972
+ error: status.error,
10973
+ isError: status.status === 'error',
10974
+ isSuccess: status.status === 'success',
10975
+ isCreating: status.status === 'creating',
10976
+ isConnecting: status.status === 'connecting',
10977
+ };
10978
+ };
10979
+ function useWallets(hookOptions = {}) {
10980
+ const { user, embeddedState, client } = useOpenfortCore();
10981
+ const { walletConfig, log, setOpen, setRoute, setConnector, uiConfig } = useOpenfortKit();
10951
10982
  const { connector, isConnected, address } = useAccount();
10952
- const chain = useChainId();
10983
+ const chainId = useChainId();
10953
10984
  const deviceWallets = useWallets$1(); // TODO: Map wallets object to be the same as wallets
10954
10985
  const { disconnect } = useDisconnect();
10955
10986
  const [status, setStatus] = useState({
@@ -10958,20 +10989,25 @@ function useWallets() {
10958
10989
  const [connectToConnector, setConnectToConnector] = useState(undefined);
10959
10990
  const { connect } = useConnect$1({
10960
10991
  mutation: {
10961
- onError: (error) => {
10962
- console.error("Error connecting ---", error);
10992
+ onError: (e) => {
10993
+ console.error("Error connecting ---", e);
10994
+ const error = new OpenfortError("Failed to connect with wallet: ", OpenfortErrorType.AUTHENTICATION_ERROR, e);
10963
10995
  setStatus({
10964
10996
  status: 'error',
10965
- error: new OpenfortKitError("Failed to connect with wallet: ", OpenfortKitErrorType.AUTHENTICATION_ERROR, error),
10997
+ error,
10998
+ });
10999
+ onError({
11000
+ error,
11001
+ options: hookOptions,
10966
11002
  });
10967
11003
  },
10968
11004
  onSuccess: (data) => {
10969
11005
  setConnectToConnector(undefined);
10970
- console.log("Connected with wallet", data, connectToConnector);
11006
+ log("Connected with wallet", data, connectToConnector);
10971
11007
  if ((connectToConnector === null || connectToConnector === void 0 ? void 0 : connectToConnector.address) && !data.accounts.some((a) => a === connectToConnector.address)) {
10972
11008
  setStatus({
10973
11009
  status: 'error',
10974
- error: new OpenfortKitError("Failed to connect with wallet: Address mismatch", OpenfortKitErrorType.AUTHENTICATION_ERROR),
11010
+ error: new OpenfortError("Failed to connect with wallet: Address mismatch", OpenfortErrorType.AUTHENTICATION_ERROR),
10975
11011
  });
10976
11012
  disconnect();
10977
11013
  return;
@@ -10984,8 +11020,32 @@ function useWallets() {
10984
11020
  });
10985
11021
  const usesEmbeddedWallet = user && walletConfig;
10986
11022
  const isEmbedded = embeddedState === EmbeddedState.READY;
11023
+ const { data: embeddedWallets, refetch } = useQuery({
11024
+ queryKey: ['openfortEmbeddedWalletList'],
11025
+ queryFn: () => !!user ? client.embeddedWallet.list() : Promise.resolve([]),
11026
+ });
11027
+ useEffect(() => {
11028
+ log("Refetching embedded wallets");
11029
+ refetch();
11030
+ }, [!!user, refetch]);
11031
+ const getEncryptionSession = useCallback(async () => {
11032
+ if (!walletConfig || !walletConfig.createEncryptedSessionEndpoint) {
11033
+ throw new Error("No createEncryptedSessionEndpoint set in walletConfig");
11034
+ }
11035
+ const resp = await fetch(walletConfig.createEncryptedSessionEndpoint, {
11036
+ method: "POST",
11037
+ headers: {
11038
+ "Content-Type": "application/json",
11039
+ },
11040
+ });
11041
+ if (!resp.ok) {
11042
+ throw new Error("Failed to create encryption session");
11043
+ }
11044
+ const respJSON = await resp.json();
11045
+ return respJSON.session;
11046
+ }, [walletConfig]);
10987
11047
  const wallets = useMemo(() => {
10988
- const linkedWallets = user ? user.linkedAccounts
11048
+ const userWallets = user ? user.linkedAccounts
10989
11049
  .filter((a) => a.provider === AuthProvider.WALLET)
10990
11050
  .map((a) => {
10991
11051
  const wallet = deviceWallets.find((c) => c.connector.id === a.walletClientType);
@@ -10995,19 +11055,18 @@ function useWallets() {
10995
11055
  walletClientType: a.walletClientType,
10996
11056
  id: (wallet === null || wallet === void 0 ? void 0 : wallet.id) || a.walletClientType || "unknown",
10997
11057
  isAvailable: !!wallet,
10998
- isActive: (connector === null || connector === void 0 ? void 0 : connector.id) === a.walletClientType && address === a.address,
10999
11058
  };
11000
11059
  }) : [];
11001
- // TODO: List user wallets with embeddedWallet.list
11002
- if (usesEmbeddedWallet) {
11003
- linkedWallets.push(createOpenfortWallet({
11004
- connector,
11005
- isEmbedded,
11006
- address
11060
+ embeddedWallets === null || embeddedWallets === void 0 ? void 0 : embeddedWallets.forEach((wallet) => {
11061
+ // Remove duplicates (different chain ids)
11062
+ if (userWallets.find(w => w.address === (wallet.address)))
11063
+ return;
11064
+ userWallets.push(createOpenfortWallet({
11065
+ address: wallet.address,
11007
11066
  }));
11008
- }
11009
- return linkedWallets;
11010
- }, [user, address, connector === null || connector === void 0 ? void 0 : connector.id]);
11067
+ });
11068
+ return userWallets;
11069
+ }, [user === null || user === void 0 ? void 0 : user.linkedAccounts, embeddedWallets]);
11011
11070
  const activeWallet = isConnected && connector ? wallets.find((w) => w.isActive) : undefined;
11012
11071
  useEffect(() => {
11013
11072
  if (connectToConnector)
@@ -11022,7 +11081,7 @@ function useWallets() {
11022
11081
  const wallet = deviceWallets.find(c => c.id === optionsObject.connector);
11023
11082
  if (!wallet) {
11024
11083
  log("Connector not found", connector);
11025
- return { error: "Connector not found" };
11084
+ return { error: new OpenfortError("Connector not found", OpenfortErrorType.WALLET_ERROR) };
11026
11085
  }
11027
11086
  log("Connecting to", wallet.connector);
11028
11087
  connector = wallet.connector;
@@ -11032,7 +11091,7 @@ function useWallets() {
11032
11091
  }
11033
11092
  if (!connector) {
11034
11093
  log("Connector not found", deviceWallets, optionsObject.connector);
11035
- return { error: "Connector not found" };
11094
+ return { error: new OpenfortError("Connector not found", OpenfortErrorType.WALLET_ERROR) };
11036
11095
  }
11037
11096
  if ((activeWallet === null || activeWallet === void 0 ? void 0 : activeWallet.id) === connector.id && address === optionsObject.address) {
11038
11097
  log(`Already connected to ${connector.id} with address ${address}, skipping connection`);
@@ -11058,7 +11117,11 @@ function useWallets() {
11058
11117
  const walletToConnect = wallets.find((w) => w.id == connector.id);
11059
11118
  if (!walletToConnect) {
11060
11119
  log("Wallet not found", connector);
11061
- return {};
11120
+ return onError({
11121
+ error: new OpenfortError("Wallet not found", OpenfortErrorType.AUTHENTICATION_ERROR),
11122
+ options: optionsObject,
11123
+ hookOptions
11124
+ });
11062
11125
  }
11063
11126
  log("Connecting to wallet", walletToConnect);
11064
11127
  if (connector.id === embeddedWalletId) {
@@ -11077,114 +11140,118 @@ function useWallets() {
11077
11140
  function isOpenfortWallet(opts) {
11078
11141
  return opts.connector === embeddedWalletId;
11079
11142
  }
11080
- const chainId = (_a = openfortKitOptions === null || openfortKitOptions === void 0 ? void 0 : openfortKitOptions.initialChainId) !== null && _a !== void 0 ? _a : chain;
11081
- console.log("Setting active wallet", { options: optionsObject, chainId });
11143
+ log("Setting active wallet", { options: optionsObject, chainId });
11082
11144
  if (isOpenfortWallet(optionsObject)) {
11083
- const getEncryptionSession = async () => {
11084
- if (!walletConfig || !walletConfig.createEncryptedSessionEndpoint) {
11085
- throw new Error("No createEncryptedSessionEndpoint set in walletConfig");
11086
- }
11087
- const resp = await fetch(walletConfig.createEncryptedSessionEndpoint, {
11088
- method: "POST",
11089
- headers: {
11090
- "Content-Type": "application/json",
11091
- },
11092
- });
11093
- if (!resp.ok) {
11094
- throw new Error("Failed to create encryption session");
11095
- }
11096
- const respJSON = await resp.json();
11097
- return respJSON.session;
11098
- };
11099
11145
  setStatus({
11100
- status: 'loading',
11146
+ status: 'connecting',
11147
+ address: optionsObject.address,
11101
11148
  });
11102
11149
  const { password } = optionsObject;
11103
- // -----
11104
- //
11105
- // HERE WOULD GO THE LOGIC TO CONNECT TO WALLET A OR B
11106
- //
11107
- // -----
11108
11150
  if (!walletConfig) {
11109
- return {
11110
- error: "Embedded signer not enabled",
11111
- };
11151
+ return onError({
11152
+ error: new OpenfortError("Embedded signer not enabled", OpenfortErrorType.WALLET_ERROR),
11153
+ options: optionsObject,
11154
+ hookOptions
11155
+ });
11112
11156
  }
11113
- log(`Handling recovery with Openfort: password=${password}, chainId=${chainId}`);
11157
+ log(`Handling recovery with Openfort: ${password ? "with password" : "without password"}, chainId=${chainId}`);
11114
11158
  try {
11115
11159
  const accessToken = await client.getAccessToken();
11116
11160
  if (!accessToken) {
11117
11161
  throw new Error("Openfort access token not found");
11118
11162
  }
11119
- if (!password) {
11120
- const shieldAuth = {
11121
- auth: ShieldAuthType.OPENFORT,
11122
- token: accessToken,
11123
- encryptionSession: walletConfig.getEncryptionSession ?
11124
- await walletConfig.getEncryptionSession() :
11125
- await getEncryptionSession(),
11126
- };
11127
- log("Configuring embedded signer with automatic recovery");
11128
- await client.embeddedWallet.configure({
11129
- chainId,
11130
- recoveryParams: {
11131
- recoveryMethod: RecoveryMethod.AUTOMATIC,
11132
- },
11133
- shieldAuthentication: shieldAuth,
11163
+ log("Access token");
11164
+ const shieldAuthentication = password ? {
11165
+ auth: ShieldAuthType.OPENFORT,
11166
+ token: accessToken,
11167
+ } : {
11168
+ auth: ShieldAuthType.OPENFORT,
11169
+ token: accessToken,
11170
+ encryptionSession: walletConfig.getEncryptionSession ?
11171
+ await walletConfig.getEncryptionSession() :
11172
+ await getEncryptionSession(),
11173
+ };
11174
+ log("Shield authentication");
11175
+ const recoveryParams = password ? {
11176
+ recoveryMethod: RecoveryMethod.PASSWORD,
11177
+ password,
11178
+ } : {
11179
+ recoveryMethod: RecoveryMethod.AUTOMATIC,
11180
+ };
11181
+ log("Recovery params", optionsObject.address);
11182
+ if (optionsObject.address) {
11183
+ const walletId = (_a = embeddedWallets === null || embeddedWallets === void 0 ? void 0 : embeddedWallets.find((w) => w.address === optionsObject.address && w.chainId === chainId)) === null || _a === void 0 ? void 0 : _a.id;
11184
+ if (!walletId) {
11185
+ return onError({
11186
+ error: new OpenfortError("Embedded wallet not found for address", OpenfortErrorType.WALLET_ERROR),
11187
+ options: optionsObject,
11188
+ hookOptions
11189
+ });
11190
+ }
11191
+ await client.embeddedWallet.recover({
11192
+ account: walletId,
11193
+ shieldAuthentication,
11194
+ recoveryParams,
11134
11195
  });
11135
11196
  }
11136
11197
  else {
11137
- if (!password || password.length < 4) {
11138
- throw "Password recovery must be at least 4 characters";
11139
- }
11140
- const shieldAuth = {
11141
- auth: ShieldAuthType.OPENFORT,
11142
- token: accessToken,
11143
- };
11144
- await client.embeddedWallet.configure({
11145
- chainId,
11146
- recoveryParams: {
11147
- recoveryMethod: RecoveryMethod.PASSWORD,
11198
+ if (!embeddedWallets || embeddedWallets.length === 0) {
11199
+ await createWallet({
11148
11200
  password,
11149
- },
11150
- shieldAuthentication: shieldAuth,
11151
- });
11201
+ });
11202
+ }
11203
+ else {
11204
+ await client.embeddedWallet.recover({
11205
+ account: embeddedWallets[0].id,
11206
+ shieldAuthentication,
11207
+ recoveryParams,
11208
+ });
11209
+ }
11152
11210
  }
11153
11211
  setStatus({
11154
11212
  status: 'success',
11155
11213
  });
11156
- return {
11157
- wallet: createOpenfortWallet({
11158
- connector,
11159
- isEmbedded,
11160
- address,
11161
- }),
11162
- };
11214
+ return onSuccess({
11215
+ data: {
11216
+ wallet: createOpenfortWallet({
11217
+ address: optionsObject.address,
11218
+ }),
11219
+ },
11220
+ options: optionsObject,
11221
+ hookOptions,
11222
+ });
11163
11223
  }
11164
11224
  catch (err) {
11165
11225
  log('Error handling recovery with Openfort:', err);
11166
11226
  if (err instanceof MissingRecoveryPasswordError) {
11167
- return {
11168
- error: "Missing recovery password",
11169
- };
11227
+ return onError({
11228
+ error: new OpenfortError("Missing recovery password", OpenfortErrorType.WALLET_ERROR),
11229
+ options: optionsObject,
11230
+ hookOptions
11231
+ });
11170
11232
  }
11171
11233
  if (typeof err === 'string') {
11172
- return {
11173
- error: err,
11174
- };
11234
+ return onError({
11235
+ error: new OpenfortError(err, OpenfortErrorType.WALLET_ERROR),
11236
+ options: optionsObject,
11237
+ hookOptions
11238
+ });
11175
11239
  }
11176
11240
  // setStatus({
11177
11241
  // status: 'error',
11178
11242
  // error: new Error("Failed to connect with embedded wallet: " + err),
11179
11243
  // });
11180
- return {
11181
- error: "The recovery phrase you entered is incorrect.",
11182
- };
11244
+ return onError({
11245
+ error: new OpenfortError("The recovery phrase you entered is incorrect.", OpenfortErrorType.WALLET_ERROR),
11246
+ options: optionsObject,
11247
+ hookOptions
11248
+ });
11183
11249
  }
11184
11250
  }
11185
11251
  else {
11186
11252
  setStatus({
11187
- status: 'loading',
11253
+ status: 'connecting',
11254
+ address: optionsObject.address,
11188
11255
  });
11189
11256
  setConnectToConnector({
11190
11257
  address: optionsObject.address,
@@ -11193,16 +11260,70 @@ function useWallets() {
11193
11260
  }
11194
11261
  return {};
11195
11262
  }, [wallets, setOpen, setRoute, setConnector, disconnect, log, isConnected, address, usesEmbeddedWallet, isEmbedded]);
11196
- useCallback(() => {
11197
- throw new Error("createWallet is not implemented in useWallets. Use setActiveWallet instead.");
11198
- }, []);
11263
+ const createWallet = useCallback(async ({ password, ...options } = {}) => {
11264
+ var _a;
11265
+ setStatus({
11266
+ status: 'creating',
11267
+ });
11268
+ log("Creating wallet", { password: !!password, options });
11269
+ const accessToken = await client.getAccessToken();
11270
+ if (!accessToken) {
11271
+ return onError({
11272
+ error: new OpenfortError("Openfort access token not found", OpenfortErrorType.WALLET_ERROR),
11273
+ hookOptions,
11274
+ options,
11275
+ });
11276
+ }
11277
+ if (!walletConfig) {
11278
+ return onError({
11279
+ error: new OpenfortError("Embedded signer not enabled", OpenfortErrorType.WALLET_ERROR),
11280
+ hookOptions,
11281
+ options,
11282
+ });
11283
+ }
11284
+ const shieldAuthentication = {
11285
+ auth: ShieldAuthType.OPENFORT,
11286
+ token: accessToken,
11287
+ encryptionSession: walletConfig.getEncryptionSession ?
11288
+ await walletConfig.getEncryptionSession() :
11289
+ await getEncryptionSession(),
11290
+ };
11291
+ const recoveryParams = password ? {
11292
+ recoveryMethod: RecoveryMethod.PASSWORD,
11293
+ password,
11294
+ } : {
11295
+ recoveryMethod: RecoveryMethod.AUTOMATIC,
11296
+ };
11297
+ const wallet = await client.embeddedWallet.create({
11298
+ chainId: (_a = uiConfig === null || uiConfig === void 0 ? void 0 : uiConfig.initialChainId) !== null && _a !== void 0 ? _a : chainId,
11299
+ accountType: AccountTypeEnum.SMART_ACCOUNT,
11300
+ chainType: ChainTypeEnum.EVM,
11301
+ recoveryParams,
11302
+ shieldAuthentication
11303
+ });
11304
+ setStatus({
11305
+ status: 'success',
11306
+ });
11307
+ refetch();
11308
+ return onSuccess({
11309
+ data: {
11310
+ wallet: createOpenfortWallet({
11311
+ address: wallet.address,
11312
+ })
11313
+ }
11314
+ });
11315
+ }, [refetch, client, uiConfig, chainId]);
11199
11316
  return {
11200
- wallets,
11317
+ wallets: wallets.map((w) => ({
11318
+ ...w,
11319
+ isConnecting: status.status === 'connecting' && status.address === w.address,
11320
+ isActive: w.address === address && isConnected && (connector === null || connector === void 0 ? void 0 : connector.id) === w.id,
11321
+ })),
11201
11322
  availableWallets: deviceWallets,
11202
11323
  activeWallet,
11203
11324
  setActiveWallet,
11204
- // createWallet,
11205
- ...mapStatus(status),
11325
+ createWallet,
11326
+ ...mapStatus$1(status),
11206
11327
  exportPrivateKey: client.embeddedWallet.exportPrivateKey,
11207
11328
  };
11208
11329
  }
@@ -11223,7 +11344,7 @@ const Recover = () => {
11223
11344
  });
11224
11345
  setLoading(false);
11225
11346
  if (error) {
11226
- setRecoveryError(error || "There was an error recovering your account");
11347
+ setRecoveryError(error.message || "There was an error recovering your account");
11227
11348
  }
11228
11349
  else {
11229
11350
  log("Recovery success");
@@ -11239,7 +11360,7 @@ const Recover = () => {
11239
11360
  }, children: [jsx(Input, { value: recoveryPhrase, onChange: (e) => setRecoveryPhrase(e.target.value), type: "password", placeholder: "Enter your password", autoComplete: "off" }), recoveryError && (jsx(motion.div, { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 }, children: jsx(ModalBody, { style: { height: 24, marginTop: 12 }, "$error": true, children: jsx(FitText, { children: recoveryError }) }) }, recoveryError)), jsx(Button, { onClick: handleSubmit, waiting: loading, disabled: loading, children: "Enter" })] })] }));
11240
11361
  };
11241
11362
  const AutomaticRecovery = () => {
11242
- const { needsRecovery } = useOpenfort();
11363
+ const { needsRecovery } = useOpenfortCore();
11243
11364
  const { setActiveWallet } = useWallets();
11244
11365
  const { log } = useOpenfortKit();
11245
11366
  const [hasRecoveryMethod, setHasRecoveryMethod] = React.useState(false);
@@ -11250,7 +11371,8 @@ const AutomaticRecovery = () => {
11250
11371
  const response = await setActiveWallet({
11251
11372
  connector: embeddedWalletId,
11252
11373
  });
11253
- if (response.error && response.error === "Missing recovery password") {
11374
+ // TODO: Handle error properly
11375
+ if (response.error && response.error.message === "Missing recovery password") {
11254
11376
  setHasRecoveryMethod(true);
11255
11377
  }
11256
11378
  }
@@ -11272,7 +11394,7 @@ const Connected = () => {
11272
11394
  return (jsx(PageContent, { children: jsx(Loader, { isLoading: false, reason: "Connected" }) }));
11273
11395
  };
11274
11396
  const CreateEmbeddedSigner = () => {
11275
- const { needsRecovery, user } = useOpenfort();
11397
+ const { needsRecovery, user } = useOpenfortCore();
11276
11398
  const { triggerResize, uiConfig, walletConfig, setRoute } = useOpenfortKit();
11277
11399
  const [loading, setLoading] = React.useState(true);
11278
11400
  const [embeddedSignerLoading, setEmbeddedSignerLoading] = React.useState(true);
@@ -11326,7 +11448,7 @@ const customThemeDefault = {};
11326
11448
  const ConnectModal = ({ mode = 'auto', theme = 'auto', customTheme = customThemeDefault, lang = 'en-US' }) => {
11327
11449
  var _a;
11328
11450
  const context = useOpenfortKit();
11329
- const { logout, user } = useOpenfort();
11451
+ const { logout, user } = useOpenfortCore();
11330
11452
  const { isConnected, chain } = useAccount();
11331
11453
  const chainIsSupported = useChainIsSupported(chain === null || chain === void 0 ? void 0 : chain.id);
11332
11454
  //if chain is unsupported we enforce a "switch chain" prompt
@@ -11455,30 +11577,30 @@ const ConnectModal = ({ mode = 'auto', theme = 'auto', customTheme = customTheme
11455
11577
  };
11456
11578
 
11457
11579
  /**
11458
- * OpenfortKitProvider component provides context and configuration for OpenfortKit.
11580
+ * OpenfortProvider component provides context and configuration for OpenfortKit.
11459
11581
  * It must be used within a WagmiProvider.
11460
11582
  *
11461
11583
  * @param {React.ReactNode} children - The child components to be wrapped by the provider.
11462
11584
  * @param {string} [theme='auto'] - The theme to be used, default is 'auto'.
11463
11585
  * @param {string} [mode='auto'] - The mode to be used, default is 'auto'.
11464
11586
  * @param {CustomTheme} [customTheme] - Custom theme configuration.
11465
- * @param {ConnectKitOptions} [options] - Additional configuration options.
11587
+ * @param {ConnectUIOptions} [options] - Additional configuration options.
11466
11588
  * @param {Function} [onConnect] - Callback function to be called on connect.
11467
11589
  * @param {Function} [onDisconnect] - Callback function to be called on disconnect.
11468
11590
  * @param {boolean} [debugMode=false] - Enable or disable debug mode, default is false.
11469
11591
  * @param {OpenfortOptions} [openfortOptions] - Options for Openfort integration.
11470
11592
  * @throws Will throw an error if used outside of a WagmiProvider or if nested usages are detected.
11471
11593
  */
11472
- const OpenfortKitProvider = ({ children, uiConfig, onConnect, onDisconnect, debugMode = false, publishableKey, walletConfig, }) => {
11594
+ const OpenfortProvider = ({ children, uiConfig, onConnect, onDisconnect, debugMode = false, publishableKey, walletConfig, }) => {
11473
11595
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
11474
- // OpenfortKitProvider must be within a WagmiProvider
11596
+ // OpenfortProvider must be within a WagmiProvider
11475
11597
  if (!React.useContext(WagmiContext)) {
11476
- throw Error('OpenfortKitProvider must be within a WagmiProvider');
11598
+ throw Error('OpenfortProvider must be within a WagmiProvider');
11477
11599
  }
11478
- // Only allow for mounting OpenfortKitProvider once, so we avoid weird global
11600
+ // Only allow for mounting OpenfortProvider once, so we avoid weird global
11479
11601
  // state collisions.
11480
11602
  if (React.useContext(OpenfortKitContext)) {
11481
- throw new Error('Multiple, nested usages of OpenfortKitProvider detected. Please use only one.');
11603
+ throw new Error('Multiple, nested usages of OpenfortProvider detected. Please use only one.');
11482
11604
  }
11483
11605
  const chains = useChains();
11484
11606
  const injectedConnector = useConnector('injected');
@@ -11596,11 +11718,12 @@ const OpenfortKitProvider = ({ children, uiConfig, onConnect, onDisconnect, debu
11596
11718
  triggerResize: () => onResize((prev) => prev + 1),
11597
11719
  walletConfig,
11598
11720
  };
11599
- return createElement(OpenfortKitContext.Provider, { value }, jsx(Fragment, { children: jsx(Web3ContextProvider, { enabled: open, children: jsxs(OpenfortProvider, { baseConfiguration: {
11721
+ return createElement(OpenfortKitContext.Provider, { value }, jsx(Fragment, { children: jsx(Web3ContextProvider, { enabled: open, children: jsxs(CoreOpenfortProvider, { baseConfiguration: {
11600
11722
  publishableKey,
11601
11723
  }, shieldConfiguration: walletConfig ? {
11602
11724
  shieldPublishableKey: walletConfig.shieldPublishableKey,
11603
11725
  shieldEncryptionKey: walletConfig.recoveryMethod === RecoveryMethod.PASSWORD ? walletConfig.shieldEncryptionKey : undefined,
11726
+ debug: debugMode,
11604
11727
  } : undefined, overrides: opts.openfortUrlOverrides, debugMode: debugMode, onConnect: onConnect, onDisconnect: onDisconnect, children: [children, jsx(ConnectModal, { lang: ckLang, theme: ckTheme, mode: (_j = uiConfig === null || uiConfig === void 0 ? void 0 : uiConfig.mode) !== null && _j !== void 0 ? _j : ckMode, customTheme: ckCustomTheme })] }) }) }));
11605
11728
  };
11606
11729
 
@@ -11855,7 +11978,7 @@ const allRoutes = [
11855
11978
  ];
11856
11979
  function useUI() {
11857
11980
  const { open, setOpen, setRoute, log } = useOpenfortKit();
11858
- const { isLoading, user, needsRecovery } = useOpenfort();
11981
+ const { isLoading, user, needsRecovery } = useOpenfortCore();
11859
11982
  const { isConnected } = useAccount();
11860
11983
  function defaultOpen() {
11861
11984
  setOpen(true);
@@ -12014,7 +12137,7 @@ const ConnectButtonRenderer = ({ children, }) => {
12014
12137
  };
12015
12138
  ConnectButtonRenderer.displayName = 'OpenfortKitButton.Custom';
12016
12139
  const ConnectedLabel = ({ separator }) => {
12017
- const { user, isLoading } = useOpenfort();
12140
+ const { user, isLoading } = useOpenfortCore();
12018
12141
  const { address } = useAccount();
12019
12142
  if (address && (user || isLoading))
12020
12143
  return truncateEthAddress(address, separator);
@@ -12026,7 +12149,7 @@ const ConnectedLabel = ({ separator }) => {
12026
12149
  };
12027
12150
  function OpenfortKitButtonInner({ label, showAvatar, separator, }) {
12028
12151
  const locales = useLocales({});
12029
- const { user } = useOpenfort();
12152
+ const { user } = useOpenfortCore();
12030
12153
  const { address, chain } = useAccount();
12031
12154
  const isChainSupported = useChainIsSupported(chain === null || chain === void 0 ? void 0 : chain.id);
12032
12155
  const ensFallbackConfig = useEnsFallbackConfig();
@@ -12117,44 +12240,41 @@ OpenfortKitButton.Custom = ConnectButtonRenderer;
12117
12240
 
12118
12241
  var OpenfortKitStatus;
12119
12242
  (function (OpenfortKitStatus) {
12120
- OpenfortKitStatus[OpenfortKitStatus["LOADING"] = 0] = "LOADING";
12121
- OpenfortKitStatus[OpenfortKitStatus["CONNECTED"] = 1] = "CONNECTED";
12122
- OpenfortKitStatus[OpenfortKitStatus["DISCONNECTED"] = 2] = "DISCONNECTED";
12123
- OpenfortKitStatus[OpenfortKitStatus["NEEDS_RECOVERY"] = 3] = "NEEDS_RECOVERY";
12124
- OpenfortKitStatus[OpenfortKitStatus["CONNECTED_WITHOUT_USER"] = 4] = "CONNECTED_WITHOUT_USER";
12243
+ OpenfortKitStatus[OpenfortKitStatus["DISCONNECTED"] = 0] = "DISCONNECTED";
12244
+ OpenfortKitStatus[OpenfortKitStatus["NEEDS_RECOVERY"] = 1] = "NEEDS_RECOVERY";
12245
+ OpenfortKitStatus[OpenfortKitStatus["LOADING"] = 2] = "LOADING";
12246
+ OpenfortKitStatus[OpenfortKitStatus["CONNECTED"] = 3] = "CONNECTED";
12125
12247
  })(OpenfortKitStatus || (OpenfortKitStatus = {}));
12126
12248
  function useStatus() {
12127
- const { isLoading, needsRecovery, user } = useOpenfort();
12128
- const { isConnected } = useAccount();
12249
+ const { embeddedState } = useOpenfortCore();
12250
+ const { isConnected, isConnecting } = useAccount();
12129
12251
  const getStatus = () => {
12130
- if (isLoading)
12252
+ if (embeddedState === EmbeddedState.READY)
12253
+ return OpenfortKitStatus.CONNECTED;
12254
+ if (embeddedState === EmbeddedState.NONE)
12131
12255
  return OpenfortKitStatus.LOADING;
12132
- if (needsRecovery)
12133
- return OpenfortKitStatus.NEEDS_RECOVERY;
12134
- if (!user) {
12256
+ // if (needsRecovery) return OpenfortKitStatus.NEEDS_RECOVERY;
12257
+ if (embeddedState === EmbeddedState.EMBEDDED_SIGNER_NOT_CONFIGURED) {
12135
12258
  if (isConnected)
12136
- return OpenfortKitStatus.CONNECTED_WITHOUT_USER;
12259
+ return OpenfortKitStatus.CONNECTED;
12137
12260
  else
12138
- return OpenfortKitStatus.DISCONNECTED;
12261
+ return OpenfortKitStatus.NEEDS_RECOVERY;
12139
12262
  }
12140
- if (!isConnected)
12141
- return OpenfortKitStatus.NEEDS_RECOVERY;
12142
- return OpenfortKitStatus.CONNECTED;
12263
+ return OpenfortKitStatus.DISCONNECTED;
12143
12264
  };
12144
12265
  const status = getStatus();
12145
12266
  return {
12146
- status,
12147
12267
  isLoading: status === OpenfortKitStatus.LOADING,
12148
- hasUser: !!user,
12149
12268
  isConnected: status === OpenfortKitStatus.CONNECTED,
12150
12269
  isDisconnected: status === OpenfortKitStatus.DISCONNECTED,
12151
- isConnectedWithoutUser: status === OpenfortKitStatus.CONNECTED_WITHOUT_USER,
12152
- needsRecovery: status === OpenfortKitStatus.NEEDS_RECOVERY,
12270
+ isConnecting: isConnecting || embeddedState === EmbeddedState.CREATING_ACCOUNT,
12271
+ isAuthenticated: embeddedState !== EmbeddedState.NONE && embeddedState !== EmbeddedState.UNAUTHENTICATED,
12153
12272
  };
12154
12273
  }
12155
12274
 
12156
12275
  function useUser() {
12157
- const { user, client } = useOpenfort();
12276
+ const { user, client } = useOpenfortCore();
12277
+ const { isAuthenticated } = useStatus();
12158
12278
  const getAccessTokenAndUpdate = useCallback(async () => {
12159
12279
  await client.validateAndRefreshToken();
12160
12280
  const token = await client.getAccessToken();
@@ -12162,11 +12282,17 @@ function useUser() {
12162
12282
  }, [client]);
12163
12283
  return {
12164
12284
  user,
12285
+ isAuthenticated,
12165
12286
  getAccessToken: getAccessTokenAndUpdate,
12166
12287
  validateAndRefreshToken: async () => await client.validateAndRefreshToken(),
12167
12288
  };
12168
12289
  }
12169
12290
 
12291
+ function useWallet() {
12292
+ const { activeWallet } = useWallets();
12293
+ return activeWallet;
12294
+ }
12295
+
12170
12296
  const buildCallbackUrl = ({ email, callbackUrl, provider, }) => {
12171
12297
  if (callbackUrl && !callbackUrl.startsWith("http")) {
12172
12298
  callbackUrl = `${window.location.origin}${callbackUrl.startsWith("/") ? "" : "/"}${callbackUrl}`;
@@ -12179,36 +12305,72 @@ const buildCallbackUrl = ({ email, callbackUrl, provider, }) => {
12179
12305
  return redirectUrl.toString();
12180
12306
  };
12181
12307
 
12182
- const onSuccess = ({ hookOptions, options, data, }) => {
12183
- var _a, _b, _c, _d;
12184
- (_a = hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.onSuccess) === null || _a === void 0 ? void 0 : _a.call(hookOptions, data);
12185
- (_b = hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.onSettled) === null || _b === void 0 ? void 0 : _b.call(hookOptions, data, null);
12186
- (_c = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _c === void 0 ? void 0 : _c.call(options, data);
12187
- (_d = options === null || options === void 0 ? void 0 : options.onSettled) === null || _d === void 0 ? void 0 : _d.call(options, data, null);
12188
- return data;
12189
- };
12190
- const onError = ({ hookOptions, options, error, }) => {
12191
- var _a, _b, _c, _d;
12192
- (_a = hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.onError) === null || _a === void 0 ? void 0 : _a.call(hookOptions, error);
12193
- (_b = hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.onSettled) === null || _b === void 0 ? void 0 : _b.call(hookOptions, null, error);
12194
- (_c = options === null || options === void 0 ? void 0 : options.onError) === null || _c === void 0 ? void 0 : _c.call(options, error);
12195
- (_d = options === null || options === void 0 ? void 0 : options.onSettled) === null || _d === void 0 ? void 0 : _d.call(options, null, error);
12196
- if ((hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.throwOnError) || (options === null || options === void 0 ? void 0 : options.throwOnError))
12197
- throw error;
12198
- return { error };
12308
+ const mapStatus = (status) => {
12309
+ return {
12310
+ isLoading: status.status === 'loading',
12311
+ isError: status.status === 'error',
12312
+ isSuccess: status.status === 'success',
12313
+ error: status.error
12314
+ };
12199
12315
  };
12200
12316
 
12317
+ function useSignOut(hookOptions = {}) {
12318
+ const { client, updateUser, user } = useOpenfortCore();
12319
+ const [status, setStatus] = useState({
12320
+ status: "idle",
12321
+ });
12322
+ const { disconnect } = useDisconnect();
12323
+ const signOut = useCallback(async (options = {}) => {
12324
+ if (!user)
12325
+ return;
12326
+ setStatus({
12327
+ status: 'loading',
12328
+ });
12329
+ try {
12330
+ await client.auth.logout();
12331
+ disconnect();
12332
+ updateUser();
12333
+ setStatus({
12334
+ status: 'success',
12335
+ });
12336
+ return onSuccess({
12337
+ hookOptions,
12338
+ options,
12339
+ data: {},
12340
+ });
12341
+ }
12342
+ catch (error) {
12343
+ setStatus({
12344
+ status: 'error',
12345
+ error: new OpenfortError('Failed to sign out', OpenfortErrorType.AUTHENTICATION_ERROR, { error }),
12346
+ });
12347
+ throw error;
12348
+ }
12349
+ }, [client, user, disconnect, updateUser, setStatus, hookOptions]);
12350
+ return {
12351
+ ...mapStatus(status),
12352
+ signOut,
12353
+ };
12354
+ }
12355
+
12201
12356
  // this hook is used to create a wallet after the user has authenticated
12202
12357
  const useCreateWalletPostAuth = () => {
12203
12358
  const { setActiveWallet } = useWallets();
12204
12359
  const { walletConfig } = useOpenfortKit();
12205
- const tryUseWallet = useCallback(async () => {
12206
- if (!walletConfig || walletConfig.recoveryMethod !== RecoveryMethod.AUTOMATIC)
12360
+ const { signOut } = useSignOut();
12361
+ const tryUseWallet = useCallback(async ({ logoutOnError: signOutOnError = true, automaticRecovery = true }) => {
12362
+ if (!walletConfig || walletConfig.recoveryMethod !== RecoveryMethod.AUTOMATIC || !automaticRecovery) {
12207
12363
  return {};
12208
- return await setActiveWallet({
12364
+ }
12365
+ const wallet = await setActiveWallet({
12209
12366
  connector: embeddedWalletId,
12210
12367
  });
12211
- }, [walletConfig]);
12368
+ if (wallet.error && signOutOnError) {
12369
+ // If there was an error and we should log out, we can call the logout function
12370
+ await signOut();
12371
+ }
12372
+ return wallet;
12373
+ }, [walletConfig, setActiveWallet, signOut]);
12212
12374
  return {
12213
12375
  tryUseWallet,
12214
12376
  };
@@ -12216,7 +12378,7 @@ const useCreateWalletPostAuth = () => {
12216
12378
 
12217
12379
  const useEmailAuth = (hookOptions = {}) => {
12218
12380
  const { log } = useOpenfortKit();
12219
- const { client, updateUser } = useOpenfort();
12381
+ const { client, updateUser } = useOpenfortCore();
12220
12382
  const [requiresEmailVerification, setRequiresEmailVerification] = useState(false);
12221
12383
  const [status, setStatus] = useState({
12222
12384
  status: "idle",
@@ -12259,7 +12421,10 @@ const useEmailAuth = (hookOptions = {}) => {
12259
12421
  });
12260
12422
  }
12261
12423
  else {
12262
- const { wallet } = await tryUseWallet();
12424
+ const { wallet } = await tryUseWallet({
12425
+ logoutOnError: options.logoutOnError || hookOptions.logoutOnError,
12426
+ automaticRecovery: options.automaticRecovery || hookOptions.automaticRecovery,
12427
+ });
12263
12428
  setStatus({
12264
12429
  status: 'success',
12265
12430
  });
@@ -12273,7 +12438,7 @@ const useEmailAuth = (hookOptions = {}) => {
12273
12438
  }
12274
12439
  }
12275
12440
  catch (e) {
12276
- const error = new OpenfortKitError("Failed to login with email and password", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error: e });
12441
+ const error = new OpenfortError("Failed to login with email and password", OpenfortErrorType.AUTHENTICATION_ERROR, { error: e });
12277
12442
  setStatus({
12278
12443
  status: 'error',
12279
12444
  error: error
@@ -12311,7 +12476,7 @@ const useEmailAuth = (hookOptions = {}) => {
12311
12476
  });
12312
12477
  }
12313
12478
  catch (e) {
12314
- const error = new OpenfortKitError("Failed to reset password", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error: e });
12479
+ const error = new OpenfortError("Failed to reset password", OpenfortErrorType.AUTHENTICATION_ERROR, { error: e });
12315
12480
  setStatus({
12316
12481
  status: 'error',
12317
12482
  error
@@ -12345,7 +12510,7 @@ const useEmailAuth = (hookOptions = {}) => {
12345
12510
  });
12346
12511
  }
12347
12512
  catch (e) {
12348
- const error = new OpenfortKitError("Failed to reset password", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error: e });
12513
+ const error = new OpenfortError("Failed to reset password", OpenfortErrorType.AUTHENTICATION_ERROR, { error: e });
12349
12514
  setStatus({
12350
12515
  status: 'error',
12351
12516
  error
@@ -12389,7 +12554,10 @@ const useEmailAuth = (hookOptions = {}) => {
12389
12554
  });
12390
12555
  }
12391
12556
  else {
12392
- const { wallet } = await tryUseWallet();
12557
+ const { wallet } = await tryUseWallet({
12558
+ logoutOnError: options.logoutOnError || hookOptions.logoutOnError,
12559
+ automaticRecovery: options.automaticRecovery || hookOptions.automaticRecovery,
12560
+ });
12393
12561
  setStatus({
12394
12562
  status: 'success',
12395
12563
  });
@@ -12403,7 +12571,7 @@ const useEmailAuth = (hookOptions = {}) => {
12403
12571
  }
12404
12572
  }
12405
12573
  catch (e) {
12406
- const error = new OpenfortKitError("Failed to login with email and password", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error: e });
12574
+ const error = new OpenfortError("Failed to login with email and password", OpenfortErrorType.AUTHENTICATION_ERROR, { error: e });
12407
12575
  setStatus({
12408
12576
  status: 'error',
12409
12577
  error
@@ -12422,7 +12590,7 @@ const useEmailAuth = (hookOptions = {}) => {
12422
12590
  const authToken = await client.getAccessToken();
12423
12591
  if (!authToken) {
12424
12592
  log("No token found");
12425
- const error = new OpenfortKitError("No token found", OpenfortKitErrorType.AUTHENTICATION_ERROR);
12593
+ const error = new OpenfortError("No token found", OpenfortErrorType.AUTHENTICATION_ERROR);
12426
12594
  setStatus({
12427
12595
  status: 'error',
12428
12596
  error
@@ -12468,7 +12636,7 @@ const useEmailAuth = (hookOptions = {}) => {
12468
12636
  }
12469
12637
  }
12470
12638
  catch (e) {
12471
- const error = new OpenfortKitError("Failed to link email", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error: e });
12639
+ const error = new OpenfortError("Failed to link email", OpenfortErrorType.AUTHENTICATION_ERROR, { error: e });
12472
12640
  setStatus({
12473
12641
  status: 'error',
12474
12642
  error: error
@@ -12480,34 +12648,10 @@ const useEmailAuth = (hookOptions = {}) => {
12480
12648
  });
12481
12649
  }
12482
12650
  }, [client, setStatus, updateUser, log, hookOptions]);
12483
- return {
12484
- signInEmail,
12485
- signUpEmail,
12486
- linkEmail,
12487
- requestResetPassword,
12488
- resetPassword,
12489
- reset,
12490
- ...mapStatus(status),
12491
- requiresEmailVerification,
12492
- isAwaitingInput: status.status === 'awaiting-input',
12493
- };
12494
- };
12495
-
12496
- const useAuthCallback = ({ automaticallyHandleCallback = true, // Automatically handle OAuth and email callback
12497
- ...hookOptions } = {}) => {
12498
- const { log } = useOpenfortKit();
12499
- const [status, setStatus] = useState({
12500
- status: "idle",
12501
- });
12502
- const { client, updateUser } = useOpenfort();
12503
- const [provider, setProvider] = useState(null);
12504
- const [email, setEmail] = useState(null);
12505
- const { tryUseWallet } = useCreateWalletPostAuth();
12506
12651
  const verifyEmail = useCallback(async ({ email, state, ...options }) => {
12507
12652
  setStatus({
12508
12653
  status: 'loading',
12509
12654
  });
12510
- setEmail(email);
12511
12655
  try {
12512
12656
  await client.auth.verifyEmail({
12513
12657
  email,
@@ -12519,24 +12663,60 @@ const useAuthCallback = ({ automaticallyHandleCallback = true, // Automatically
12519
12663
  return onSuccess({
12520
12664
  hookOptions,
12521
12665
  options,
12522
- data: { email, type: "verifyEmail" },
12666
+ data: {
12667
+ email,
12668
+ },
12523
12669
  });
12524
12670
  }
12525
12671
  catch (e) {
12526
- const error = new OpenfortKitError("Failed to verify email", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error: e });
12672
+ const error = new OpenfortError("Failed to verify email", OpenfortErrorType.AUTHENTICATION_ERROR, { error: e });
12527
12673
  setStatus({
12528
12674
  status: 'error',
12529
12675
  error,
12530
12676
  });
12531
12677
  log("Error verifying email", e);
12532
- onError({
12678
+ return onError({
12533
12679
  hookOptions,
12534
12680
  options,
12535
12681
  error,
12536
12682
  });
12537
- return { error, type: "verifyEmail" };
12538
12683
  }
12539
12684
  }, [client, log, hookOptions]);
12685
+ return {
12686
+ signInEmail,
12687
+ signUpEmail,
12688
+ verifyEmail,
12689
+ linkEmail,
12690
+ requestResetPassword,
12691
+ resetPassword,
12692
+ reset,
12693
+ ...mapStatus(status),
12694
+ requiresEmailVerification,
12695
+ isAwaitingInput: status.status === 'awaiting-input',
12696
+ };
12697
+ };
12698
+
12699
+ const providerToAuthProvider = {
12700
+ // [OAuthProvider.APPLE]: AuthProvider.,
12701
+ // [OAuthProvider.DISCORD]: AuthProvider.,
12702
+ // [OAuthProvider.EPIC_GAMES]: AuthProvider.,
12703
+ [AuthProvider.FACEBOOK]: OAuthProvider.FACEBOOK,
12704
+ [AuthProvider.GOOGLE]: OAuthProvider.GOOGLE,
12705
+ // [OAuthProvider.LINE]: AuthProvider.,
12706
+ [AuthProvider.TWITTER]: OAuthProvider.TWITTER,
12707
+ };
12708
+ function getOAuthProvider(provider) {
12709
+ if (!providerToAuthProvider[provider]) {
12710
+ throw new Error(`Unsupported OAuth provider: ${provider}`);
12711
+ }
12712
+ return providerToAuthProvider[provider];
12713
+ }
12714
+ const useOAuth = (hookOptions = {}) => {
12715
+ const { client, updateUser } = useOpenfortCore();
12716
+ const [status, setStatus] = useState({
12717
+ status: "idle",
12718
+ });
12719
+ const { tryUseWallet } = useCreateWalletPostAuth();
12540
12720
  const storeCredentials = useCallback(async ({ player, accessToken, refreshToken, ...options }) => {
12541
12721
  setStatus({
12542
12722
  status: 'loading',
@@ -12551,7 +12731,10 @@ const useAuthCallback = ({ automaticallyHandleCallback = true, // Automatically
12551
12731
  status: 'success',
12552
12732
  });
12553
12733
  const user = await updateUser() || undefined;
12554
- const { wallet } = await tryUseWallet();
12734
+ const { wallet } = await tryUseWallet({
12735
+ logoutOnError: options.logoutOnError || hookOptions.logoutOnError,
12736
+ automaticRecovery: options.automaticRecovery || hookOptions.automaticRecovery,
12737
+ });
12555
12738
  return onSuccess({
12556
12739
  data: { user, wallet, type: "storeCredentials" },
12557
12740
  hookOptions,
@@ -12559,22 +12742,110 @@ const useAuthCallback = ({ automaticallyHandleCallback = true, // Automatically
12559
12742
  });
12560
12743
  }
12561
12744
  catch (e) {
12562
- const error = new OpenfortKitError("Failed to store credentials", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error: e });
12745
+ const error = new OpenfortError("Failed to store credentials", OpenfortErrorType.AUTHENTICATION_ERROR, { error: e });
12563
12746
  setStatus({
12564
12747
  status: 'error',
12565
12748
  error,
12566
12749
  });
12567
- log("Error storing credentials", e);
12568
- onError({
12750
+ return onError({
12569
12751
  hookOptions,
12570
12752
  options,
12571
12753
  error,
12572
12754
  });
12573
- return { error, type: "storeCredentials" };
12574
12755
  }
12575
- }, [client, log, hookOptions]);
12756
+ }, [client, hookOptions]);
12757
+ const initOAuth = useCallback(async (options) => {
12758
+ var _a;
12759
+ const authProvider = options.provider;
12760
+ try {
12761
+ setStatus({
12762
+ status: 'loading',
12763
+ });
12764
+ await client.auth.initOAuth({
12765
+ provider: getOAuthProvider(authProvider),
12766
+ options: {
12767
+ redirectTo: buildCallbackUrl({
12768
+ provider: authProvider,
12769
+ callbackUrl: (_a = hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.redirectTo) !== null && _a !== void 0 ? _a : options === null || options === void 0 ? void 0 : options.redirectTo,
12770
+ })
12771
+ },
12772
+ });
12773
+ return onSuccess({
12774
+ data: {},
12775
+ hookOptions,
12776
+ options,
12777
+ });
12778
+ }
12779
+ catch (e) {
12780
+ const error = new OpenfortError("Failed to login with OAuth", OpenfortErrorType.AUTHENTICATION_ERROR, { error: e });
12781
+ setStatus({
12782
+ status: 'error',
12783
+ error
12784
+ });
12785
+ return onError({
12786
+ hookOptions,
12787
+ options,
12788
+ error,
12789
+ });
12790
+ }
12791
+ }, [client, setStatus, updateUser, hookOptions]);
12792
+ const linkOauth = useCallback(async (options) => {
12793
+ var _a;
12794
+ const authProvider = options.provider;
12795
+ try {
12796
+ setStatus({
12797
+ status: 'loading',
12798
+ });
12799
+ const authToken = await client.getAccessToken();
12800
+ if (!authToken) {
12801
+ throw new OpenfortError("No auth token found", OpenfortErrorType.AUTHENTICATION_ERROR);
12802
+ }
12803
+ await client.auth.initLinkOAuth({
12804
+ authToken,
12805
+ provider: getOAuthProvider(authProvider),
12806
+ options: {
12807
+ redirectTo: buildCallbackUrl({
12808
+ provider: authProvider,
12809
+ callbackUrl: (_a = options === null || options === void 0 ? void 0 : options.redirectTo) !== null && _a !== void 0 ? _a : hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.redirectTo,
12810
+ })
12811
+ },
12812
+ });
12813
+ return onSuccess({
12814
+ data: {},
12815
+ hookOptions,
12816
+ options,
12817
+ });
12818
+ }
12819
+ catch (e) {
12820
+ const error = new OpenfortError("Failed to link OAuth", OpenfortErrorType.AUTHENTICATION_ERROR, { error: e });
12821
+ setStatus({
12822
+ status: 'error',
12823
+ error
12824
+ });
12825
+ return onError({
12826
+ hookOptions,
12827
+ options,
12828
+ error,
12829
+ });
12830
+ }
12831
+ }, [client, setStatus, updateUser, hookOptions]);
12832
+ return {
12833
+ initOAuth,
12834
+ linkOauth,
12835
+ storeCredentials,
12836
+ ...mapStatus(status),
12837
+ };
12838
+ };
12839
+
12840
+ const useAuthCallback = ({ enabled = true, // Automatically handle OAuth and email callback
12841
+ ...hookOptions } = {}) => {
12842
+ const { log } = useOpenfortKit();
12843
+ const [provider, setProvider] = useState(null);
12844
+ const [email, setEmail] = useState(null);
12845
+ const { verifyEmail, isSuccess: isEmailSuccess, isLoading: isEmailLoading, isError: isEmailError, error: emailError, } = useEmailAuth();
12846
+ const { storeCredentials, isSuccess: isOAuthSuccess, isLoading: isOAuthLoading, isError: isOAuthError, error: oAuthError, } = useOAuth();
12576
12847
  useEffect(() => {
12577
- if (!automaticallyHandleCallback)
12848
+ if (!enabled)
12578
12849
  return;
12579
12850
  (async () => {
12580
12851
  // redirectUrl is not working with query params OF-1013
@@ -12594,7 +12865,7 @@ const useAuthCallback = ({ automaticallyHandleCallback = true, // Automatically
12594
12865
  onError({
12595
12866
  hookOptions,
12596
12867
  options: {},
12597
- error: new OpenfortKitError("No state or email found in URL", OpenfortKitErrorType.AUTHENTICATION_ERROR),
12868
+ error: new OpenfortError("No state or email found in URL", OpenfortErrorType.AUTHENTICATION_ERROR),
12598
12869
  });
12599
12870
  return;
12600
12871
  }
@@ -12603,7 +12874,26 @@ const useAuthCallback = ({ automaticallyHandleCallback = true, // Automatically
12603
12874
  window.history.replaceState({}, document.title, url.toString());
12604
12875
  };
12605
12876
  log("EmailVerification", state, email);
12606
- await verifyEmail({ email, state });
12877
+ const options = {
12878
+ onSuccess: (data) => {
12879
+ var _a;
12880
+ (_a = hookOptions.onSuccess) === null || _a === void 0 ? void 0 : _a.call(hookOptions, {
12881
+ ...data,
12882
+ type: "verifyEmail",
12883
+ });
12884
+ },
12885
+ onSettled: (data, error) => {
12886
+ var _a;
12887
+ (_a = hookOptions.onSettled) === null || _a === void 0 ? void 0 : _a.call(hookOptions, {
12888
+ ...data,
12889
+ type: "verifyEmail",
12890
+ }, error);
12891
+ },
12892
+ onError: hookOptions.onError,
12893
+ throwOnError: hookOptions.throwOnError,
12894
+ };
12895
+ await verifyEmail({ email, state, ...options });
12896
+ setEmail(email);
12607
12897
  removeParams();
12608
12898
  }
12609
12899
  else {
@@ -12620,7 +12910,7 @@ const useAuthCallback = ({ automaticallyHandleCallback = true, // Automatically
12620
12910
  onError({
12621
12911
  hookOptions,
12622
12912
  options: {},
12623
- error: new OpenfortKitError("Missing player id or access token or refresh token", OpenfortKitErrorType.AUTHENTICATION_ERROR),
12913
+ error: new OpenfortError("Missing player id or access token or refresh token", OpenfortErrorType.AUTHENTICATION_ERROR),
12624
12914
  });
12625
12915
  return;
12626
12916
  }
@@ -12634,7 +12924,25 @@ const useAuthCallback = ({ automaticallyHandleCallback = true, // Automatically
12634
12924
  window.history.replaceState({}, document.title, url.toString());
12635
12925
  };
12636
12926
  log("callback", { player, accessToken, refreshToken });
12637
- await storeCredentials({ player, accessToken, refreshToken });
12927
+ const options = {
12928
+ onSuccess: (data) => {
12929
+ var _a;
12930
+ (_a = hookOptions.onSuccess) === null || _a === void 0 ? void 0 : _a.call(hookOptions, {
12931
+ ...data,
12932
+ type: "storeCredentials",
12933
+ });
12934
+ },
12935
+ onSettled: (data, error) => {
12936
+ var _a;
12937
+ (_a = hookOptions.onSettled) === null || _a === void 0 ? void 0 : _a.call(hookOptions, {
12938
+ ...data,
12939
+ type: "storeCredentials",
12940
+ }, error);
12941
+ },
12942
+ onError: hookOptions.onError,
12943
+ throwOnError: hookOptions.throwOnError,
12944
+ };
12945
+ await storeCredentials({ player, accessToken, refreshToken, ...options });
12638
12946
  removeParams();
12639
12947
  }
12640
12948
  })();
@@ -12644,12 +12952,15 @@ const useAuthCallback = ({ automaticallyHandleCallback = true, // Automatically
12644
12952
  provider,
12645
12953
  verifyEmail,
12646
12954
  storeCredentials,
12647
- ...mapStatus(status),
12955
+ isLoading: isEmailLoading || isOAuthLoading,
12956
+ isError: isEmailError || isOAuthError,
12957
+ isSuccess: isEmailSuccess || isOAuthSuccess,
12958
+ error: emailError || oAuthError,
12648
12959
  };
12649
12960
  };
12650
12961
 
12651
12962
  const useGuestAuth = (hookOptions = {}) => {
12652
- const { client, updateUser } = useOpenfort();
12963
+ const { client, updateUser } = useOpenfortCore();
12653
12964
  const [status, setStatus] = useState({
12654
12965
  status: "idle",
12655
12966
  });
@@ -12662,7 +12973,10 @@ const useGuestAuth = (hookOptions = {}) => {
12662
12973
  const result = await client.auth.signUpGuest();
12663
12974
  const user = result.player;
12664
12975
  await updateUser(user);
12665
- const { wallet } = await tryUseWallet();
12976
+ const { wallet } = await tryUseWallet({
12977
+ logoutOnError: options.logoutOnError || hookOptions.logoutOnError,
12978
+ automaticRecovery: options.automaticRecovery || hookOptions.automaticRecovery,
12979
+ });
12666
12980
  setStatus({
12667
12981
  status: 'success',
12668
12982
  });
@@ -12674,15 +12988,15 @@ const useGuestAuth = (hookOptions = {}) => {
12674
12988
  return { user, wallet };
12675
12989
  }
12676
12990
  catch (error) {
12677
- const openfortKitError = new OpenfortKitError("Failed to signup guest", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error });
12991
+ const openfortError = new OpenfortError("Failed to signup guest", OpenfortErrorType.AUTHENTICATION_ERROR, { error });
12678
12992
  setStatus({
12679
12993
  status: 'error',
12680
- error: openfortKitError,
12994
+ error: openfortError,
12681
12995
  });
12682
12996
  return onError({
12683
12997
  hookOptions,
12684
12998
  options,
12685
- error: openfortKitError,
12999
+ error: openfortError,
12686
13000
  });
12687
13001
  }
12688
13002
  }, [client, setStatus, updateUser, hookOptions]);
@@ -12693,12 +13007,14 @@ const useGuestAuth = (hookOptions = {}) => {
12693
13007
  };
12694
13008
 
12695
13009
  const useWalletAuth = (hookOptions = {}) => {
12696
- const { updateUser } = useOpenfort();
13010
+ const { updateUser } = useOpenfortCore();
12697
13011
  const { log } = useOpenfortKit();
12698
13012
  const siwe = useConnectWithSiwe();
12699
13013
  const availableWallets = useWallets$1(); // TODO: Use this to get the wallet client type
12700
13014
  const { disconnect } = useDisconnect();
12701
13015
  const [walletConnectingTo, setWalletConnectingTo] = useState(null);
13016
+ const [shouldConnectWithSiwe, setShouldConnectWithSiwe] = useState(false);
13017
+ useAccount();
12702
13018
  const [status, setStatus] = useState({
12703
13019
  status: "idle",
12704
13020
  });
@@ -12716,32 +13032,39 @@ const useWalletAuth = (hookOptions = {}) => {
12716
13032
  const { connectAsync } = useConnect({
12717
13033
  mutation: {
12718
13034
  onError: (e) => {
12719
- const error = new OpenfortKitError("Failed to connect with wallet", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error: e });
13035
+ const error = new OpenfortError("Failed to connect with wallet", OpenfortErrorType.AUTHENTICATION_ERROR, { error: e });
12720
13036
  handleError(error);
12721
13037
  },
12722
13038
  onSuccess: () => {
12723
- siwe({
12724
- onError: (e) => {
12725
- log("Error connecting with SIWE", e);
12726
- disconnect();
12727
- const error = new OpenfortKitError("Failed to connect with siwe", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error: e });
12728
- handleError(error);
12729
- },
12730
- onConnect: () => {
12731
- log("Successfully connected with SIWE");
12732
- setStatus({
12733
- status: 'success',
12734
- });
12735
- updateUser();
12736
- onSuccess({
12737
- hookOptions,
12738
- data: {},
12739
- });
12740
- },
12741
- });
13039
+ setShouldConnectWithSiwe(true);
12742
13040
  }
12743
13041
  }
12744
13042
  });
13043
+ useEffect(() => {
13044
+ // Ensure it has been connected with a wallet before connecting with SIWE
13045
+ if (!shouldConnectWithSiwe)
13046
+ return;
13047
+ setShouldConnectWithSiwe(false);
13048
+ siwe({
13049
+ onError: (e) => {
13050
+ log("Error connecting with SIWE", e);
13051
+ disconnect();
13052
+ const error = new OpenfortError("Failed to connect with siwe", OpenfortErrorType.AUTHENTICATION_ERROR, { error: e });
13053
+ handleError(error);
13054
+ },
13055
+ onConnect: () => {
13056
+ log("Successfully connected with SIWE");
13057
+ setStatus({
13058
+ status: 'success',
13059
+ });
13060
+ updateUser();
13061
+ onSuccess({
13062
+ hookOptions,
13063
+ data: {},
13064
+ });
13065
+ },
13066
+ });
13067
+ }, [shouldConnectWithSiwe, siwe, updateUser, log]);
12745
13068
  // const generateSiweMessage = useCallback(
12746
13069
  // async (args) => {
12747
13070
  // try {
@@ -12827,7 +13150,7 @@ const useWalletAuth = (hookOptions = {}) => {
12827
13150
  }
12828
13151
  if (!connector) {
12829
13152
  log("Connector not found", connector);
12830
- return handleError(new OpenfortKitError("Connector not found", OpenfortKitErrorType.AUTHENTICATION_ERROR));
13153
+ return handleError(new OpenfortError("Connector not found", OpenfortErrorType.AUTHENTICATION_ERROR));
12831
13154
  }
12832
13155
  setWalletConnectingTo(connector.id);
12833
13156
  const hasDisconnected = new Promise((resolve) => {
@@ -12837,7 +13160,7 @@ const useWalletAuth = (hookOptions = {}) => {
12837
13160
  },
12838
13161
  onError: (e) => {
12839
13162
  console.error("Error disconnecting", e);
12840
- const error = new OpenfortKitError("Failed to disconnect", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error: e });
13163
+ const error = new OpenfortError("Failed to disconnect", OpenfortErrorType.AUTHENTICATION_ERROR, { error: e });
12841
13164
  handleError(error);
12842
13165
  resolve();
12843
13166
  },
@@ -12848,11 +13171,11 @@ const useWalletAuth = (hookOptions = {}) => {
12848
13171
  await connectAsync({
12849
13172
  connector,
12850
13173
  });
12851
- console.log("Connected to wallet!!!", connector.id);
13174
+ log("Connected to wallet!!!", connector.id);
12852
13175
  }
12853
13176
  catch (error) {
12854
13177
  console.error("Error connecting", error);
12855
- handleError(new OpenfortKitError("Failed to connect", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error }));
13178
+ handleError(new OpenfortError("Failed to connect", OpenfortErrorType.AUTHENTICATION_ERROR, { error }));
12856
13179
  }
12857
13180
  }, [siwe, disconnect, updateUser, availableWallets, log, setStatus, hookOptions]);
12858
13181
  return {
@@ -12866,146 +13189,5 @@ const useWalletAuth = (hookOptions = {}) => {
12866
13189
  };
12867
13190
  };
12868
13191
 
12869
- const providerToAuthProvider = {
12870
- // [OAuthProvider.APPLE]: AuthProvider.,
12871
- // [OAuthProvider.DISCORD]: AuthProvider.,
12872
- // [OAuthProvider.EPIC_GAMES]: AuthProvider.,
12873
- [AuthProvider.FACEBOOK]: OAuthProvider.FACEBOOK,
12874
- [AuthProvider.GOOGLE]: OAuthProvider.GOOGLE,
12875
- // [OAuthProvider.LINE]: AuthProvider.,
12876
- [AuthProvider.TWITTER]: OAuthProvider.TWITTER,
12877
- };
12878
- function getOAuthProvider(provider) {
12879
- if (!providerToAuthProvider[provider]) {
12880
- throw new Error(`Unsupported OAuth provider: ${provider}`);
12881
- }
12882
- return providerToAuthProvider[provider];
12883
- }
12884
- const useOAuth = (hookOptions = {}) => {
12885
- const { client, updateUser } = useOpenfort();
12886
- const [status, setStatus] = useState({
12887
- status: "idle",
12888
- });
12889
- const initOAuth = useCallback(async (options) => {
12890
- var _a;
12891
- const authProvider = options.provider;
12892
- try {
12893
- setStatus({
12894
- status: 'loading',
12895
- });
12896
- await client.auth.initOAuth({
12897
- provider: getOAuthProvider(authProvider),
12898
- options: {
12899
- redirectTo: buildCallbackUrl({
12900
- provider: authProvider,
12901
- callbackUrl: (_a = hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.redirectTo) !== null && _a !== void 0 ? _a : options === null || options === void 0 ? void 0 : options.redirectTo,
12902
- })
12903
- },
12904
- });
12905
- return onSuccess({
12906
- data: {},
12907
- hookOptions,
12908
- options,
12909
- });
12910
- }
12911
- catch (e) {
12912
- const error = new OpenfortKitError("Failed to login with OAuth", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error: e });
12913
- setStatus({
12914
- status: 'error',
12915
- error
12916
- });
12917
- return onError({
12918
- hookOptions,
12919
- options,
12920
- error,
12921
- });
12922
- }
12923
- }, [client, setStatus, updateUser, hookOptions]);
12924
- const linkOauth = useCallback(async (options) => {
12925
- var _a;
12926
- const authProvider = options.provider;
12927
- try {
12928
- setStatus({
12929
- status: 'loading',
12930
- });
12931
- const authToken = await client.getAccessToken();
12932
- if (!authToken) {
12933
- throw new OpenfortKitError("No auth token found", OpenfortKitErrorType.AUTHENTICATION_ERROR);
12934
- }
12935
- await client.auth.initLinkOAuth({
12936
- authToken,
12937
- provider: getOAuthProvider(authProvider),
12938
- options: {
12939
- redirectTo: buildCallbackUrl({
12940
- provider: authProvider,
12941
- callbackUrl: (_a = options === null || options === void 0 ? void 0 : options.redirectTo) !== null && _a !== void 0 ? _a : hookOptions === null || hookOptions === void 0 ? void 0 : hookOptions.redirectTo,
12942
- })
12943
- },
12944
- });
12945
- return onSuccess({
12946
- data: {},
12947
- hookOptions,
12948
- options,
12949
- });
12950
- }
12951
- catch (e) {
12952
- const error = new OpenfortKitError("Failed to link OAuth", OpenfortKitErrorType.AUTHENTICATION_ERROR, { error: e });
12953
- setStatus({
12954
- status: 'error',
12955
- error
12956
- });
12957
- return onError({
12958
- hookOptions,
12959
- options,
12960
- error,
12961
- });
12962
- }
12963
- }, [client, setStatus, updateUser, hookOptions]);
12964
- return {
12965
- initOAuth,
12966
- linkOauth,
12967
- ...mapStatus(status),
12968
- };
12969
- };
12970
-
12971
- function useSignOut(hookOptions = {}) {
12972
- const { client, updateUser, user } = useOpenfort();
12973
- const [status, setStatus] = useState({
12974
- status: "idle",
12975
- });
12976
- const { disconnect } = useDisconnect();
12977
- const signOut = useCallback(async (options = {}) => {
12978
- if (!user)
12979
- return;
12980
- setStatus({
12981
- status: 'loading',
12982
- });
12983
- try {
12984
- await client.auth.logout();
12985
- disconnect();
12986
- updateUser();
12987
- setStatus({
12988
- status: 'success',
12989
- });
12990
- return onSuccess({
12991
- hookOptions,
12992
- options,
12993
- data: {},
12994
- });
12995
- }
12996
- catch (error) {
12997
- setStatus({
12998
- status: 'error',
12999
- error: new OpenfortKitError('Failed to sign out', OpenfortKitErrorType.AUTHENTICATION_ERROR, { error }),
13000
- });
13001
- throw error;
13002
- }
13003
- }, [client, user, disconnect, updateUser, setStatus, hookOptions]);
13004
- return {
13005
- ...mapStatus(status),
13006
- signOut,
13007
- };
13008
- }
13009
-
13010
- export { AuthProvider, Avatar, Chain as ChainIcon, OPENFORTKIT_VERSION, OpenfortKitButton, OpenfortKitContext, OpenfortKitError, OpenfortKitErrorType, OpenfortKitProvider, OpenfortKitStatus, embeddedWalletId, defaultConfig as getDefaultConfig, defaultConnectors as getDefaultConnectors, useAuthCallback, useChainIsSupported, useChains, useConnectWithSiwe, useEmailAuth, useGuestAuth, useOAuth, useOpenfort, useSignOut, useStatus, useUI, useUser, useWalletAuth, useWallets, wallets };
13192
+ export { AuthProvider, Avatar, Chain as ChainIcon, OPENFORTKIT_VERSION, OpenfortError, OpenfortErrorType, OpenfortKitButton, OpenfortKitContext, OpenfortKitStatus, OpenfortProvider, embeddedWalletId, defaultConfig as getDefaultConfig, defaultConnectors as getDefaultConnectors, useAuthCallback, useChainIsSupported, useChains, useConnectWithSiwe, useEmailAuth, useGuestAuth, useOAuth, useOpenfortCore as useOpenfort, useSignOut, useStatus, useUI, useUser, useWallet, useWalletAuth, useWallets, wallets };
13011
13193
  //# sourceMappingURL=index.es.js.map