@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.
- package/dist/cjs/anyspend/react/components/AnySpend.js +3 -0
- package/dist/cjs/anyspend/react/components/AnySpendCustom.js +3 -0
- package/dist/cjs/anyspend/react/components/common/CryptoPaymentMethod.js +177 -47
- package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.js +3 -0
- package/dist/cjs/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +10 -0
- package/dist/cjs/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.js +73 -0
- 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 +2 -2
- package/dist/cjs/global-account/react/hooks/useAuthentication.js +72 -63
- 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/cjs/global-account/react/hooks/useUserQuery.d.ts +1 -1
- package/dist/esm/anyspend/react/components/AnySpend.js +3 -0
- package/dist/esm/anyspend/react/components/AnySpendCustom.js +3 -0
- package/dist/esm/anyspend/react/components/common/CryptoPaymentMethod.js +175 -45
- package/dist/esm/anyspend/react/hooks/useAnyspendFlow.js +3 -0
- package/dist/esm/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +10 -0
- package/dist/esm/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.js +70 -0
- 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 +2 -2
- package/dist/esm/global-account/react/hooks/useAuthentication.js +72 -63
- 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/esm/global-account/react/hooks/useUserQuery.d.ts +1 -1
- package/dist/styles/index.css +1 -1
- package/dist/types/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +10 -0
- 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 +2 -2
- package/dist/types/global-account/react/hooks/useTWAuth.d.ts +3 -0
- package/dist/types/global-account/react/hooks/useUserQuery.d.ts +1 -1
- package/package.json +1 -1
- package/src/anyspend/react/components/AnySpend.tsx +4 -0
- package/src/anyspend/react/components/AnySpendCustom.tsx +4 -0
- package/src/anyspend/react/components/common/CryptoPaymentMethod.tsx +253 -22
- package/src/anyspend/react/hooks/useAnyspendFlow.ts +4 -0
- package/src/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.ts +80 -0
- 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 +88 -85
- package/src/global-account/react/hooks/useTWAuth.tsx +10 -0
- package/src/global-account/react/hooks/useTurnkeyAuth.ts +59 -26
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import app from "../../app";
|
|
2
|
-
import { useAuthStore } from "../stores";
|
|
3
|
-
import { useCallback, useState } from "react";
|
|
4
|
-
import { useB3 } from "../components/B3Provider/useB3";
|
|
5
1
|
import { TurnkeyAuthInitResponse } from "@b3dotfun/b3-api";
|
|
6
2
|
import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
|
|
3
|
+
import { useCallback, useState } from "react";
|
|
4
|
+
import app from "../../app";
|
|
5
|
+
import { useB3 } from "../components/B3Provider/useB3";
|
|
6
|
+
import { useAuthStore } from "../stores";
|
|
7
|
+
import { useAuth } from "./useAuth";
|
|
7
8
|
|
|
8
9
|
const debug = debugB3React("useTurnkeyAuth");
|
|
9
10
|
|
|
@@ -32,12 +33,16 @@ export function useTurnkeyAuth(): UseTurnkeyAuthReturn {
|
|
|
32
33
|
const [error, setError] = useState<string | null>(null);
|
|
33
34
|
const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
|
|
34
35
|
const setIsAuthenticated = useAuthStore(state => state.setIsAuthenticated);
|
|
35
|
-
const {
|
|
36
|
+
const { partnerId } = useB3();
|
|
37
|
+
const { authenticate } = useAuth();
|
|
36
38
|
|
|
37
39
|
/**
|
|
38
40
|
* Step 1: Initiate login with email
|
|
39
|
-
* - 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
|
|
40
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.
|
|
41
46
|
*/
|
|
42
47
|
const initiateLogin = useCallback(
|
|
43
48
|
async (email: string): Promise<TurnkeyAuthInitResponse> => {
|
|
@@ -46,20 +51,44 @@ export function useTurnkeyAuth(): UseTurnkeyAuthReturn {
|
|
|
46
51
|
setIsAuthenticating(true);
|
|
47
52
|
|
|
48
53
|
try {
|
|
49
|
-
if (!user?.userId) {
|
|
50
|
-
throw new Error("User ID is required to initiate Turnkey login.");
|
|
51
|
-
}
|
|
52
54
|
debug(`Initiating login for: ${email}`);
|
|
53
55
|
|
|
54
|
-
//
|
|
55
|
-
|
|
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;
|
|
56
70
|
|
|
57
71
|
debug(`OTP initialized successfully. OtpId: ${data.otpId}`);
|
|
58
72
|
|
|
59
73
|
return data;
|
|
60
74
|
} catch (err: any) {
|
|
61
75
|
debug("Error initiating login:", err);
|
|
62
|
-
|
|
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
|
+
|
|
63
92
|
setError(errorMessage);
|
|
64
93
|
throw err;
|
|
65
94
|
} finally {
|
|
@@ -67,13 +96,13 @@ export function useTurnkeyAuth(): UseTurnkeyAuthReturn {
|
|
|
67
96
|
setIsAuthenticating(false);
|
|
68
97
|
}
|
|
69
98
|
},
|
|
70
|
-
[
|
|
99
|
+
[setIsAuthenticating],
|
|
71
100
|
);
|
|
72
101
|
|
|
73
102
|
/**
|
|
74
103
|
* Step 2: Verify OTP and authenticate
|
|
75
|
-
* - Verifies OTP with backend
|
|
76
|
-
* - Gets Turnkey session JWT
|
|
104
|
+
* - Verifies OTP with backend via turnkey-jwt strategy (verify action)
|
|
105
|
+
* - Gets Turnkey session JWT from the verify response
|
|
77
106
|
* - Authenticates with b3-api using "turnkey-jwt" strategy
|
|
78
107
|
* - JWT automatically stored in cookies by SDK
|
|
79
108
|
*/
|
|
@@ -84,22 +113,26 @@ export function useTurnkeyAuth(): UseTurnkeyAuthReturn {
|
|
|
84
113
|
setIsAuthenticating(true);
|
|
85
114
|
|
|
86
115
|
try {
|
|
87
|
-
debug(`Verifying OTP...`, {
|
|
116
|
+
debug(`Verifying OTP...`, { otpId });
|
|
88
117
|
|
|
89
|
-
// Step 1: Verify OTP
|
|
90
|
-
|
|
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",
|
|
91
123
|
otpId,
|
|
92
124
|
otpCode,
|
|
93
|
-
});
|
|
125
|
+
} as any);
|
|
126
|
+
|
|
127
|
+
// The strategy returns the TurnkeyAuthVerifyResponse directly
|
|
128
|
+
const verifyResult = response as unknown as TurnkeyVerifyResponse;
|
|
129
|
+
const { turnkeySessionJwt } = verifyResult;
|
|
94
130
|
|
|
95
|
-
debug(`OTP verified! Authenticating with b3-api...`);
|
|
131
|
+
debug(`OTP verified! Got Turnkey session JWT. Authenticating with b3-api...`);
|
|
96
132
|
|
|
97
133
|
// Step 2: Authenticate with b3-api using Turnkey JWT
|
|
98
|
-
//
|
|
99
|
-
const authResult = await
|
|
100
|
-
strategy: "turnkey-jwt",
|
|
101
|
-
accessToken: turnkeySessionJwt,
|
|
102
|
-
} as any);
|
|
134
|
+
// Use the unified useAuth hook for authentication with "turnkey-jwt" strategy
|
|
135
|
+
const authResult = await authenticate(turnkeySessionJwt, partnerId || "");
|
|
103
136
|
|
|
104
137
|
debug(`Successfully authenticated with b3-api!`, authResult);
|
|
105
138
|
|
|
@@ -121,7 +154,7 @@ export function useTurnkeyAuth(): UseTurnkeyAuthReturn {
|
|
|
121
154
|
setIsAuthenticating(false);
|
|
122
155
|
}
|
|
123
156
|
},
|
|
124
|
-
[
|
|
157
|
+
[partnerId, setIsAuthenticating, setIsAuthenticated, authenticate],
|
|
125
158
|
);
|
|
126
159
|
|
|
127
160
|
const clearError = useCallback(() => {
|