@b3dotfun/sdk 0.0.88-alpha.2 → 0.0.88-alpha.4

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 (31) hide show
  1. package/dist/cjs/global-account/react/components/B3Provider/RelayKitProviderWrapper.js +3 -1
  2. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +76 -20
  3. package/dist/cjs/global-account/react/components/TurnkeyAuthModal.js +3 -1
  4. package/dist/cjs/global-account/react/hooks/index.d.ts +1 -0
  5. package/dist/cjs/global-account/react/hooks/index.js +3 -1
  6. package/dist/cjs/global-account/react/hooks/useAuth.d.ts +76 -0
  7. package/dist/cjs/global-account/react/hooks/useAuth.js +338 -0
  8. package/dist/cjs/global-account/react/hooks/useTWAuth.d.ts +3 -0
  9. package/dist/cjs/global-account/react/hooks/useTWAuth.js +8 -0
  10. package/dist/cjs/global-account/react/hooks/useTurnkeyAuth.js +50 -22
  11. package/dist/esm/global-account/react/components/B3Provider/RelayKitProviderWrapper.js +3 -1
  12. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +76 -20
  13. package/dist/esm/global-account/react/components/TurnkeyAuthModal.js +5 -3
  14. package/dist/esm/global-account/react/hooks/index.d.ts +1 -0
  15. package/dist/esm/global-account/react/hooks/index.js +1 -0
  16. package/dist/esm/global-account/react/hooks/useAuth.d.ts +76 -0
  17. package/dist/esm/global-account/react/hooks/useAuth.js +332 -0
  18. package/dist/esm/global-account/react/hooks/useTWAuth.d.ts +3 -0
  19. package/dist/esm/global-account/react/hooks/useTWAuth.js +8 -0
  20. package/dist/esm/global-account/react/hooks/useTurnkeyAuth.js +50 -22
  21. package/dist/types/global-account/react/hooks/index.d.ts +1 -0
  22. package/dist/types/global-account/react/hooks/useAuth.d.ts +76 -0
  23. package/dist/types/global-account/react/hooks/useTWAuth.d.ts +3 -0
  24. package/package.json +1 -1
  25. package/src/global-account/react/components/B3Provider/RelayKitProviderWrapper.tsx +4 -1
  26. package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +168 -100
  27. package/src/global-account/react/components/TurnkeyAuthModal.tsx +7 -4
  28. package/src/global-account/react/hooks/index.ts +1 -0
  29. package/src/global-account/react/hooks/useAuth.ts +380 -0
  30. package/src/global-account/react/hooks/useTWAuth.tsx +10 -0
  31. package/src/global-account/react/hooks/useTurnkeyAuth.ts +54 -23
