@b3dotfun/sdk 0.0.82 → 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.
- package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +57 -12
- package/dist/cjs/global-account/react/hooks/index.d.ts +1 -0
- package/dist/cjs/global-account/react/hooks/index.js +3 -1
- package/dist/cjs/global-account/react/hooks/useAuth.d.ts +76 -0
- package/dist/cjs/global-account/react/hooks/useAuth.js +338 -0
- package/dist/cjs/global-account/react/hooks/useAuthentication.d.ts +1 -1
- package/dist/cjs/global-account/react/hooks/useAuthentication.js +36 -25
- package/dist/cjs/global-account/react/hooks/useTWAuth.d.ts +3 -0
- package/dist/cjs/global-account/react/hooks/useTWAuth.js +8 -0
- package/dist/cjs/global-account/react/hooks/useTurnkeyAuth.js +54 -24
- package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +57 -12
- package/dist/esm/global-account/react/hooks/index.d.ts +1 -0
- package/dist/esm/global-account/react/hooks/index.js +1 -0
- package/dist/esm/global-account/react/hooks/useAuth.d.ts +76 -0
- package/dist/esm/global-account/react/hooks/useAuth.js +332 -0
- package/dist/esm/global-account/react/hooks/useAuthentication.d.ts +1 -1
- package/dist/esm/global-account/react/hooks/useAuthentication.js +36 -25
- package/dist/esm/global-account/react/hooks/useTWAuth.d.ts +3 -0
- package/dist/esm/global-account/react/hooks/useTWAuth.js +8 -0
- package/dist/esm/global-account/react/hooks/useTurnkeyAuth.js +54 -24
- package/dist/types/global-account/react/hooks/index.d.ts +1 -0
- package/dist/types/global-account/react/hooks/useAuth.d.ts +76 -0
- package/dist/types/global-account/react/hooks/useAuthentication.d.ts +1 -1
- package/dist/types/global-account/react/hooks/useTWAuth.d.ts +3 -0
- package/package.json +1 -1
- package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +77 -22
- package/src/global-account/react/hooks/index.ts +1 -0
- package/src/global-account/react/hooks/useAuth.ts +380 -0
- package/src/global-account/react/hooks/useAuthentication.ts +51 -47
- package/src/global-account/react/hooks/useTWAuth.tsx +10 -0
- package/src/global-account/react/hooks/useTurnkeyAuth.ts +59 -26
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Wallet } from "thirdweb/wallets";
|
|
2
|
+
import { preAuthenticate } from "thirdweb/wallets/in-app";
|
|
3
|
+
/**
|
|
4
|
+
* Unified authentication hook that uses Turnkey for authentication
|
|
5
|
+
* This replaces the previous Thirdweb-based authentication
|
|
6
|
+
*
|
|
7
|
+
* This hook provides 1:1 feature parity with useAuthentication.ts
|
|
8
|
+
*/
|
|
9
|
+
export declare function useAuth(): {
|
|
10
|
+
authenticate: (turnkeySessionJwt: string, partnerId: string) => Promise<import("@feathersjs/authentication").AuthenticationResult>;
|
|
11
|
+
reAuthenticate: () => Promise<import("@feathersjs/authentication").AuthenticationResult>;
|
|
12
|
+
logout: (callback?: () => void) => Promise<void>;
|
|
13
|
+
isAuthenticated: boolean;
|
|
14
|
+
isReady: boolean;
|
|
15
|
+
isConnecting: boolean;
|
|
16
|
+
isConnected: boolean;
|
|
17
|
+
wallet: import("thirdweb/dist/types/wallets/in-app/core/wallet/types").EcosystemWallet;
|
|
18
|
+
preAuthenticate: typeof preAuthenticate;
|
|
19
|
+
connect: (_walleAutoConnectedWith: Wallet, allConnectedWallets: Wallet[]) => Promise<void>;
|
|
20
|
+
isAuthenticating: boolean;
|
|
21
|
+
onConnect: (_walleAutoConnectedWith: Wallet, allConnectedWallets: Wallet[]) => Promise<void>;
|
|
22
|
+
user: {
|
|
23
|
+
email?: string | undefined;
|
|
24
|
+
telNumber?: string | undefined;
|
|
25
|
+
username?: string | undefined;
|
|
26
|
+
ens?: string | undefined;
|
|
27
|
+
avatar?: string | undefined;
|
|
28
|
+
preferences?: {} | undefined;
|
|
29
|
+
referredBy?: string | {} | undefined;
|
|
30
|
+
sourceApp?: string | undefined;
|
|
31
|
+
referralCode?: string | undefined;
|
|
32
|
+
userGroups?: number[] | undefined;
|
|
33
|
+
isMigratedFromBSMNT?: boolean | undefined;
|
|
34
|
+
privyLinkedAccounts?: {
|
|
35
|
+
name?: string | undefined;
|
|
36
|
+
address?: string | undefined;
|
|
37
|
+
email?: string | undefined;
|
|
38
|
+
chain_type?: string | undefined;
|
|
39
|
+
lv?: number | undefined;
|
|
40
|
+
wallet_client_type?: string | undefined;
|
|
41
|
+
smart_wallet_type?: string | undefined;
|
|
42
|
+
subject?: string | undefined;
|
|
43
|
+
type: string;
|
|
44
|
+
}[] | undefined;
|
|
45
|
+
twProfiles?: {
|
|
46
|
+
type: string;
|
|
47
|
+
details: {
|
|
48
|
+
id?: string | undefined;
|
|
49
|
+
name?: string | undefined;
|
|
50
|
+
address?: string | undefined;
|
|
51
|
+
email?: string | undefined;
|
|
52
|
+
phone?: string | undefined;
|
|
53
|
+
username?: string | undefined;
|
|
54
|
+
fid?: string | undefined;
|
|
55
|
+
};
|
|
56
|
+
}[] | undefined;
|
|
57
|
+
turnkeySubOrgs?: {
|
|
58
|
+
hasDelegatedUser?: boolean | undefined;
|
|
59
|
+
subOrgId: string;
|
|
60
|
+
accounts: Record<string, any>[];
|
|
61
|
+
}[] | undefined;
|
|
62
|
+
_id: string | {};
|
|
63
|
+
userId: string;
|
|
64
|
+
smartAccountAddress: string;
|
|
65
|
+
createdAt: number;
|
|
66
|
+
updatedAt: number;
|
|
67
|
+
partnerIds: {
|
|
68
|
+
privyId?: string | undefined;
|
|
69
|
+
thirdwebId?: string | undefined;
|
|
70
|
+
turnkeyId?: string | undefined;
|
|
71
|
+
turnkeyOtpId?: string | undefined;
|
|
72
|
+
};
|
|
73
|
+
} | undefined;
|
|
74
|
+
refetchUser: () => Promise<import("@feathersjs/authentication").AuthenticationResult>;
|
|
75
|
+
setUser: (newUser?: import("@b3dotfun/b3-api").Users) => void;
|
|
76
|
+
};
|
|
@@ -63,6 +63,6 @@ export declare function useAuthentication(partnerId: string): {
|
|
|
63
63
|
turnkeyOtpId?: string | undefined;
|
|
64
64
|
};
|
|
65
65
|
} | undefined;
|
|
66
|
-
refetchUser: (
|
|
66
|
+
refetchUser: () => Promise<import("@feathersjs/authentication").AuthenticationResult>;
|
|
67
67
|
setUser: (newUser?: import("@b3dotfun/b3-api").Users) => void;
|
|
68
68
|
};
|
|
@@ -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
|
};
|
package/package.json
CHANGED
|
@@ -10,6 +10,7 @@ import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
|
|
|
10
10
|
import { useCallback, useEffect, useState } from "react";
|
|
11
11
|
import { useActiveAccount } from "thirdweb/react";
|
|
12
12
|
import { Account } from "thirdweb/wallets";
|
|
13
|
+
import { TurnkeyAuthModal } from "../TurnkeyAuthModal";
|
|
13
14
|
import { SignInWithB3Privy } from "./SignInWithB3Privy";
|
|
14
15
|
import { LoginStep, LoginStepContainer } from "./steps/LoginStep";
|
|
15
16
|
import { LoginStepCustom } from "./steps/LoginStepCustom";
|
|
@@ -40,7 +41,9 @@ export function SignInWithB3Flow({
|
|
|
40
41
|
const account = useActiveAccount();
|
|
41
42
|
const isAuthenticating = useAuthStore(state => state.isAuthenticating);
|
|
42
43
|
const isAuthenticated = useAuthStore(state => state.isAuthenticated);
|
|
44
|
+
const setIsAuthenticated = useAuthStore(state => state.setIsAuthenticated);
|
|
43
45
|
const isConnected = useAuthStore(state => state.isConnected);
|
|
46
|
+
const setIsConnected = useAuthStore(state => state.setIsConnected);
|
|
44
47
|
const setJustCompletedLogin = useAuthStore(state => state.setJustCompletedLogin);
|
|
45
48
|
const [refetchCount, setRefetchCount] = useState(0);
|
|
46
49
|
const [refetchError, setRefetchError] = useState<string | null>(null);
|
|
@@ -154,6 +157,11 @@ export function SignInWithB3Flow({
|
|
|
154
157
|
await refetchUser();
|
|
155
158
|
debug("User refetched successfully");
|
|
156
159
|
|
|
160
|
+
// Set authentication and connection state so UI updates properly
|
|
161
|
+
setIsAuthenticated(true);
|
|
162
|
+
setIsConnected(true);
|
|
163
|
+
setJustCompletedLogin(true);
|
|
164
|
+
|
|
157
165
|
// After user data is refreshed, close Turnkey modal and go back to sign-in flow
|
|
158
166
|
debug("Switching back to signInWithB3 modal");
|
|
159
167
|
setB3ModalContentType({
|
|
@@ -184,6 +192,9 @@ export function SignInWithB3Flow({
|
|
|
184
192
|
closeAfterLogin,
|
|
185
193
|
source,
|
|
186
194
|
signersEnabled,
|
|
195
|
+
setIsAuthenticated,
|
|
196
|
+
setIsConnected,
|
|
197
|
+
setJustCompletedLogin,
|
|
187
198
|
],
|
|
188
199
|
);
|
|
189
200
|
|
|
@@ -340,32 +351,76 @@ export function SignInWithB3Flow({
|
|
|
340
351
|
<div className="p-4 text-center text-red-500">{refetchError}</div>
|
|
341
352
|
</LoginStepContainer>
|
|
342
353
|
);
|
|
343
|
-
} else if (isAuthenticating || (isFetchingSigners && step === "login") || source === "requestPermissions") {
|
|
344
|
-
content = (
|
|
345
|
-
<LoginStepContainer partnerId={partnerId}>
|
|
346
|
-
<div className="my-8 flex min-h-[350px] items-center justify-center">
|
|
347
|
-
<Loading variant="white" size="lg" />
|
|
348
|
-
</div>
|
|
349
|
-
</LoginStepContainer>
|
|
350
|
-
);
|
|
351
354
|
} else if (step === "login") {
|
|
352
|
-
//
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
355
|
+
// PRIORITY: If Turnkey is enabled, show Turnkey modal FIRST as the primary authentication option
|
|
356
|
+
// Show Turnkey when enabled and not already completed in this session
|
|
357
|
+
const shouldShowTurnkeyFirst = enableTurnkey && !turnkeyAuthCompleted;
|
|
358
|
+
|
|
359
|
+
if (shouldShowTurnkeyFirst) {
|
|
360
|
+
// Don't show loading spinner for Turnkey - let the modal handle its own loading state
|
|
361
|
+
// This prevents the infinite loop where isAuthenticating causes the modal to be replaced
|
|
362
|
+
debug("Showing Turnkey as primary authentication option", {
|
|
363
|
+
enableTurnkey,
|
|
364
|
+
turnkeyAuthCompleted,
|
|
365
|
+
isAuthenticated,
|
|
366
|
+
});
|
|
367
|
+
// Show Turnkey authentication as primary option
|
|
357
368
|
content = (
|
|
358
|
-
<
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
369
|
+
<LoginStepContainer partnerId={partnerId}>
|
|
370
|
+
<TurnkeyAuthModal
|
|
371
|
+
onSuccess={async (authenticatedUser: any) => {
|
|
372
|
+
debug("Turnkey authentication successful in primary flow", { authenticatedUser });
|
|
373
|
+
setTurnkeyAuthCompleted(true);
|
|
374
|
+
// After Turnkey auth, refetch user to get the full user object
|
|
375
|
+
await refetchUser();
|
|
376
|
+
// User is now authenticated via Turnkey
|
|
377
|
+
// Set both isAuthenticated and isConnected to true so UI updates properly
|
|
378
|
+
// Wallet connection is optional and can happen later for signing transactions
|
|
379
|
+
setIsAuthenticated(true);
|
|
380
|
+
setIsConnected(true);
|
|
381
|
+
setJustCompletedLogin(true);
|
|
382
|
+
// Call the login success callback
|
|
383
|
+
onLoginSuccess?.({} as Account);
|
|
384
|
+
}}
|
|
385
|
+
onClose={() => {
|
|
386
|
+
// If user closes Turnkey modal, they can still use wallet connection as fallback
|
|
387
|
+
setTurnkeyAuthCompleted(true);
|
|
388
|
+
}}
|
|
389
|
+
initialEmail=""
|
|
390
|
+
skipToOtp={false}
|
|
391
|
+
/>
|
|
392
|
+
</LoginStepContainer>
|
|
365
393
|
);
|
|
366
394
|
} else {
|
|
367
|
-
//
|
|
368
|
-
|
|
395
|
+
// Show loading spinner only if not in Turnkey flow
|
|
396
|
+
if (isAuthenticating || (isFetchingSigners && step === "login") || source === "requestPermissions") {
|
|
397
|
+
content = (
|
|
398
|
+
<LoginStepContainer partnerId={partnerId}>
|
|
399
|
+
<div className="my-8 flex min-h-[350px] items-center justify-center">
|
|
400
|
+
<Loading variant="white" size="lg" />
|
|
401
|
+
</div>
|
|
402
|
+
</LoginStepContainer>
|
|
403
|
+
);
|
|
404
|
+
} else {
|
|
405
|
+
// Custom strategy
|
|
406
|
+
if (strategies?.[0] === "privy") {
|
|
407
|
+
content = <SignInWithB3Privy onSuccess={handleLoginSuccess} chain={chain} />;
|
|
408
|
+
} else if (strategies) {
|
|
409
|
+
// Strategies are explicitly provided
|
|
410
|
+
content = (
|
|
411
|
+
<LoginStepCustom
|
|
412
|
+
strategies={strategies}
|
|
413
|
+
chain={chain}
|
|
414
|
+
onSuccess={handleLoginSuccess}
|
|
415
|
+
onError={onError}
|
|
416
|
+
automaticallySetFirstEoa={!!automaticallySetFirstEoa}
|
|
417
|
+
/>
|
|
418
|
+
);
|
|
419
|
+
} else {
|
|
420
|
+
// Default to handle all strategies we support
|
|
421
|
+
content = <LoginStep chain={chain} onSuccess={handleLoginSuccess} onError={onError} />;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
369
424
|
}
|
|
370
425
|
}
|
|
371
426
|
|
|
@@ -3,6 +3,7 @@ export { useAccountAssets } from "./useAccountAssets";
|
|
|
3
3
|
export { useAccountWallet } from "./useAccountWallet";
|
|
4
4
|
export { useAddTWSessionKey } from "./useAddTWSessionKey";
|
|
5
5
|
export { useAnalytics } from "./useAnalytics";
|
|
6
|
+
export { useAuth } from "./useAuth";
|
|
6
7
|
export { useAuthentication } from "./useAuthentication";
|
|
7
8
|
export { useB3BalanceFromAddresses } from "./useB3BalanceFromAddresses";
|
|
8
9
|
export { useB3EnsName } from "./useB3EnsName";
|
|
@@ -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
|
+
}
|