@b3dotfun/sdk 0.0.82-alpha.3 → 0.0.83-test.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 (53) hide show
  1. package/dist/cjs/anyspend/react/components/AnySpend.js +3 -0
  2. package/dist/cjs/anyspend/react/components/AnySpendCustom.js +3 -0
  3. package/dist/cjs/anyspend/react/components/common/CryptoPaymentMethod.js +177 -47
  4. package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.js +3 -0
  5. package/dist/cjs/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +10 -0
  6. package/dist/cjs/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.js +73 -0
  7. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +57 -12
  8. package/dist/cjs/global-account/react/hooks/index.d.ts +1 -0
  9. package/dist/cjs/global-account/react/hooks/index.js +3 -1
  10. package/dist/cjs/global-account/react/hooks/useAuth.d.ts +76 -0
  11. package/dist/cjs/global-account/react/hooks/useAuth.js +338 -0
  12. package/dist/cjs/global-account/react/hooks/useAuthentication.d.ts +2 -2
  13. package/dist/cjs/global-account/react/hooks/useAuthentication.js +72 -63
  14. package/dist/cjs/global-account/react/hooks/useTWAuth.d.ts +3 -0
  15. package/dist/cjs/global-account/react/hooks/useTWAuth.js +8 -0
  16. package/dist/cjs/global-account/react/hooks/useTurnkeyAuth.js +54 -24
  17. package/dist/cjs/global-account/react/hooks/useUserQuery.d.ts +1 -1
  18. package/dist/esm/anyspend/react/components/AnySpend.js +3 -0
  19. package/dist/esm/anyspend/react/components/AnySpendCustom.js +3 -0
  20. package/dist/esm/anyspend/react/components/common/CryptoPaymentMethod.js +175 -45
  21. package/dist/esm/anyspend/react/hooks/useAnyspendFlow.js +3 -0
  22. package/dist/esm/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +10 -0
  23. package/dist/esm/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.js +70 -0
  24. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +57 -12
  25. package/dist/esm/global-account/react/hooks/index.d.ts +1 -0
  26. package/dist/esm/global-account/react/hooks/index.js +1 -0
  27. package/dist/esm/global-account/react/hooks/useAuth.d.ts +76 -0
  28. package/dist/esm/global-account/react/hooks/useAuth.js +332 -0
  29. package/dist/esm/global-account/react/hooks/useAuthentication.d.ts +2 -2
  30. package/dist/esm/global-account/react/hooks/useAuthentication.js +72 -63
  31. package/dist/esm/global-account/react/hooks/useTWAuth.d.ts +3 -0
  32. package/dist/esm/global-account/react/hooks/useTWAuth.js +8 -0
  33. package/dist/esm/global-account/react/hooks/useTurnkeyAuth.js +54 -24
  34. package/dist/esm/global-account/react/hooks/useUserQuery.d.ts +1 -1
  35. package/dist/styles/index.css +1 -1
  36. package/dist/types/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +10 -0
  37. package/dist/types/global-account/react/hooks/index.d.ts +1 -0
  38. package/dist/types/global-account/react/hooks/useAuth.d.ts +76 -0
  39. package/dist/types/global-account/react/hooks/useAuthentication.d.ts +2 -2
  40. package/dist/types/global-account/react/hooks/useTWAuth.d.ts +3 -0
  41. package/dist/types/global-account/react/hooks/useUserQuery.d.ts +1 -1
  42. package/package.json +1 -1
  43. package/src/anyspend/react/components/AnySpend.tsx +4 -0
  44. package/src/anyspend/react/components/AnySpendCustom.tsx +4 -0
  45. package/src/anyspend/react/components/common/CryptoPaymentMethod.tsx +253 -22
  46. package/src/anyspend/react/hooks/useAnyspendFlow.ts +4 -0
  47. package/src/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.ts +80 -0
  48. package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +77 -22
  49. package/src/global-account/react/hooks/index.ts +1 -0
  50. package/src/global-account/react/hooks/useAuth.ts +380 -0
  51. package/src/global-account/react/hooks/useAuthentication.ts +88 -85
  52. package/src/global-account/react/hooks/useTWAuth.tsx +10 -0
  53. package/src/global-account/react/hooks/useTurnkeyAuth.ts +59 -26
