@b3dotfun/sdk 0.0.88-alpha.2 → 0.0.88-alpha.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 (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 +80 -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 +81 -21
  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 +170 -99
  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,338 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.useAuth = useAuth;
7
+ const app_1 = __importDefault(require("../../../global-account/app"));
8
+ const bsmnt_1 = require("../../../global-account/bsmnt");
9
+ const react_1 = require("../../../global-account/react");
10
+ const constants_1 = require("../../../shared/constants");
11
+ const debug_1 = require("../../../shared/utils/debug");
12
+ const thirdweb_1 = require("../../../shared/utils/thirdweb");
13
+ const core_1 = require("@wagmi/core");
14
+ const react_2 = require("react");
15
+ const react_3 = require("thirdweb/react");
16
+ const wallets_1 = require("thirdweb/wallets");
17
+ const in_app_1 = require("thirdweb/wallets/in-app");
18
+ const wagmi_1 = require("wagmi");
19
+ const LocalSDKProvider_1 = require("../components/B3Provider/LocalSDKProvider");
20
+ const useB3_1 = require("../components/B3Provider/useB3");
21
+ const createWagmiConfig_1 = require("../utils/createWagmiConfig");
22
+ const useSearchParamsSSR_1 = require("./useSearchParamsSSR");
23
+ const useUserQuery_1 = require("./useUserQuery");
24
+ const debug = (0, debug_1.debugB3React)("useAuth");
25
+ /**
26
+ * Unified authentication hook that uses Turnkey for authentication
27
+ * This replaces the previous Thirdweb-based authentication
28
+ *
29
+ * This hook provides 1:1 feature parity with useAuthentication.ts
30
+ */
31
+ function useAuth() {
32
+ const { onConnectCallback } = (0, react_2.useContext)(LocalSDKProvider_1.LocalSDKContext);
33
+ const { disconnect } = (0, react_3.useDisconnect)();
34
+ const wallets = (0, react_3.useConnectedWallets)();
35
+ const activeWallet = (0, react_3.useActiveWallet)();
36
+ const isAuthenticated = (0, react_1.useAuthStore)(state => state.isAuthenticated);
37
+ const setIsAuthenticated = (0, react_1.useAuthStore)(state => state.setIsAuthenticated);
38
+ const setIsConnected = (0, react_1.useAuthStore)(state => state.setIsConnected);
39
+ const isConnecting = (0, react_1.useAuthStore)(state => state.isConnecting);
40
+ const isConnected = (0, react_1.useAuthStore)(state => state.isConnected);
41
+ const isAuthenticating = (0, react_1.useAuthStore)(state => state.isAuthenticating);
42
+ const setIsAuthenticating = (0, react_1.useAuthStore)(state => state.setIsAuthenticating);
43
+ const setHasStartedConnecting = (0, react_1.useAuthStore)(state => state.setHasStartedConnecting);
44
+ const setActiveWallet = (0, react_3.useSetActiveWallet)();
45
+ const hasStartedConnecting = (0, react_1.useAuthStore)(state => state.hasStartedConnecting);
46
+ const useAutoConnectLoadingPrevious = (0, react_2.useRef)(false);
47
+ const referralCode = (0, useSearchParamsSSR_1.useSearchParam)("referralCode");
48
+ const { partnerId } = (0, useB3_1.useB3)();
49
+ const wagmiConfig = (0, createWagmiConfig_1.createWagmiConfig)({ partnerId });
50
+ const { connect } = (0, wagmi_1.useConnect)();
51
+ const activeWagmiAccount = (0, wagmi_1.useAccount)();
52
+ const { switchAccount } = (0, wagmi_1.useSwitchAccount)();
53
+ const { user, setUser } = (0, useUserQuery_1.useUserQuery)();
54
+ debug("@@activeWagmiAccount", activeWagmiAccount);
55
+ const wallet = (0, wallets_1.ecosystemWallet)(constants_1.ecosystemWalletId, {
56
+ partnerId: partnerId,
57
+ });
58
+ /**
59
+ * Re-authenticate using existing session
60
+ * Also updates user state and authenticates with BSMNT
61
+ */
62
+ const reAuthenticate = (0, react_2.useCallback)(async () => {
63
+ debug("Re-authenticating...");
64
+ try {
65
+ const response = await app_1.default.reAuthenticate();
66
+ debug("Re-authentication successful", response);
67
+ // Update user state if user data exists
68
+ if (response.user) {
69
+ setUser(response.user);
70
+ debug("User state updated", response.user);
71
+ }
72
+ // Authenticate with BSMNT
73
+ try {
74
+ const b3Jwt = await (0, bsmnt_1.authenticateWithB3JWT)(response.accessToken);
75
+ debug("BSMNT re-authentication successful", b3Jwt);
76
+ }
77
+ catch (bsmntError) {
78
+ // BSMNT authentication failure shouldn't block the main auth flow
79
+ debug("BSMNT re-authentication failed (non-critical)", bsmntError);
80
+ }
81
+ return response;
82
+ }
83
+ catch (err) {
84
+ debug("Re-authentication failed", err);
85
+ throw err;
86
+ }
87
+ }, [setUser]);
88
+ const syncWagmi = (0, react_2.useCallback)(async () => {
89
+ function syncWagmiFunc() {
90
+ const connectors = (0, core_1.getConnectors)(wagmiConfig);
91
+ debug("@@syncWagmi", {
92
+ connectors,
93
+ wallets,
94
+ });
95
+ // For each that matchs a TW wallet on wallets, connect to the wagmi connector
96
+ // or, since ecosystem wallets is separate, connect those via in-app-wallet from wagmi
97
+ connectors.forEach(async (connector) => {
98
+ const twWallet = wallets.find(wallet => wallet.id === connector.id || connector.id === "in-app-wallet");
99
+ // If no TW wallet, do not prompt the user to connect
100
+ if (!twWallet) {
101
+ return;
102
+ }
103
+ // Metamask will prompt to connect, we can just switch accounts here.
104
+ if (connector.id === "io.metamask") {
105
+ return switchAccount({ connector });
106
+ }
107
+ if (
108
+ // If it's not an in-app wallet or it is the ecosystem wallet, connect
109
+ connector.id !== "in-app-wallet" ||
110
+ (connector.id === "in-app-wallet" && twWallet.id === constants_1.ecosystemWalletId)) {
111
+ try {
112
+ const options = {
113
+ wallet: twWallet, // the connected wallet
114
+ };
115
+ debug("@@syncWagmi:connecting", { twWallet, connector });
116
+ connect({
117
+ connector,
118
+ ...options,
119
+ });
120
+ }
121
+ catch (error) {
122
+ console.error("@@syncWagmi:error", error);
123
+ }
124
+ }
125
+ else {
126
+ debug("@@syncWagmi:not-connecting", connector);
127
+ }
128
+ });
129
+ }
130
+ syncWagmiFunc();
131
+ // wagmi config shouldn't change
132
+ // eslint-disable-next-line react-hooks/exhaustive-deps
133
+ }, [partnerId, wallets]);
134
+ (0, react_2.useEffect)(() => {
135
+ syncWagmi();
136
+ }, [wallets, syncWagmi]);
137
+ /**
138
+ * Authenticate user using Turnkey
139
+ * Note: This no longer requires a wallet for authentication.
140
+ * Wallets are still used for signing transactions, but authentication is done via Turnkey email OTP.
141
+ *
142
+ * For backward compatibility, this function still accepts a wallet parameter,
143
+ * but it's not used for authentication anymore.
144
+ */
145
+ const authenticateUser = (0, react_2.useCallback)(async () => {
146
+ setHasStartedConnecting(true);
147
+ // Try to re-authenticate first
148
+ try {
149
+ const userAuth = await reAuthenticate();
150
+ setUser(userAuth.user);
151
+ setIsAuthenticated(true);
152
+ setIsAuthenticating(false);
153
+ debug("Re-authenticated successfully", { userAuth });
154
+ // Authenticate on BSMNT with B3 JWT
155
+ const b3Jwt = await (0, bsmnt_1.authenticateWithB3JWT)(userAuth.accessToken);
156
+ debug("@@b3Jwt", b3Jwt);
157
+ return userAuth;
158
+ }
159
+ catch (error) {
160
+ // If re-authentication fails, user needs to authenticate via Turnkey
161
+ // This should be handled by the Turnkey auth modal/flow
162
+ debug("Re-authentication failed. User needs to authenticate via Turnkey.", error);
163
+ setIsAuthenticated(false);
164
+ setIsAuthenticating(false);
165
+ throw new Error("Authentication required. Please authenticate via Turnkey.");
166
+ }
167
+ }, [reAuthenticate, setIsAuthenticated, setIsAuthenticating, setUser, setHasStartedConnecting]);
168
+ /**
169
+ * Authenticate with Turnkey using email OTP
170
+ * This is the primary authentication method, replacing Thirdweb wallet-based auth
171
+ *
172
+ * This function:
173
+ * 1. Authenticates with FeathersJS (persists session via cookies)
174
+ * 2. Sets user state in the user store (persists to localStorage)
175
+ * 3. Authenticates with BSMNT for basement integration
176
+ */
177
+ const authenticate = (0, react_2.useCallback)(async (turnkeySessionJwt, partnerId) => {
178
+ if (!turnkeySessionJwt) {
179
+ throw new Error("Turnkey session JWT is required");
180
+ }
181
+ debug("Authenticating with Turnkey JWT", { referralCode, partnerId });
182
+ try {
183
+ // Step 1: Authenticate with FeathersJS (session persisted via cookies)
184
+ const response = await app_1.default.authenticate({
185
+ strategy: "turnkey-jwt",
186
+ accessToken: turnkeySessionJwt,
187
+ referralCode,
188
+ partnerId: partnerId,
189
+ });
190
+ debug("Authentication successful", response);
191
+ // Step 2: Set user state (persists to localStorage via Zustand)
192
+ if (response.user) {
193
+ setUser(response.user);
194
+ debug("User state updated", response.user);
195
+ }
196
+ // Step 3: Authenticate with BSMNT for basement integration
197
+ try {
198
+ const b3Jwt = await (0, bsmnt_1.authenticateWithB3JWT)(response.accessToken);
199
+ debug("BSMNT authentication successful", b3Jwt);
200
+ }
201
+ catch (bsmntError) {
202
+ // BSMNT authentication failure shouldn't block the main auth flow
203
+ debug("BSMNT authentication failed (non-critical)", bsmntError);
204
+ }
205
+ return response;
206
+ }
207
+ catch (err) {
208
+ debug("Authentication failed", err);
209
+ throw err;
210
+ }
211
+ }, [referralCode, setUser]);
212
+ /**
213
+ * Handle wallet connection
214
+ * Note: With Turnkey migration, wallet connection is primarily for signing transactions,
215
+ * not for authentication. Authentication should be done separately via Turnkey email OTP.
216
+ */
217
+ /**
218
+ * Handle wallet connection
219
+ * Note: With Turnkey migration, wallet connection is primarily for signing transactions,
220
+ * not for authentication. Authentication should be done separately via Turnkey email OTP.
221
+ */
222
+ const onConnect = (0, react_2.useCallback)(async (_walleAutoConnectedWith, allConnectedWallets) => {
223
+ debug("@@useAuth:onConnect", { _walleAutoConnectedWith, allConnectedWallets });
224
+ const wallet = allConnectedWallets.find(wallet => wallet.id.startsWith("ecosystem."));
225
+ if (!wallet) {
226
+ throw new Error("No smart wallet found during auto-connect");
227
+ }
228
+ debug("@@useAuth:onConnect", { wallet });
229
+ try {
230
+ setHasStartedConnecting(true);
231
+ setIsConnected(true);
232
+ setIsAuthenticating(true);
233
+ await setActiveWallet(wallet);
234
+ // Try to authenticate user (will use re-authenticate if session exists)
235
+ // If no session exists, authentication will need to happen via Turnkey flow
236
+ try {
237
+ const userAuth = await authenticateUser();
238
+ if (userAuth && onConnectCallback) {
239
+ await onConnectCallback(wallet, userAuth.accessToken);
240
+ }
241
+ }
242
+ catch (authError) {
243
+ // Authentication failed - this is expected if user hasn't authenticated via Turnkey yet
244
+ // The Turnkey auth modal should handle this
245
+ debug("@@useAuth:onConnect:authFailed", { authError });
246
+ // Don't set isAuthenticated to false here - let the Turnkey flow handle it
247
+ }
248
+ }
249
+ catch (error) {
250
+ debug("@@useAuth:onConnect:failed", { error });
251
+ setIsAuthenticated(false);
252
+ setUser(undefined);
253
+ }
254
+ finally {
255
+ setIsAuthenticating(false);
256
+ }
257
+ debug({
258
+ isAuthenticated,
259
+ isAuthenticating,
260
+ isConnected,
261
+ });
262
+ }, [
263
+ onConnectCallback,
264
+ authenticateUser,
265
+ isAuthenticated,
266
+ isAuthenticating,
267
+ isConnected,
268
+ setActiveWallet,
269
+ setHasStartedConnecting,
270
+ setIsAuthenticated,
271
+ setIsAuthenticating,
272
+ setIsConnected,
273
+ setUser,
274
+ ]);
275
+ const logout = (0, react_2.useCallback)(async (callback) => {
276
+ if (activeWallet) {
277
+ debug("@@logout:activeWallet", activeWallet);
278
+ disconnect(activeWallet);
279
+ debug("@@logout:activeWallet", activeWallet);
280
+ }
281
+ // Log out of each wallet
282
+ wallets.forEach(wallet => {
283
+ console.log("@@logging out", wallet);
284
+ disconnect(wallet);
285
+ });
286
+ // Delete localStorage thirdweb:connected-wallet-ids
287
+ // https://npc-labs.slack.com/archives/C070E6HNG85/p1750185115273099
288
+ if (typeof localStorage !== "undefined") {
289
+ localStorage.removeItem("thirdweb:connected-wallet-ids");
290
+ localStorage.removeItem("wagmi.store");
291
+ localStorage.removeItem("lastAuthProvider");
292
+ localStorage.removeItem("b3-user");
293
+ }
294
+ app_1.default.logout();
295
+ debug("@@logout:loggedOut");
296
+ setIsAuthenticated(false);
297
+ setIsConnected(false);
298
+ setUser();
299
+ callback?.();
300
+ }, [activeWallet, disconnect, wallets, setIsAuthenticated, setUser, setIsConnected]);
301
+ const { isLoading: useAutoConnectLoading } = (0, react_3.useAutoConnect)({
302
+ client: thirdweb_1.client,
303
+ wallets: [wallet],
304
+ onConnect,
305
+ onTimeout: () => {
306
+ logout().catch(error => {
307
+ debug("@@useAuth:logout on timeout failed", { error });
308
+ });
309
+ },
310
+ });
311
+ /**
312
+ * useAutoConnectLoading starts as false
313
+ */
314
+ (0, react_2.useEffect)(() => {
315
+ if (!useAutoConnectLoading && useAutoConnectLoadingPrevious.current && !hasStartedConnecting) {
316
+ setIsAuthenticating(false);
317
+ }
318
+ useAutoConnectLoadingPrevious.current = useAutoConnectLoading;
319
+ }, [useAutoConnectLoading, hasStartedConnecting, setIsAuthenticating]);
320
+ const isReady = isAuthenticated && !isAuthenticating;
321
+ return {
322
+ authenticate,
323
+ reAuthenticate,
324
+ logout,
325
+ isAuthenticated,
326
+ isReady,
327
+ isConnecting,
328
+ isConnected,
329
+ wallet,
330
+ preAuthenticate: in_app_1.preAuthenticate,
331
+ connect: onConnect,
332
+ isAuthenticating,
333
+ onConnect,
334
+ user,
335
+ refetchUser: authenticateUser,
336
+ setUser,
337
+ };
338
+ }
@@ -1,4 +1,7 @@
1
1
  import { Wallet } from "thirdweb/wallets";
2
+ /**
3
+ * @deprecated Use useAuth() with Turnkey authentication instead
4
+ */
2
5
  export declare function useTWAuth(): {
3
6
  authenticate: (wallet: Wallet, partnerId: string) => Promise<import("@feathersjs/authentication").AuthenticationResult>;
4
7
  };
@@ -4,11 +4,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.useTWAuth = useTWAuth;
7
+ /**
8
+ * @deprecated This hook is deprecated. Use useAuth() with Turnkey authentication instead.
9
+ * This file is kept for backward compatibility but should not be used in new code.
10
+ */
7
11
  const app_1 = __importDefault(require("../../../global-account/app"));
8
12
  const debug_1 = __importDefault(require("../../../shared/utils/debug"));
9
13
  const react_1 = require("react");
10
14
  const useSearchParamsSSR_1 = require("./useSearchParamsSSR");
15
+ /**
16
+ * @deprecated Use useAuth() with Turnkey authentication instead
17
+ */
11
18
  function useTWAuth() {
19
+ console.warn("useTWAuth is deprecated. Please migrate to useAuth() with Turnkey authentication. See useTurnkeyAuth.ts for the new implementation.");
12
20
  const referralCode = (0, useSearchParamsSSR_1.useSearchParam)("referralCode");
13
21
  const authenticate = (0, react_1.useCallback)(async (wallet, partnerId) => {
14
22
  if (!wallet || !wallet?.getAuthToken?.())
@@ -9,7 +9,7 @@ const react_1 = require("react");
9
9
  const app_1 = __importDefault(require("../../app"));
10
10
  const components_1 = require("../components");
11
11
  const stores_1 = require("../stores");
12
- const useAuthentication_1 = require("./useAuthentication");
12
+ const useAuth_1 = require("./useAuth");
13
13
  const debug = (0, debug_1.debugB3React)("useTurnkeyAuth");
14
14
  /**
15
15
  * Hook for Turnkey email-based OTP authentication
@@ -25,29 +25,54 @@ function useTurnkeyAuth() {
25
25
  const setIsAuthenticating = (0, stores_1.useAuthStore)(state => state.setIsAuthenticating);
26
26
  const setIsAuthenticated = (0, stores_1.useAuthStore)(state => state.setIsAuthenticated);
27
27
  const { partnerId } = (0, components_1.useB3Config)();
28
- const { user } = (0, useAuthentication_1.useAuthentication)(partnerId);
28
+ const { authenticate } = (0, useAuth_1.useAuth)();
29
29
  /**
30
30
  * Step 1: Initiate login with email
31
- * - Calls backend to create sub-org (if needed) and send OTP
31
+ * - Calls backend turnkey-jwt strategy (init action) to create sub-org (if needed) and send OTP
32
32
  * - Returns otpId to use in verification step
33
+ *
34
+ * Note: Uses the turnkey-jwt authentication strategy, not the service directly.
35
+ * The turnkey-jwt strategy handles both OTP flow (init/verify) and final authentication.
33
36
  */
34
37
  const initiateLogin = (0, react_1.useCallback)(async (email) => {
35
38
  setIsLoading(true);
36
39
  setError(null);
37
40
  setIsAuthenticating(true);
38
41
  try {
39
- if (!user?.userId) {
40
- throw new Error("User ID is required to initiate Turnkey login.");
41
- }
42
42
  debug(`Initiating login for: ${email}`);
43
- // Call FeathersJS service to initialize OTP
44
- const data = await app_1.default.service("turnkey-auth").init({ email, userId: user.userId });
43
+ // Use authentication service with turnkey-jwt strategy (init action)
44
+ // userId is resolved from authentication context on the backend (params.user.userId)
45
+ // Backend will get userId from _params.user?.userId if authenticated, or handle unauthenticated case
46
+ // So we only need to send email
47
+ debug(`Calling app.authenticate with turnkey-jwt strategy (init action)`, { email });
48
+ const response = await app_1.default.authenticate({
49
+ strategy: "turnkey-jwt",
50
+ action: "init",
51
+ email,
52
+ });
53
+ // The strategy returns the TurnkeyAuthInitResponse directly
54
+ const data = response;
45
55
  debug(`OTP initialized successfully. OtpId: ${data.otpId}`);
46
56
  return data;
47
57
  }
48
58
  catch (err) {
49
59
  debug("Error initiating login:", err);
50
- const errorMessage = err.message || "Failed to send OTP email. Please try again.";
60
+ // Provide more detailed error information
61
+ let errorMessage = "Failed to send OTP email. Please try again.";
62
+ if (err.message) {
63
+ errorMessage = err.message;
64
+ }
65
+ else if (err.name === "TypeError" && err.message?.includes("fetch")) {
66
+ errorMessage = "Network error: Unable to reach the server. Please check your connection and try again.";
67
+ }
68
+ else if (err.code === "ECONNREFUSED" || err.code === "ENOTFOUND") {
69
+ errorMessage = "Connection error: Unable to reach the server. Please check your internet connection.";
70
+ }
71
+ else if (err.response) {
72
+ // FeathersJS error response
73
+ errorMessage = err.response.message || err.message || errorMessage;
74
+ debug("FeathersJS error response:", err.response);
75
+ }
51
76
  setError(errorMessage);
52
77
  throw err;
53
78
  }
@@ -55,11 +80,11 @@ function useTurnkeyAuth() {
55
80
  setIsLoading(false);
56
81
  setIsAuthenticating(false);
57
82
  }
58
- }, [user, setIsAuthenticating]);
83
+ }, [setIsAuthenticating]);
59
84
  /**
60
85
  * Step 2: Verify OTP and authenticate
61
- * - Verifies OTP with backend
62
- * - Gets Turnkey session JWT
86
+ * - Verifies OTP with backend via turnkey-jwt strategy (verify action)
87
+ * - Gets Turnkey session JWT from the verify response
63
88
  * - Authenticates with b3-api using "turnkey-jwt" strategy
64
89
  * - JWT automatically stored in cookies by SDK
65
90
  */
@@ -68,19 +93,22 @@ function useTurnkeyAuth() {
68
93
  setError(null);
69
94
  setIsAuthenticating(true);
70
95
  try {
71
- debug(`Verifying OTP...`, { userId: user?.userId });
72
- // Step 1: Verify OTP and get Turnkey session JWT
73
- const { turnkeySessionJwt } = await app_1.default.service("turnkey-auth").verify({
96
+ debug(`Verifying OTP...`, { otpId });
97
+ // Step 1: Verify OTP with backend using turnkey-jwt strategy (verify action)
98
+ // This returns the Turnkey session JWT
99
+ const response = await app_1.default.authenticate({
100
+ strategy: "turnkey-jwt",
101
+ action: "verify",
74
102
  otpId,
75
103
  otpCode,
76
104
  });
77
- debug(`OTP verified! Authenticating with b3-api...`);
105
+ // The strategy returns the TurnkeyAuthVerifyResponse directly
106
+ const verifyResult = response;
107
+ const { turnkeySessionJwt } = verifyResult;
108
+ debug(`OTP verified! Got Turnkey session JWT. Authenticating with b3-api...`);
78
109
  // Step 2: Authenticate with b3-api using Turnkey JWT
79
- // The SDK will automatically store the b3-api JWT in cookies
80
- const authResult = await app_1.default.authenticate({
81
- strategy: "turnkey-jwt",
82
- accessToken: turnkeySessionJwt,
83
- });
110
+ // Use the unified useAuth hook for authentication with "turnkey-jwt" strategy
111
+ const authResult = await authenticate(turnkeySessionJwt, partnerId || "");
84
112
  debug(`Successfully authenticated with b3-api!`, authResult);
85
113
  // Update auth store to reflect authenticated state
86
114
  setIsAuthenticated(true);
@@ -100,7 +128,7 @@ function useTurnkeyAuth() {
100
128
  setIsLoading(false);
101
129
  setIsAuthenticating(false);
102
130
  }
103
- }, [user, setIsAuthenticating, setIsAuthenticated]);
131
+ }, [partnerId, setIsAuthenticating, setIsAuthenticated, authenticate]);
104
132
  const clearError = (0, react_1.useCallback)(() => {
105
133
  setError(null);
106
134
  }, []);
@@ -11,6 +11,8 @@ export function RelayKitProviderWrapper({ children, simDuneApiKey, }) {
11
11
  };
12
12
  fetchChains();
13
13
  }, []);
14
+ const isTurnkeyPrimary = process.env.NEXT_PUBLIC_TURNKEY_PRIMARY === "true";
15
+ const appName = isTurnkeyPrimary ? "Smart Wallet" : "AnySpend";
14
16
  return (_jsx(RelayKitProvider, { options: {
15
17
  baseApiUrl: MAINNET_RELAY_API,
16
18
  source: "anyspend",
@@ -20,7 +22,7 @@ export function RelayKitProviderWrapper({ children, simDuneApiKey, }) {
20
22
  },
21
23
  chains: relayChains,
22
24
  privateChainIds: undefined,
23
- appName: "AnySpend",
25
+ appName,
24
26
  useGasFeeEstimations: true,
25
27
  }, children: _jsx(_Fragment, { children: children }) }));
26
28
  }