@@ -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
+ }
@@ -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(
@@ -4,7 +4,7 @@ import { useCallback, useState } from "react";
4
4
  import app from "../../app";
5
5
  import { useB3Config } from "../components";
6
6
  import { useAuthStore } from "../stores";
7
- import { useAuthentication } from "./useAuthentication";
7
+ import { useAuth } from "./useAuth";
8
8
 
9
9
  const debug = debugB3React("useTurnkeyAuth");
10
10
 
@@ -34,12 +34,15 @@ export function useTurnkeyAuth(): UseTurnkeyAuthReturn {
34
34
  const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
35
35
  const setIsAuthenticated = useAuthStore(state => state.setIsAuthenticated);
36
36
  const { partnerId } = useB3Config();
37
- const { user } = useAuthentication(partnerId);
37
+ const { authenticate } = useAuth();
38
38
 
39
39
  /**
40
40
  * Step 1: Initiate login with email
41
- * - Calls backend to create sub-org (if needed) and send OTP
41
+ * - Calls backend turnkey-jwt strategy (init action) to create sub-org (if needed) and send OTP
42
42
  * - Returns otpId to use in verification step
43
+ *
44
+ * Note: Uses the turnkey-jwt authentication strategy, not the service directly.
45
+ * The turnkey-jwt strategy handles both OTP flow (init/verify) and final authentication.
43
46
  */
44
47
  const initiateLogin = useCallback(
45
48
  async (email: string): Promise<TurnkeyAuthInitResponse> => {
@@ -48,20 +51,44 @@ export function useTurnkeyAuth(): UseTurnkeyAuthReturn {
48
51
  setIsAuthenticating(true);
49
52
 
50
53
  try {
51
- if (!user?.userId) {
52
- throw new Error("User ID is required to initiate Turnkey login.");
53
- }
54
54
  debug(`Initiating login for: ${email}`);
55
55
 
56
- // Call FeathersJS service to initialize OTP
57
- const data: TurnkeyAuthInitResponse = await app.service("turnkey-auth").init({ email, userId: user.userId });
56
+ // Use authentication service with turnkey-jwt strategy (init action)
57
+ // userId is resolved from authentication context on the backend (params.user.userId)
58
+ // Backend will get userId from _params.user?.userId if authenticated, or handle unauthenticated case
59
+ // So we only need to send email
60
+ debug(`Calling app.authenticate with turnkey-jwt strategy (init action)`, { email });
61
+
62
+ const response = await app.authenticate({
63
+ strategy: "turnkey-jwt",
64
+ action: "init",
65
+ email,
66
+ } as any);
67
+
68
+ // The strategy returns the TurnkeyAuthInitResponse directly
69
+ const data = response as unknown as TurnkeyAuthInitResponse;
58
70
 
59
71
  debug(`OTP initialized successfully. OtpId: ${data.otpId}`);
60
72
 
61
73
  return data;
62
74
  } catch (err: any) {
63
75
  debug("Error initiating login:", err);
64
- const errorMessage = err.message || "Failed to send OTP email. Please try again.";
76
+
77
+ // Provide more detailed error information
78
+ let errorMessage = "Failed to send OTP email. Please try again.";
79
+
80
+ if (err.message) {
81
+ errorMessage = err.message;
82
+ } else if (err.name === "TypeError" && err.message?.includes("fetch")) {
83
+ errorMessage = "Network error: Unable to reach the server. Please check your connection and try again.";
84
+ } else if (err.code === "ECONNREFUSED" || err.code === "ENOTFOUND") {
85
+ errorMessage = "Connection error: Unable to reach the server. Please check your internet connection.";
86
+ } else if (err.response) {
87
+ // FeathersJS error response
88
+ errorMessage = err.response.message || err.message || errorMessage;
89
+ debug("FeathersJS error response:", err.response);
90
+ }
91
+
65
92
  setError(errorMessage);
66
93
  throw err;
67
94
  } finally {
@@ -69,13 +96,13 @@ export function useTurnkeyAuth(): UseTurnkeyAuthReturn {
69
96
  setIsAuthenticating(false);
70
97
  }
71
98
  },
72
- [user, setIsAuthenticating],
99
+ [setIsAuthenticating],
73
100
  );
74
101
 
75
102
  /**
76
103
  * Step 2: Verify OTP and authenticate
77
- * - Verifies OTP with backend
78
- * - Gets Turnkey session JWT
104
+ * - Verifies OTP with backend via turnkey-jwt strategy (verify action)
105
+ * - Gets Turnkey session JWT from the verify response
79
106
  * - Authenticates with b3-api using "turnkey-jwt" strategy
80
107
  * - JWT automatically stored in cookies by SDK
81
108
  */
@@ -86,22 +113,26 @@ export function useTurnkeyAuth(): UseTurnkeyAuthReturn {
86
113
  setIsAuthenticating(true);
87
114
 
88
115
  try {
89
- debug(`Verifying OTP...`, { userId: user?.userId });
116
+ debug(`Verifying OTP...`, { otpId });
90
117
 
91
- // Step 1: Verify OTP and get Turnkey session JWT
92
- const { turnkeySessionJwt }: TurnkeyVerifyResponse = await app.service("turnkey-auth").verify({
118
+ // Step 1: Verify OTP with backend using turnkey-jwt strategy (verify action)
119
+ // This returns the Turnkey session JWT
120
+ const response = await app.authenticate({
121
+ strategy: "turnkey-jwt",
122
+ action: "verify",
93
123
  otpId,
94
124
  otpCode,
95
- });
125
+ } as any);
96
126
 
97
- debug(`OTP verified! Authenticating with b3-api...`);
127
+ // The strategy returns the TurnkeyAuthVerifyResponse directly
128
+ const verifyResult = response as unknown as TurnkeyVerifyResponse;
129
+ const { turnkeySessionJwt } = verifyResult;
130
+
131
+ debug(`OTP verified! Got Turnkey session JWT. Authenticating with b3-api...`);
98
132
 
99
133
  // Step 2: Authenticate with b3-api using Turnkey JWT
100
- // The SDK will automatically store the b3-api JWT in cookies
101
- const authResult = await app.authenticate({
102
- strategy: "turnkey-jwt",
103
- accessToken: turnkeySessionJwt,
104
- } as any);
134
+ // Use the unified useAuth hook for authentication with "turnkey-jwt" strategy
135
+ const authResult = await authenticate(turnkeySessionJwt, partnerId || "");
105
136
 
106
137
  debug(`Successfully authenticated with b3-api!`, authResult);
107
138
 
@@ -123,7 +154,7 @@ export function useTurnkeyAuth(): UseTurnkeyAuthReturn {
123
154
  setIsAuthenticating(false);
124
155
  }
125
156
  },
126
- [user, setIsAuthenticating, setIsAuthenticated],
157
+ [partnerId, setIsAuthenticating, setIsAuthenticated, authenticate],
127
158
  );
128
159
 
129
160
  const clearError = useCallback(() => {