@@ -0,0 +1,380 @@
1
+ import app from "@b3dotfun/sdk/global-account/app";
2
+ import { authenticateWithB3JWT } from "@b3dotfun/sdk/global-account/bsmnt";
3
+ import { useAuthStore } from "@b3dotfun/sdk/global-account/react";
4
+ import { ecosystemWalletId } from "@b3dotfun/sdk/shared/constants";
5
+ import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
6
+ import { client } from "@b3dotfun/sdk/shared/utils/thirdweb";
7
+ import { ConnectionOptions } from "@thirdweb-dev/wagmi-adapter";
8
+ import { getConnectors } from "@wagmi/core";
9
+ import { useCallback, useContext, useEffect, useRef } from "react";
10
+ import {
11
+ useActiveWallet,
12
+ useAutoConnect,
13
+ useConnectedWallets,
14
+ useDisconnect,
15
+ useSetActiveWallet,
16
+ } from "thirdweb/react";
17
+ import { Wallet, ecosystemWallet } from "thirdweb/wallets";
18
+ import { preAuthenticate } from "thirdweb/wallets/in-app";
19
+ import { useAccount, useConnect, useSwitchAccount } from "wagmi";
20
+ import { LocalSDKContext } from "../components/B3Provider/LocalSDKProvider";
21
+ import { useB3 } from "../components/B3Provider/useB3";
22
+ import { createWagmiConfig } from "../utils/createWagmiConfig";
23
+ import { useSearchParam } from "./useSearchParamsSSR";
24
+ import { useUserQuery } from "./useUserQuery";
25
+
26
+ const debug = debugB3React("useAuth");
27
+
28
+ /**
29
+ * Unified authentication hook that uses Turnkey for authentication
30
+ * This replaces the previous Thirdweb-based authentication
31
+ *
32
+ * This hook provides 1:1 feature parity with useAuthentication.ts
33
+ */
34
+ export function useAuth() {
35
+ const { onConnectCallback } = useContext(LocalSDKContext);
36
+ const { disconnect } = useDisconnect();
37
+ const wallets = useConnectedWallets();
38
+ const activeWallet = useActiveWallet();
39
+ const isAuthenticated = useAuthStore(state => state.isAuthenticated);
40
+ const setIsAuthenticated = useAuthStore(state => state.setIsAuthenticated);
41
+ const setIsConnected = useAuthStore(state => state.setIsConnected);
42
+ const isConnecting = useAuthStore(state => state.isConnecting);
43
+ const isConnected = useAuthStore(state => state.isConnected);
44
+ const isAuthenticating = useAuthStore(state => state.isAuthenticating);
45
+ const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
46
+ const setHasStartedConnecting = useAuthStore(state => state.setHasStartedConnecting);
47
+ const setActiveWallet = useSetActiveWallet();
48
+ const hasStartedConnecting = useAuthStore(state => state.hasStartedConnecting);
49
+ const useAutoConnectLoadingPrevious = useRef(false);
50
+ const referralCode = useSearchParam("referralCode");
51
+ const { partnerId } = useB3();
52
+ const wagmiConfig = createWagmiConfig({ partnerId });
53
+ const { connect } = useConnect();
54
+ const activeWagmiAccount = useAccount();
55
+ const { switchAccount } = useSwitchAccount();
56
+ const { user, setUser } = useUserQuery();
57
+ debug("@@activeWagmiAccount", activeWagmiAccount);
58
+
59
+ const wallet = ecosystemWallet(ecosystemWalletId, {
60
+ partnerId: partnerId,
61
+ });
62
+
63
+ /**
64
+ * Re-authenticate using existing session
65
+ * Also updates user state and authenticates with BSMNT
66
+ */
67
+ const reAuthenticate = useCallback(async () => {
68
+ debug("Re-authenticating...");
69
+ try {
70
+ const response = await app.reAuthenticate();
71
+ debug("Re-authentication successful", response);
72
+
73
+ // Update user state if user data exists
74
+ if (response.user) {
75
+ setUser(response.user);
76
+ debug("User state updated", response.user);
77
+ }
78
+
79
+ // Authenticate with BSMNT
80
+ try {
81
+ const b3Jwt = await authenticateWithB3JWT(response.accessToken);
82
+ debug("BSMNT re-authentication successful", b3Jwt);
83
+ } catch (bsmntError) {
84
+ // BSMNT authentication failure shouldn't block the main auth flow
85
+ debug("BSMNT re-authentication failed (non-critical)", bsmntError);
86
+ }
87
+
88
+ return response;
89
+ } catch (err: any) {
90
+ debug("Re-authentication failed", err);
91
+ throw err;
92
+ }
93
+ }, [setUser]);
94
+
95
+ const syncWagmi = useCallback(async () => {
96
+ function syncWagmiFunc() {
97
+ const connectors = getConnectors(wagmiConfig);
98
+ debug("@@syncWagmi", {
99
+ connectors,
100
+ wallets,
101
+ });
102
+
103
+ // For each that matchs a TW wallet on wallets, connect to the wagmi connector
104
+ // or, since ecosystem wallets is separate, connect those via in-app-wallet from wagmi
105
+ connectors.forEach(async connector => {
106
+ const twWallet = wallets.find(wallet => wallet.id === connector.id || connector.id === "in-app-wallet");
107
+
108
+ // If no TW wallet, do not prompt the user to connect
109
+ if (!twWallet) {
110
+ return;
111
+ }
112
+
113
+ // Metamask will prompt to connect, we can just switch accounts here.
114
+ if (connector.id === "io.metamask") {
115
+ return switchAccount({ connector });
116
+ }
117
+
118
+ if (
119
+ // If it's not an in-app wallet or it is the ecosystem wallet, connect
120
+ connector.id !== "in-app-wallet" ||
121
+ (connector.id === "in-app-wallet" && twWallet.id === ecosystemWalletId)
122
+ ) {
123
+ try {
124
+ const options = {
125
+ wallet: twWallet, // the connected wallet
126
+ } satisfies ConnectionOptions;
127
+ debug("@@syncWagmi:connecting", { twWallet, connector });
128
+ connect({
129
+ connector,
130
+ ...options,
131
+ });
132
+ } catch (error) {
133
+ console.error("@@syncWagmi:error", error);
134
+ }
135
+ } else {
136
+ debug("@@syncWagmi:not-connecting", connector);
137
+ }
138
+ });
139
+ }
140
+ syncWagmiFunc();
141
+ // wagmi config shouldn't change
142
+ // eslint-disable-next-line react-hooks/exhaustive-deps
143
+ }, [partnerId, wallets]);
144
+
145
+ useEffect(() => {
146
+ syncWagmi();
147
+ }, [wallets, syncWagmi]);
148
+
149
+ /**
150
+ * Authenticate user using Turnkey
151
+ * Note: This no longer requires a wallet for authentication.
152
+ * Wallets are still used for signing transactions, but authentication is done via Turnkey email OTP.
153
+ *
154
+ * For backward compatibility, this function still accepts a wallet parameter,
155
+ * but it's not used for authentication anymore.
156
+ */
157
+ const authenticateUser = useCallback(async () => {
158
+ setHasStartedConnecting(true);
159
+
160
+ // Try to re-authenticate first
161
+ try {
162
+ const userAuth = await reAuthenticate();
163
+ setUser(userAuth.user);
164
+ setIsAuthenticated(true);
165
+ setIsAuthenticating(false);
166
+ debug("Re-authenticated successfully", { userAuth });
167
+
168
+ // Authenticate on BSMNT with B3 JWT
169
+ const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
170
+ debug("@@b3Jwt", b3Jwt);
171
+
172
+ return userAuth;
173
+ } catch (error) {
174
+ // If re-authentication fails, user needs to authenticate via Turnkey
175
+ // This should be handled by the Turnkey auth modal/flow
176
+ debug("Re-authentication failed. User needs to authenticate via Turnkey.", error);
177
+ setIsAuthenticated(false);
178
+ setIsAuthenticating(false);
179
+ throw new Error("Authentication required. Please authenticate via Turnkey.");
180
+ }
181
+ }, [reAuthenticate, setIsAuthenticated, setIsAuthenticating, setUser, setHasStartedConnecting]);
182
+
183
+ /**
184
+ * Authenticate with Turnkey using email OTP
185
+ * This is the primary authentication method, replacing Thirdweb wallet-based auth
186
+ *
187
+ * This function:
188
+ * 1. Authenticates with FeathersJS (persists session via cookies)
189
+ * 2. Sets user state in the user store (persists to localStorage)
190
+ * 3. Authenticates with BSMNT for basement integration
191
+ */
192
+ const authenticate = useCallback(
193
+ async (turnkeySessionJwt: string, partnerId: string) => {
194
+ if (!turnkeySessionJwt) {
195
+ throw new Error("Turnkey session JWT is required");
196
+ }
197
+
198
+ debug("Authenticating with Turnkey JWT", { referralCode, partnerId });
199
+
200
+ try {
201
+ // Step 1: Authenticate with FeathersJS (session persisted via cookies)
202
+ const response = await app.authenticate({
203
+ strategy: "turnkey-jwt",
204
+ accessToken: turnkeySessionJwt,
205
+ referralCode,
206
+ partnerId: partnerId,
207
+ } as any);
208
+
209
+ debug("Authentication successful", response);
210
+
211
+ // Step 2: Set user state (persists to localStorage via Zustand)
212
+ if (response.user) {
213
+ setUser(response.user);
214
+ debug("User state updated", response.user);
215
+ }
216
+
217
+ // Step 3: Authenticate with BSMNT for basement integration
218
+ try {
219
+ const b3Jwt = await authenticateWithB3JWT(response.accessToken);
220
+ debug("BSMNT authentication successful", b3Jwt);
221
+ } catch (bsmntError) {
222
+ // BSMNT authentication failure shouldn't block the main auth flow
223
+ debug("BSMNT authentication failed (non-critical)", bsmntError);
224
+ }
225
+
226
+ return response;
227
+ } catch (err: any) {
228
+ debug("Authentication failed", err);
229
+ throw err;
230
+ }
231
+ },
232
+ [referralCode, setUser],
233
+ );
234
+
235
+ /**
236
+ * Handle wallet connection
237
+ * Note: With Turnkey migration, wallet connection is primarily for signing transactions,
238
+ * not for authentication. Authentication should be done separately via Turnkey email OTP.
239
+ */
240
+ /**
241
+ * Handle wallet connection
242
+ * Note: With Turnkey migration, wallet connection is primarily for signing transactions,
243
+ * not for authentication. Authentication should be done separately via Turnkey email OTP.
244
+ */
245
+ const onConnect = useCallback(
246
+ async (_walleAutoConnectedWith: Wallet, allConnectedWallets: Wallet[]) => {
247
+ debug("@@useAuth:onConnect", { _walleAutoConnectedWith, allConnectedWallets });
248
+
249
+ const wallet = allConnectedWallets.find(wallet => wallet.id.startsWith("ecosystem."));
250
+
251
+ if (!wallet) {
252
+ throw new Error("No smart wallet found during auto-connect");
253
+ }
254
+
255
+ debug("@@useAuth:onConnect", { wallet });
256
+
257
+ try {
258
+ setHasStartedConnecting(true);
259
+ setIsConnected(true);
260
+ setIsAuthenticating(true);
261
+ await setActiveWallet(wallet);
262
+
263
+ // Try to authenticate user (will use re-authenticate if session exists)
264
+ // If no session exists, authentication will need to happen via Turnkey flow
265
+ try {
266
+ const userAuth = await authenticateUser();
267
+
268
+ if (userAuth && onConnectCallback) {
269
+ await onConnectCallback(wallet, userAuth.accessToken);
270
+ }
271
+ } catch (authError) {
272
+ // Authentication failed - this is expected if user hasn't authenticated via Turnkey yet
273
+ // The Turnkey auth modal should handle this
274
+ debug("@@useAuth:onConnect:authFailed", { authError });
275
+ // Don't set isAuthenticated to false here - let the Turnkey flow handle it
276
+ }
277
+ } catch (error) {
278
+ debug("@@useAuth:onConnect:failed", { error });
279
+ setIsAuthenticated(false);
280
+ setUser(undefined);
281
+ } finally {
282
+ setIsAuthenticating(false);
283
+ }
284
+
285
+ debug({
286
+ isAuthenticated,
287
+ isAuthenticating,
288
+ isConnected,
289
+ });
290
+ },
291
+ [
292
+ onConnectCallback,
293
+ authenticateUser,
294
+ isAuthenticated,
295
+ isAuthenticating,
296
+ isConnected,
297
+ setActiveWallet,
298
+ setHasStartedConnecting,
299
+ setIsAuthenticated,
300
+ setIsAuthenticating,
301
+ setIsConnected,
302
+ setUser,
303
+ ],
304
+ );
305
+
306
+ const logout = useCallback(
307
+ async (callback?: () => void) => {
308
+ if (activeWallet) {
309
+ debug("@@logout:activeWallet", activeWallet);
310
+ disconnect(activeWallet);
311
+ debug("@@logout:activeWallet", activeWallet);
312
+ }
313
+
314
+ // Log out of each wallet
315
+ wallets.forEach(wallet => {
316
+ console.log("@@logging out", wallet);
317
+ disconnect(wallet);
318
+ });
319
+
320
+ // Delete localStorage thirdweb:connected-wallet-ids
321
+ // https://npc-labs.slack.com/archives/C070E6HNG85/p1750185115273099
322
+ if (typeof localStorage !== "undefined") {
323
+ localStorage.removeItem("thirdweb:connected-wallet-ids");
324
+ localStorage.removeItem("wagmi.store");
325
+ localStorage.removeItem("lastAuthProvider");
326
+ localStorage.removeItem("b3-user");
327
+ }
328
+
329
+ app.logout();
330
+ debug("@@logout:loggedOut");
331
+
332
+ setIsAuthenticated(false);
333
+ setIsConnected(false);
334
+ setUser();
335
+ callback?.();
336
+ },
337
+ [activeWallet, disconnect, wallets, setIsAuthenticated, setUser, setIsConnected],
338
+ );
339
+
340
+ const { isLoading: useAutoConnectLoading } = useAutoConnect({
341
+ client,
342
+ wallets: [wallet],
343
+ onConnect,
344
+ onTimeout: () => {
345
+ logout().catch(error => {
346
+ debug("@@useAuth:logout on timeout failed", { error });
347
+ });
348
+ },
349
+ });
350
+
351
+ /**
352
+ * useAutoConnectLoading starts as false
353
+ */
354
+ useEffect(() => {
355
+ if (!useAutoConnectLoading && useAutoConnectLoadingPrevious.current && !hasStartedConnecting) {
356
+ setIsAuthenticating(false);
357
+ }
358
+ useAutoConnectLoadingPrevious.current = useAutoConnectLoading;
359
+ }, [useAutoConnectLoading, hasStartedConnecting, setIsAuthenticating]);
360
+
361
+ const isReady = isAuthenticated && !isAuthenticating;
362
+
363
+ return {
364
+ authenticate,
365
+ reAuthenticate,
366
+ logout,
367
+ isAuthenticated,
368
+ isReady,
369
+ isConnecting,
370
+ isConnected,
371
+ wallet,
372
+ preAuthenticate,
373
+ connect: onConnect,
374
+ isAuthenticating,
375
+ onConnect,
376
+ user,
377
+ refetchUser: authenticateUser,
378
+ setUser,
379
+ };
380
+ }
@@ -19,7 +19,7 @@ import { preAuthenticate } from "thirdweb/wallets/in-app";
19
19
  import { useAccount, useConnect, useSwitchAccount } from "wagmi";
20
20
  import { LocalSDKContext } from "../components/B3Provider/LocalSDKProvider";
21
21
  import { createWagmiConfig } from "../utils/createWagmiConfig";
22
- import { useTWAuth } from "./useTWAuth";
22
+ import { useAuth } from "./useAuth";
23
23
  import { useUserQuery } from "./useUserQuery";
24
24
 
25
25
  const debug = debugB3React("useAuthentication");
@@ -39,7 +39,7 @@ export function useAuthentication(partnerId: string) {
39
39
  const setHasStartedConnecting = useAuthStore(state => state.setHasStartedConnecting);
40
40
  const setActiveWallet = useSetActiveWallet();
41
41
  const hasStartedConnecting = useAuthStore(state => state.hasStartedConnecting);
42
- const { authenticate } = useTWAuth();
42
+ const { reAuthenticate } = useAuth();
43
43
  const { user, setUser } = useUserQuery();
44
44
  const useAutoConnectLoadingPrevious = useRef(false);
45
45
  const wagmiConfig = createWagmiConfig({ partnerId });
@@ -106,49 +106,104 @@ export function useAuthentication(partnerId: string) {
106
106
  syncWagmi();
107
107
  }, [wallets, syncWagmi]);
108
108
 
109
- const authenticateUser = useCallback(
110
- async (wallet?: Wallet) => {
111
- setHasStartedConnecting(true);
109
+ /**
110
+ * Authenticate user using Turnkey
111
+ * Note: This no longer requires a wallet for authentication.
112
+ * Wallets are still used for signing transactions, but authentication is done via Turnkey email OTP.
113
+ *
114
+ * For backward compatibility, this function still accepts a wallet parameter,
115
+ * but it's not used for authentication anymore.
116
+ */
117
+ const authenticateUser = useCallback(async () => {
118
+ setHasStartedConnecting(true);
119
+
120
+ // Try to re-authenticate first
121
+ try {
122
+ const userAuth = await reAuthenticate();
123
+ setUser(userAuth.user);
124
+ setIsAuthenticated(true);
125
+ setIsAuthenticating(false);
126
+ debug("Re-authenticated successfully", { userAuth });
127
+
128
+ // Authenticate on BSMNT with B3 JWT
129
+ const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
130
+ debug("@@b3Jwt", b3Jwt);
131
+
132
+ return userAuth;
133
+ } catch (error) {
134
+ // If re-authentication fails, user needs to authenticate via Turnkey
135
+ // This should be handled by the Turnkey auth modal/flow
136
+ debug("Re-authentication failed. User needs to authenticate via Turnkey.", error);
137
+ setIsAuthenticated(false);
138
+ setIsAuthenticating(false);
139
+ throw new Error("Authentication required. Please authenticate via Turnkey.");
140
+ }
141
+ }, [reAuthenticate, setIsAuthenticated, setIsAuthenticating, setUser, setHasStartedConnecting]);
142
+
143
+ /**
144
+ * Handle wallet connection
145
+ * Note: With Turnkey migration, wallet connection is primarily for signing transactions,
146
+ * not for authentication. Authentication should be done separately via Turnkey email OTP.
147
+ */
148
+ const onConnect = useCallback(
149
+ async (_walleAutoConnectedWith: Wallet, allConnectedWallets: Wallet[]) => {
150
+ debug("@@useAuthentication:onConnect", { _walleAutoConnectedWith, allConnectedWallets });
151
+
152
+ const wallet = allConnectedWallets.find(wallet => wallet.id.startsWith("ecosystem."));
112
153
 
113
154
  if (!wallet) {
114
- throw new Error("No wallet found during auto-connect");
155
+ throw new Error("No smart wallet found during auto-connect");
115
156
  }
116
157
 
117
- const account = wallet ? wallet.getAccount() : activeWallet?.getAccount();
118
- if (!account) {
119
- throw new Error("No account found during auto-connect");
120
- }
158
+ debug("@@useAuthentication:onConnect", { wallet });
121
159
 
122
- // Try to re-authenticate first
123
160
  try {
124
- const userAuth = await app.reAuthenticate();
125
- setUser(userAuth.user);
126
- setIsAuthenticated(true);
127
- setIsAuthenticating(false);
128
- debug("Re-authenticated successfully", { userAuth });
161
+ setHasStartedConnecting(true);
162
+ setIsConnected(true);
163
+ setIsAuthenticating(true);
164
+ await setActiveWallet(wallet);
129
165
 
130
- // Authenticate on BSMNT with B3 JWT
131
- const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
132
- debug("@@b3Jwt", b3Jwt);
166
+ // Try to authenticate user (will use re-authenticate if session exists)
167
+ // If no session exists, authentication will need to happen via Turnkey flow
168
+ try {
169
+ const userAuth = await authenticateUser();
133
170
 
134
- return userAuth;
171
+ if (userAuth && onConnectCallback) {
172
+ await onConnectCallback(wallet, userAuth.accessToken);
173
+ }
174
+ } catch (authError) {
175
+ // Authentication failed - this is expected if user hasn't authenticated via Turnkey yet
176
+ // The Turnkey auth modal should handle this
177
+ debug("@@useAuthentication:onConnect:authFailed", { authError });
178
+ // Don't set isAuthenticated to false here - let the Turnkey flow handle it
179
+ }
135
180
  } catch (error) {
136
- // If re-authentication fails, try fresh authentication
137
- debug("Re-authentication failed, attempting fresh authentication");
138
- const userAuth = await authenticate(wallet, partnerId);
139
- setUser(userAuth.user);
140
- setIsAuthenticated(true);
181
+ debug("@@useAuthentication:onConnect:failed", { error });
182
+ setIsAuthenticated(false);
183
+ setUser(undefined);
184
+ } finally {
141
185
  setIsAuthenticating(false);
142
- debug("Fresh authentication successful", { userAuth });
143
-
144
- // Authenticate on BSMNT with B3 JWT
145
- const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
146
- debug("@@b3Jwt", b3Jwt);
147
-
148
- return userAuth;
149
186
  }
187
+
188
+ debug({
189
+ isAuthenticated,
190
+ isAuthenticating,
191
+ isConnected,
192
+ });
150
193
  },
151
- [activeWallet, partnerId, authenticate, setIsAuthenticated, setIsAuthenticating, setUser, setHasStartedConnecting],
194
+ [
195
+ onConnectCallback,
196
+ authenticateUser,
197
+ isAuthenticated,
198
+ isAuthenticating,
199
+ isConnected,
200
+ setActiveWallet,
201
+ setHasStartedConnecting,
202
+ setIsAuthenticated,
203
+ setIsAuthenticating,
204
+ setIsConnected,
205
+ setUser,
206
+ ],
152
207
  );
153
208
 
154
209
  const logout = useCallback(
@@ -185,58 +240,6 @@ export function useAuthentication(partnerId: string) {
185
240
  [activeWallet, disconnect, wallets, setIsAuthenticated, setUser, setIsConnected],
186
241
  );
187
242
 
188
- const onConnect = useCallback(
189
- async (_walleAutoConnectedWith: Wallet, allConnectedWallets: Wallet[]) => {
190
- debug("@@useAuthentication:onConnect", { _walleAutoConnectedWith, allConnectedWallets });
191
- try {
192
- const wallet = allConnectedWallets.find(wallet => wallet.id.startsWith("ecosystem."));
193
-
194
- if (!wallet) {
195
- throw new Error("No smart wallet found during auto-connect");
196
- }
197
-
198
- debug("@@useAuthentication:onConnect", { wallet });
199
- setHasStartedConnecting(true);
200
- setIsConnected(true);
201
- setIsAuthenticating(true);
202
- await setActiveWallet(wallet);
203
- const userAuth = await authenticateUser(wallet);
204
-
205
- if (userAuth && onConnectCallback) {
206
- await onConnectCallback(wallet, userAuth.accessToken);
207
- }
208
- } catch (error) {
209
- debug("@@useAuthentication:onConnect:failed", { error });
210
- setIsAuthenticated(false);
211
- setUser(undefined);
212
-
213
- await logout();
214
- } finally {
215
- setIsAuthenticating(false);
216
- }
217
-
218
- debug({
219
- isAuthenticated,
220
- isAuthenticating,
221
- isConnected,
222
- });
223
- },
224
- [
225
- isAuthenticated,
226
- isAuthenticating,
227
- isConnected,
228
- setHasStartedConnecting,
229
- setIsConnected,
230
- setIsAuthenticating,
231
- setActiveWallet,
232
- authenticateUser,
233
- onConnectCallback,
234
- setIsAuthenticated,
235
- setUser,
236
- logout,
237
- ],
238
- );
239
-
240
243
  const { isLoading: useAutoConnectLoading } = useAutoConnect({
241
244
  client,
242
245
  wallets: [wallet],
@@ -1,10 +1,20 @@
1
+ /**
2
+ * @deprecated This hook is deprecated. Use useAuth() with Turnkey authentication instead.
3
+ * This file is kept for backward compatibility but should not be used in new code.
4
+ */
1
5
  import app from "@b3dotfun/sdk/global-account/app";
2
6
  import debug from "@b3dotfun/sdk/shared/utils/debug";
3
7
  import { useCallback } from "react";
4
8
  import { Wallet } from "thirdweb/wallets";
5
9
  import { useSearchParam } from "./useSearchParamsSSR";
6
10
 
11
+ /**
12
+ * @deprecated Use useAuth() with Turnkey authentication instead
13
+ */
7
14
  export function useTWAuth() {
15
+ console.warn(
16
+ "useTWAuth is deprecated. Please migrate to useAuth() with Turnkey authentication. See useTurnkeyAuth.ts for the new implementation.",
17
+ );
8
18
  const referralCode = useSearchParam("referralCode");
9
19
 
10
20
  const authenticate = useCallback(