@b3dotfun/sdk 0.1.69-alpha.17 → 0.1.69-alpha.18
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/utils/chain.js +1 -1
- package/dist/cjs/global-account/better-auth-client.d.ts +1883 -0
- package/dist/cjs/global-account/better-auth-client.js +17 -0
- package/dist/cjs/global-account/react/components/B3Provider/B3ConfigProvider.d.ts +4 -1
- package/dist/cjs/global-account/react/components/B3Provider/B3ConfigProvider.js +2 -1
- package/dist/cjs/global-account/react/components/B3Provider/B3Provider.d.ts +4 -1
- package/dist/cjs/global-account/react/components/B3Provider/B3Provider.js +3 -2
- package/dist/cjs/global-account/react/components/B3Provider/BetterAuthProvider.d.ts +16 -0
- package/dist/cjs/global-account/react/components/B3Provider/BetterAuthProvider.js +120 -0
- package/dist/cjs/global-account/react/components/SignInWithB3/BetterAuthResetPassword.d.ts +21 -0
- package/dist/cjs/global-account/react/components/SignInWithB3/BetterAuthResetPassword.js +67 -0
- package/dist/cjs/global-account/react/components/SignInWithB3/BetterAuthSignIn.d.ts +34 -0
- package/dist/cjs/global-account/react/components/SignInWithB3/BetterAuthSignIn.js +149 -0
- package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +9 -3
- package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStepBetterAuth.d.ts +6 -0
- package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStepBetterAuth.js +123 -0
- package/dist/cjs/global-account/react/components/SignInWithB3/utils/signInUtils.js +5 -1
- package/dist/cjs/global-account/react/components/index.d.ts +3 -0
- package/dist/cjs/global-account/react/components/index.js +7 -3
- package/dist/cjs/global-account/react/hooks/index.d.ts +1 -0
- package/dist/cjs/global-account/react/hooks/index.js +4 -2
- package/dist/cjs/global-account/react/hooks/useBetterAuth.d.ts +969 -0
- package/dist/cjs/global-account/react/hooks/useBetterAuth.js +142 -0
- package/dist/esm/anyspend/utils/chain.js +1 -1
- package/dist/esm/global-account/better-auth-client.d.ts +1883 -0
- package/dist/esm/global-account/better-auth-client.js +13 -0
- package/dist/esm/global-account/react/components/B3Provider/B3ConfigProvider.d.ts +4 -1
- package/dist/esm/global-account/react/components/B3Provider/B3ConfigProvider.js +2 -1
- package/dist/esm/global-account/react/components/B3Provider/B3Provider.d.ts +4 -1
- package/dist/esm/global-account/react/components/B3Provider/B3Provider.js +3 -2
- package/dist/esm/global-account/react/components/B3Provider/BetterAuthProvider.d.ts +16 -0
- package/dist/esm/global-account/react/components/B3Provider/BetterAuthProvider.js +115 -0
- package/dist/esm/global-account/react/components/SignInWithB3/BetterAuthResetPassword.d.ts +21 -0
- package/dist/esm/global-account/react/components/SignInWithB3/BetterAuthResetPassword.js +64 -0
- package/dist/esm/global-account/react/components/SignInWithB3/BetterAuthSignIn.d.ts +34 -0
- package/dist/esm/global-account/react/components/SignInWithB3/BetterAuthSignIn.js +146 -0
- package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +9 -3
- package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStepBetterAuth.d.ts +6 -0
- package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStepBetterAuth.js +120 -0
- package/dist/esm/global-account/react/components/SignInWithB3/utils/signInUtils.js +5 -1
- package/dist/esm/global-account/react/components/index.d.ts +3 -0
- package/dist/esm/global-account/react/components/index.js +2 -0
- 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/useBetterAuth.d.ts +969 -0
- package/dist/esm/global-account/react/hooks/useBetterAuth.js +136 -0
- package/dist/styles/index.css +1 -1
- package/dist/types/global-account/better-auth-client.d.ts +1883 -0
- package/dist/types/global-account/react/components/B3Provider/B3ConfigProvider.d.ts +4 -1
- package/dist/types/global-account/react/components/B3Provider/B3Provider.d.ts +4 -1
- package/dist/types/global-account/react/components/B3Provider/BetterAuthProvider.d.ts +16 -0
- package/dist/types/global-account/react/components/SignInWithB3/BetterAuthResetPassword.d.ts +21 -0
- package/dist/types/global-account/react/components/SignInWithB3/BetterAuthSignIn.d.ts +34 -0
- package/dist/types/global-account/react/components/SignInWithB3/steps/LoginStepBetterAuth.d.ts +6 -0
- package/dist/types/global-account/react/components/index.d.ts +3 -0
- package/dist/types/global-account/react/hooks/index.d.ts +1 -0
- package/dist/types/global-account/react/hooks/useBetterAuth.d.ts +969 -0
- package/package.json +2 -1
- package/src/anyspend/utils/chain.ts +1 -2
- package/src/global-account/better-auth-client.ts +17 -0
- package/src/global-account/react/components/B3Provider/B3ConfigProvider.tsx +6 -0
- package/src/global-account/react/components/B3Provider/B3Provider.tsx +15 -5
- package/src/global-account/react/components/B3Provider/BetterAuthProvider.tsx +127 -0
- package/src/global-account/react/components/SignInWithB3/BetterAuthResetPassword.tsx +146 -0
- package/src/global-account/react/components/SignInWithB3/BetterAuthSignIn.tsx +375 -0
- package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +9 -3
- package/src/global-account/react/components/SignInWithB3/steps/LoginStepBetterAuth.tsx +263 -0
- package/src/global-account/react/components/SignInWithB3/utils/signInUtils.ts +5 -1
- package/src/global-account/react/components/index.ts +3 -0
- package/src/global-account/react/hooks/index.ts +1 -0
- package/src/global-account/react/hooks/useBetterAuth.ts +177 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { Button, Input, useAuthStore } from "@b3dotfun/sdk/global-account/react";
|
|
2
|
+
import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { type BetterAuthSocialProvider, useBetterAuth } from "../../hooks/useBetterAuth";
|
|
5
|
+
import { strategyIcons, strategyLabels } from "./utils/signInUtils";
|
|
6
|
+
|
|
7
|
+
const debug = debugB3React("BetterAuthSignIn");
|
|
8
|
+
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
9
|
+
|
|
10
|
+
type SocialProviderConfig = { id: BetterAuthSocialProvider; label: string };
|
|
11
|
+
|
|
12
|
+
const DEFAULT_SOCIAL_PROVIDERS: SocialProviderConfig[] = [
|
|
13
|
+
{ id: "google", label: "Google" },
|
|
14
|
+
{ id: "github", label: "GitHub" },
|
|
15
|
+
{ id: "discord", label: "Discord" },
|
|
16
|
+
{ id: "microsoft", label: "Microsoft" },
|
|
17
|
+
{ id: "slack", label: "Slack" },
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
export interface BetterAuthSignInProps {
|
|
21
|
+
/** Title displayed above the form. Default: "Welcome back" for sign-in, "Create an account" for sign-up */
|
|
22
|
+
title?: string;
|
|
23
|
+
/** Subtitle displayed below the title */
|
|
24
|
+
subtitle?: string;
|
|
25
|
+
/** Which social providers to show. Defaults to all. Pass empty array to hide. */
|
|
26
|
+
socialProviders?: BetterAuthSocialProvider[];
|
|
27
|
+
/** Show email/password form. Default: true */
|
|
28
|
+
showEmail?: boolean;
|
|
29
|
+
/** URL to redirect to after password reset link is clicked. Token is appended as ?token=... */
|
|
30
|
+
passwordResetRedirectTo?: string;
|
|
31
|
+
/** Called after successful authentication */
|
|
32
|
+
onSuccess?: () => void;
|
|
33
|
+
/** Called on authentication error */
|
|
34
|
+
onError?: (error: Error) => void;
|
|
35
|
+
/** Optional class name for the root container */
|
|
36
|
+
className?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Standalone inline sign-in component for Better Auth.
|
|
41
|
+
*
|
|
42
|
+
* Renders directly — no modal, no button wrapper. Designed for full-page
|
|
43
|
+
* sign-in layouts (e.g., B3OS web with branding on the left, form on the right).
|
|
44
|
+
*
|
|
45
|
+
* Usage:
|
|
46
|
+
* ```tsx
|
|
47
|
+
* <BetterAuthSignIn
|
|
48
|
+
* socialProviders={["google", "github"]}
|
|
49
|
+
* onSuccess={() => router.push("/dashboard")}
|
|
50
|
+
* />
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function BetterAuthSignIn({
|
|
54
|
+
title,
|
|
55
|
+
subtitle = "Enter your credentials to access your account",
|
|
56
|
+
socialProviders = DEFAULT_SOCIAL_PROVIDERS.map(p => p.id),
|
|
57
|
+
showEmail = true,
|
|
58
|
+
passwordResetRedirectTo,
|
|
59
|
+
onSuccess,
|
|
60
|
+
onError,
|
|
61
|
+
className,
|
|
62
|
+
}: BetterAuthSignInProps) {
|
|
63
|
+
const { signInWithEmail, signUpWithEmail, signInWithSocial, requestPasswordReset } = useBetterAuth();
|
|
64
|
+
const isAuthenticated = useAuthStore(state => state.isAuthenticated);
|
|
65
|
+
|
|
66
|
+
const [mode, setMode] = useState<"sign-in" | "sign-up" | "forgot-password">("sign-in");
|
|
67
|
+
const [email, setEmail] = useState("");
|
|
68
|
+
const [password, setPassword] = useState("");
|
|
69
|
+
const [name, setName] = useState("");
|
|
70
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
71
|
+
const [loadingProvider, setLoadingProvider] = useState<string | null>(null);
|
|
72
|
+
const [error, setError] = useState<string | null>(null);
|
|
73
|
+
const [resetEmailSent, setResetEmailSent] = useState(false);
|
|
74
|
+
|
|
75
|
+
const resolvedTitle =
|
|
76
|
+
title ||
|
|
77
|
+
(mode === "forgot-password" ? "Reset password" : mode === "sign-in" ? "Welcome back" : "Create an account");
|
|
78
|
+
|
|
79
|
+
const resolvedSubtitle =
|
|
80
|
+
mode === "forgot-password"
|
|
81
|
+
? "Enter your email and we'll send you a reset link"
|
|
82
|
+
: subtitle || "Enter your credentials to access your account";
|
|
83
|
+
|
|
84
|
+
const handleForgotPassword = async () => {
|
|
85
|
+
const normalizedEmail = email.trim().toLowerCase();
|
|
86
|
+
|
|
87
|
+
if (!EMAIL_REGEX.test(normalizedEmail)) {
|
|
88
|
+
setError("Please enter a valid email address");
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
setIsLoading(true);
|
|
94
|
+
setError(null);
|
|
95
|
+
await requestPasswordReset(normalizedEmail, passwordResetRedirectTo);
|
|
96
|
+
setResetEmailSent(true);
|
|
97
|
+
} catch (err) {
|
|
98
|
+
const message = err instanceof Error ? err.message : "Failed to send reset email";
|
|
99
|
+
setError(message);
|
|
100
|
+
} finally {
|
|
101
|
+
setIsLoading(false);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const providers: SocialProviderConfig[] = socialProviders
|
|
106
|
+
.map(id => DEFAULT_SOCIAL_PROVIDERS.find(p => p.id === id))
|
|
107
|
+
.filter((p): p is SocialProviderConfig => !!p);
|
|
108
|
+
|
|
109
|
+
if (isAuthenticated) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const handleEmailSubmit = async () => {
|
|
114
|
+
const normalizedEmail = email.trim().toLowerCase();
|
|
115
|
+
|
|
116
|
+
if (!EMAIL_REGEX.test(normalizedEmail)) {
|
|
117
|
+
setError("Please enter a valid email address");
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!password) {
|
|
122
|
+
setError("Please enter a password");
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (mode === "sign-up" && !name.trim()) {
|
|
127
|
+
setError("Please enter your name");
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
setIsLoading(true);
|
|
133
|
+
setError(null);
|
|
134
|
+
|
|
135
|
+
if (mode === "sign-up") {
|
|
136
|
+
await signUpWithEmail(normalizedEmail, password, name.trim());
|
|
137
|
+
} else {
|
|
138
|
+
await signInWithEmail(normalizedEmail, password);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
debug("Auth successful");
|
|
142
|
+
onSuccess?.();
|
|
143
|
+
} catch (err) {
|
|
144
|
+
const message = err instanceof Error ? err.message : "Authentication failed";
|
|
145
|
+
setError(message);
|
|
146
|
+
debug("Auth failed", err);
|
|
147
|
+
onError?.(err as Error);
|
|
148
|
+
} finally {
|
|
149
|
+
setPassword("");
|
|
150
|
+
setIsLoading(false);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const handleSocialSignIn = async (provider: BetterAuthSocialProvider) => {
|
|
155
|
+
try {
|
|
156
|
+
setIsLoading(true);
|
|
157
|
+
setLoadingProvider(provider);
|
|
158
|
+
setError(null);
|
|
159
|
+
await signInWithSocial(provider);
|
|
160
|
+
} catch (err) {
|
|
161
|
+
const message = err instanceof Error ? err.message : `Sign in failed`;
|
|
162
|
+
setError(message);
|
|
163
|
+
onError?.(err as Error);
|
|
164
|
+
setIsLoading(false);
|
|
165
|
+
setLoadingProvider(null);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
return (
|
|
170
|
+
<div className={`w-full max-w-[400px] px-6 ${className || ""}`}>
|
|
171
|
+
{/* Header */}
|
|
172
|
+
<div className="mb-10 text-center">
|
|
173
|
+
<h1 className="text-[28px] font-semibold tracking-tight text-gray-900 dark:text-gray-100">{resolvedTitle}</h1>
|
|
174
|
+
<p className="mt-3 text-[15px] text-gray-500 dark:text-gray-400">{resolvedSubtitle}</p>
|
|
175
|
+
</div>
|
|
176
|
+
|
|
177
|
+
{/* Forgot password view */}
|
|
178
|
+
{mode === "forgot-password" && (
|
|
179
|
+
<div className="space-y-5">
|
|
180
|
+
{resetEmailSent ? (
|
|
181
|
+
<div className="space-y-4 text-center">
|
|
182
|
+
<div className="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-green-100">
|
|
183
|
+
<svg className="h-6 w-6 text-green-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
184
|
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
|
|
185
|
+
</svg>
|
|
186
|
+
</div>
|
|
187
|
+
<p className="text-[15px] text-gray-600 dark:text-gray-300">Check your email for a reset link.</p>
|
|
188
|
+
</div>
|
|
189
|
+
) : (
|
|
190
|
+
<>
|
|
191
|
+
<div>
|
|
192
|
+
<label className="mb-2 block text-xs font-medium uppercase tracking-wide text-gray-700 dark:text-gray-300">
|
|
193
|
+
Email
|
|
194
|
+
</label>
|
|
195
|
+
<Input
|
|
196
|
+
type="email"
|
|
197
|
+
placeholder="name@company.com"
|
|
198
|
+
value={email}
|
|
199
|
+
onChange={e => setEmail(e.target.value)}
|
|
200
|
+
disabled={isLoading}
|
|
201
|
+
onKeyDown={e => {
|
|
202
|
+
if (e.key === "Enter") handleForgotPassword();
|
|
203
|
+
}}
|
|
204
|
+
className="h-11 px-4 text-[15px]"
|
|
205
|
+
/>
|
|
206
|
+
</div>
|
|
207
|
+
|
|
208
|
+
{error && <p className="text-sm text-red-500">{error}</p>}
|
|
209
|
+
|
|
210
|
+
<Button
|
|
211
|
+
onClick={handleForgotPassword}
|
|
212
|
+
disabled={isLoading}
|
|
213
|
+
className="h-11 w-full bg-gray-900 text-[15px] font-medium text-white hover:bg-gray-800 dark:bg-white dark:text-gray-900 dark:hover:bg-gray-100"
|
|
214
|
+
>
|
|
215
|
+
{isLoading ? "Sending..." : "Send reset link"}
|
|
216
|
+
</Button>
|
|
217
|
+
</>
|
|
218
|
+
)}
|
|
219
|
+
|
|
220
|
+
<p className="mt-8 text-center text-[14px] text-gray-500 dark:text-gray-400">
|
|
221
|
+
<button
|
|
222
|
+
onClick={() => {
|
|
223
|
+
setMode("sign-in");
|
|
224
|
+
setError(null);
|
|
225
|
+
setResetEmailSent(false);
|
|
226
|
+
}}
|
|
227
|
+
className="font-medium text-blue-600 hover:text-blue-500 dark:text-blue-400"
|
|
228
|
+
>
|
|
229
|
+
Back to sign in
|
|
230
|
+
</button>
|
|
231
|
+
</p>
|
|
232
|
+
</div>
|
|
233
|
+
)}
|
|
234
|
+
|
|
235
|
+
{/* Email/Password form */}
|
|
236
|
+
{showEmail && mode !== "forgot-password" && (
|
|
237
|
+
<div className="space-y-5">
|
|
238
|
+
{mode === "sign-up" && (
|
|
239
|
+
<div>
|
|
240
|
+
<label className="mb-2 block text-xs font-medium uppercase tracking-wide text-gray-700 dark:text-gray-300">
|
|
241
|
+
Name
|
|
242
|
+
</label>
|
|
243
|
+
<Input
|
|
244
|
+
type="text"
|
|
245
|
+
placeholder="Your name"
|
|
246
|
+
value={name}
|
|
247
|
+
onChange={e => setName(e.target.value)}
|
|
248
|
+
disabled={isLoading}
|
|
249
|
+
className="h-11 px-4 text-[15px]"
|
|
250
|
+
/>
|
|
251
|
+
</div>
|
|
252
|
+
)}
|
|
253
|
+
|
|
254
|
+
<div>
|
|
255
|
+
<label className="mb-2 block text-xs font-medium uppercase tracking-wide text-gray-700 dark:text-gray-300">
|
|
256
|
+
Email
|
|
257
|
+
</label>
|
|
258
|
+
<Input
|
|
259
|
+
type="email"
|
|
260
|
+
placeholder="name@company.com"
|
|
261
|
+
value={email}
|
|
262
|
+
onChange={e => setEmail(e.target.value)}
|
|
263
|
+
disabled={isLoading}
|
|
264
|
+
className="h-11 px-4 text-[15px]"
|
|
265
|
+
/>
|
|
266
|
+
</div>
|
|
267
|
+
|
|
268
|
+
<div>
|
|
269
|
+
<div className="mb-2 flex items-center justify-between">
|
|
270
|
+
<label className="block text-xs font-medium uppercase tracking-wide text-gray-700 dark:text-gray-300">
|
|
271
|
+
Password
|
|
272
|
+
</label>
|
|
273
|
+
{mode === "sign-in" && (
|
|
274
|
+
<button
|
|
275
|
+
type="button"
|
|
276
|
+
onClick={() => {
|
|
277
|
+
setMode("forgot-password");
|
|
278
|
+
setError(null);
|
|
279
|
+
}}
|
|
280
|
+
className="text-xs font-medium text-blue-600 hover:text-blue-500 dark:text-blue-400"
|
|
281
|
+
>
|
|
282
|
+
Forgot password?
|
|
283
|
+
</button>
|
|
284
|
+
)}
|
|
285
|
+
</div>
|
|
286
|
+
<Input
|
|
287
|
+
type="password"
|
|
288
|
+
placeholder="Password"
|
|
289
|
+
value={password}
|
|
290
|
+
onChange={e => setPassword(e.target.value)}
|
|
291
|
+
disabled={isLoading}
|
|
292
|
+
onKeyDown={e => {
|
|
293
|
+
if (e.key === "Enter") handleEmailSubmit();
|
|
294
|
+
}}
|
|
295
|
+
className="h-11 px-4 text-[15px]"
|
|
296
|
+
/>
|
|
297
|
+
</div>
|
|
298
|
+
|
|
299
|
+
{error && <p className="text-sm text-red-500">{error}</p>}
|
|
300
|
+
|
|
301
|
+
<Button
|
|
302
|
+
onClick={handleEmailSubmit}
|
|
303
|
+
disabled={isLoading}
|
|
304
|
+
className="h-11 w-full bg-gray-900 text-[15px] font-medium text-white hover:bg-gray-800 dark:bg-white dark:text-gray-900 dark:hover:bg-gray-100"
|
|
305
|
+
>
|
|
306
|
+
{isLoading ? "Loading..." : mode === "sign-in" ? "Sign in" : "Sign up"}
|
|
307
|
+
</Button>
|
|
308
|
+
</div>
|
|
309
|
+
)}
|
|
310
|
+
|
|
311
|
+
{/* Social providers */}
|
|
312
|
+
{providers.length > 0 && mode !== "forgot-password" && (
|
|
313
|
+
<>
|
|
314
|
+
{showEmail && (
|
|
315
|
+
<div className="my-8 flex items-center gap-4">
|
|
316
|
+
<div className="h-px flex-1 bg-gray-200 dark:bg-gray-700" />
|
|
317
|
+
<span className="text-xs text-gray-400">Or continue with</span>
|
|
318
|
+
<div className="h-px flex-1 bg-gray-200 dark:bg-gray-700" />
|
|
319
|
+
</div>
|
|
320
|
+
)}
|
|
321
|
+
|
|
322
|
+
<div className="space-y-4">
|
|
323
|
+
{providers.map(provider => {
|
|
324
|
+
const icon = strategyIcons[provider.id];
|
|
325
|
+
const label = strategyLabels[provider.id] || provider.label;
|
|
326
|
+
|
|
327
|
+
const isProviderLoading = loadingProvider === provider.id;
|
|
328
|
+
|
|
329
|
+
return (
|
|
330
|
+
<button
|
|
331
|
+
key={provider.id}
|
|
332
|
+
onClick={() => handleSocialSignIn(provider.id)}
|
|
333
|
+
disabled={isLoading}
|
|
334
|
+
style={{ paddingTop: "12px", paddingBottom: "12px" }}
|
|
335
|
+
className="flex w-full items-center justify-center gap-3 rounded-lg border border-gray-200 bg-white px-4 text-[14px] font-medium text-gray-700 transition-colors hover:bg-gray-50 disabled:opacity-50 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-200 dark:hover:bg-gray-700"
|
|
336
|
+
>
|
|
337
|
+
{isProviderLoading ? (
|
|
338
|
+
<svg className="h-5 w-5 animate-spin text-gray-400" viewBox="0 0 24 24" fill="none">
|
|
339
|
+
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
|
|
340
|
+
<path
|
|
341
|
+
className="opacity-75"
|
|
342
|
+
fill="currentColor"
|
|
343
|
+
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"
|
|
344
|
+
/>
|
|
345
|
+
</svg>
|
|
346
|
+
) : (
|
|
347
|
+
icon && <img src={icon} alt="" className="h-5 w-5" />
|
|
348
|
+
)}
|
|
349
|
+
<span>{isProviderLoading ? "Redirecting..." : label}</span>
|
|
350
|
+
</button>
|
|
351
|
+
);
|
|
352
|
+
})}
|
|
353
|
+
</div>
|
|
354
|
+
</>
|
|
355
|
+
)}
|
|
356
|
+
|
|
357
|
+
{/* Toggle sign-in / sign-up */}
|
|
358
|
+
{showEmail && mode !== "forgot-password" && (
|
|
359
|
+
<p className="mt-8 text-center text-[14px] text-gray-500 dark:text-gray-400">
|
|
360
|
+
{mode === "sign-in" ? "Don't have an account? " : "Already have an account? "}
|
|
361
|
+
<button
|
|
362
|
+
onClick={() => {
|
|
363
|
+
setMode(mode === "sign-in" ? "sign-up" : "sign-in");
|
|
364
|
+
setError(null);
|
|
365
|
+
}}
|
|
366
|
+
disabled={isLoading}
|
|
367
|
+
className="font-medium text-blue-600 hover:text-blue-500 dark:text-blue-400"
|
|
368
|
+
>
|
|
369
|
+
{mode === "sign-in" ? "Sign up" : "Sign in"}
|
|
370
|
+
</button>
|
|
371
|
+
</p>
|
|
372
|
+
)}
|
|
373
|
+
</div>
|
|
374
|
+
);
|
|
375
|
+
}
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
useGetAllTWSigners,
|
|
8
8
|
useModalStore,
|
|
9
9
|
} from "@b3dotfun/sdk/global-account/react";
|
|
10
|
+
import { LoginStepBetterAuth } from "./steps/LoginStepBetterAuth";
|
|
10
11
|
import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
|
|
11
12
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
12
13
|
import { useActiveAccount } from "thirdweb/react";
|
|
@@ -34,7 +35,7 @@ export function SignInWithB3Flow({
|
|
|
34
35
|
source = "signInWithB3Button",
|
|
35
36
|
signersEnabled = false,
|
|
36
37
|
}: SignInWithB3ModalProps) {
|
|
37
|
-
const { automaticallySetFirstEoa } = useB3Config();
|
|
38
|
+
const { automaticallySetFirstEoa, authStrategy } = useB3Config();
|
|
38
39
|
// skipAutoConnect: this component intentionally logs out on mount to show a fresh login screen.
|
|
39
40
|
// AuthenticationProvider is the sole owner of useAutoConnect to avoid competing auth cycles.
|
|
40
41
|
const { user, logout } = useAuthentication(partnerId, { skipAutoConnect: true });
|
|
@@ -42,10 +43,13 @@ export function SignInWithB3Flow({
|
|
|
42
43
|
// Tracks whether the pre-login logout has finished.
|
|
43
44
|
// We must not render ConnectEmbed until logout (wallet disconnect) completes,
|
|
44
45
|
// otherwise the wallet state disrupts ConnectEmbed causing a blank modal.
|
|
45
|
-
const [readyToShowLogin, setReadyToShowLogin] = useState(
|
|
46
|
+
const [readyToShowLogin, setReadyToShowLogin] = useState(
|
|
47
|
+
source === "requestPermissions" || authStrategy === "better-auth",
|
|
48
|
+
);
|
|
46
49
|
const hasLoggedOutRef = useRef(false);
|
|
47
50
|
useEffect(() => {
|
|
48
51
|
if (hasLoggedOutRef.current) return;
|
|
52
|
+
if (authStrategy === "better-auth") return;
|
|
49
53
|
if (source !== "requestPermissions") {
|
|
50
54
|
debug("Logging out before login");
|
|
51
55
|
hasLoggedOutRef.current = true;
|
|
@@ -53,7 +57,7 @@ export function SignInWithB3Flow({
|
|
|
53
57
|
setReadyToShowLogin(true);
|
|
54
58
|
});
|
|
55
59
|
}
|
|
56
|
-
}, [source, logout]);
|
|
60
|
+
}, [source, logout, authStrategy]);
|
|
57
61
|
|
|
58
62
|
const [step, setStep] = useState<"login" | "permissions" | null>(source === "requestPermissions" ? null : "login");
|
|
59
63
|
const [sessionKeyAdded, setSessionKeyAdded] = useState(source === "requestPermissions" ? true : false);
|
|
@@ -292,6 +296,8 @@ export function SignInWithB3Flow({
|
|
|
292
296
|
</div>
|
|
293
297
|
</LoginStepContainer>
|
|
294
298
|
);
|
|
299
|
+
} else if (authStrategy === "better-auth") {
|
|
300
|
+
content = <LoginStepBetterAuth onSuccess={() => handleLoginSuccess({} as Account)} onError={onError} />;
|
|
295
301
|
} else {
|
|
296
302
|
// Custom strategy
|
|
297
303
|
if (strategies?.[0] === "privy") {
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { Button, Input, LoginStepContainer, useB3Config } from "@b3dotfun/sdk/global-account/react";
|
|
2
|
+
import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { type BetterAuthSocialProvider, useBetterAuth } from "../../../hooks/useBetterAuth";
|
|
5
|
+
import { AuthButton } from "../components/AuthButton";
|
|
6
|
+
|
|
7
|
+
const debug = debugB3React("LoginStepBetterAuth");
|
|
8
|
+
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
9
|
+
|
|
10
|
+
const SOCIAL_PROVIDERS: { id: BetterAuthSocialProvider; label: string }[] = [
|
|
11
|
+
{ id: "google", label: "Google" },
|
|
12
|
+
{ id: "github", label: "GitHub" },
|
|
13
|
+
{ id: "discord", label: "Discord" },
|
|
14
|
+
{ id: "microsoft", label: "Microsoft" },
|
|
15
|
+
{ id: "slack", label: "Slack" },
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
interface LoginStepBetterAuthProps {
|
|
19
|
+
onSuccess?: () => void;
|
|
20
|
+
onError?: (error: Error) => Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function LoginStepBetterAuth({ onSuccess, onError }: LoginStepBetterAuthProps) {
|
|
24
|
+
const { partnerId } = useB3Config();
|
|
25
|
+
const { signInWithEmail, signUpWithEmail, signInWithSocial, requestPasswordReset } = useBetterAuth();
|
|
26
|
+
|
|
27
|
+
const [mode, setMode] = useState<"sign-in" | "sign-up" | "forgot-password">("sign-in");
|
|
28
|
+
const [showEmailForm, setShowEmailForm] = useState(false);
|
|
29
|
+
const [email, setEmail] = useState("");
|
|
30
|
+
const [password, setPassword] = useState("");
|
|
31
|
+
const [name, setName] = useState("");
|
|
32
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
33
|
+
const [error, setError] = useState<string | null>(null);
|
|
34
|
+
const [resetEmailSent, setResetEmailSent] = useState(false);
|
|
35
|
+
|
|
36
|
+
const handleEmailSubmit = async () => {
|
|
37
|
+
const normalizedEmail = email.trim().toLowerCase();
|
|
38
|
+
|
|
39
|
+
if (!EMAIL_REGEX.test(normalizedEmail)) {
|
|
40
|
+
setError("Please enter a valid email address");
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!password) {
|
|
45
|
+
setError("Please enter a password");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (mode === "sign-up" && !name.trim()) {
|
|
50
|
+
setError("Please enter your name");
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
setIsLoading(true);
|
|
56
|
+
setError(null);
|
|
57
|
+
|
|
58
|
+
if (mode === "sign-up") {
|
|
59
|
+
debug("Signing up", { email: normalizedEmail, name: name.trim() });
|
|
60
|
+
await signUpWithEmail(normalizedEmail, password, name.trim());
|
|
61
|
+
} else {
|
|
62
|
+
debug("Signing in", { email: normalizedEmail });
|
|
63
|
+
await signInWithEmail(normalizedEmail, password);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
debug("Auth successful");
|
|
67
|
+
onSuccess?.();
|
|
68
|
+
} catch (err) {
|
|
69
|
+
const message = err instanceof Error ? err.message : "Authentication failed";
|
|
70
|
+
setError(message);
|
|
71
|
+
debug("Auth failed", err);
|
|
72
|
+
await onError?.(err as Error);
|
|
73
|
+
} finally {
|
|
74
|
+
setPassword("");
|
|
75
|
+
setIsLoading(false);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const handleForgotPassword = async () => {
|
|
80
|
+
const normalizedEmail = email.trim().toLowerCase();
|
|
81
|
+
|
|
82
|
+
if (!EMAIL_REGEX.test(normalizedEmail)) {
|
|
83
|
+
setError("Please enter a valid email address");
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
setIsLoading(true);
|
|
89
|
+
setError(null);
|
|
90
|
+
await requestPasswordReset(normalizedEmail);
|
|
91
|
+
setResetEmailSent(true);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
const message = err instanceof Error ? err.message : "Failed to send reset email";
|
|
94
|
+
setError(message);
|
|
95
|
+
} finally {
|
|
96
|
+
setIsLoading(false);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const handleSocialSignIn = async (provider: BetterAuthSocialProvider) => {
|
|
101
|
+
try {
|
|
102
|
+
setIsLoading(true);
|
|
103
|
+
setError(null);
|
|
104
|
+
await signInWithSocial(provider);
|
|
105
|
+
} catch (err) {
|
|
106
|
+
const message = err instanceof Error ? err.message : `${provider} sign in failed`;
|
|
107
|
+
setError(message);
|
|
108
|
+
await onError?.(err as Error);
|
|
109
|
+
setIsLoading(false);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<LoginStepContainer partnerId={partnerId}>
|
|
115
|
+
{mode === "forgot-password" ? (
|
|
116
|
+
<div className="mb-6 w-full space-y-3 px-3">
|
|
117
|
+
<p className="text-center text-sm font-medium text-gray-900 dark:text-gray-100">Reset password</p>
|
|
118
|
+
<p className="text-center text-xs text-gray-500">Enter your email and we'll send you a reset link</p>
|
|
119
|
+
|
|
120
|
+
{resetEmailSent ? (
|
|
121
|
+
<div className="space-y-3 py-4 text-center">
|
|
122
|
+
<p className="text-sm text-green-600">Check your email for a reset link.</p>
|
|
123
|
+
</div>
|
|
124
|
+
) : (
|
|
125
|
+
<>
|
|
126
|
+
<Input
|
|
127
|
+
type="email"
|
|
128
|
+
placeholder="you@example.com"
|
|
129
|
+
value={email}
|
|
130
|
+
onChange={event => setEmail(event.target.value)}
|
|
131
|
+
disabled={isLoading}
|
|
132
|
+
onKeyDown={event => {
|
|
133
|
+
if (event.key === "Enter") handleForgotPassword();
|
|
134
|
+
}}
|
|
135
|
+
/>
|
|
136
|
+
|
|
137
|
+
{error && <p className="text-sm text-red-500">{error}</p>}
|
|
138
|
+
|
|
139
|
+
<Button onClick={handleForgotPassword} disabled={isLoading} className="w-full">
|
|
140
|
+
{isLoading ? "Sending..." : "Send reset link"}
|
|
141
|
+
</Button>
|
|
142
|
+
</>
|
|
143
|
+
)}
|
|
144
|
+
|
|
145
|
+
<button
|
|
146
|
+
onClick={() => {
|
|
147
|
+
setMode("sign-in");
|
|
148
|
+
setShowEmailForm(true);
|
|
149
|
+
setError(null);
|
|
150
|
+
setResetEmailSent(false);
|
|
151
|
+
}}
|
|
152
|
+
className="w-full text-center text-sm text-gray-500 hover:text-gray-700"
|
|
153
|
+
>
|
|
154
|
+
Back to sign in
|
|
155
|
+
</button>
|
|
156
|
+
</div>
|
|
157
|
+
) : showEmailForm ? (
|
|
158
|
+
<div className="mb-6 w-full space-y-3 px-3">
|
|
159
|
+
<p className="text-center text-sm font-medium text-gray-900 dark:text-gray-100">
|
|
160
|
+
{mode === "sign-in" ? "Sign in with email" : "Create an account"}
|
|
161
|
+
</p>
|
|
162
|
+
|
|
163
|
+
{mode === "sign-up" && (
|
|
164
|
+
<Input
|
|
165
|
+
type="text"
|
|
166
|
+
placeholder="Your name"
|
|
167
|
+
value={name}
|
|
168
|
+
onChange={event => setName(event.target.value)}
|
|
169
|
+
disabled={isLoading}
|
|
170
|
+
/>
|
|
171
|
+
)}
|
|
172
|
+
|
|
173
|
+
<Input
|
|
174
|
+
type="email"
|
|
175
|
+
placeholder="you@example.com"
|
|
176
|
+
value={email}
|
|
177
|
+
onChange={event => setEmail(event.target.value)}
|
|
178
|
+
disabled={isLoading}
|
|
179
|
+
/>
|
|
180
|
+
|
|
181
|
+
<Input
|
|
182
|
+
type="password"
|
|
183
|
+
placeholder="Password"
|
|
184
|
+
value={password}
|
|
185
|
+
onChange={event => setPassword(event.target.value)}
|
|
186
|
+
disabled={isLoading}
|
|
187
|
+
onKeyDown={event => {
|
|
188
|
+
if (event.key === "Enter") handleEmailSubmit();
|
|
189
|
+
}}
|
|
190
|
+
/>
|
|
191
|
+
|
|
192
|
+
{error && <p className="text-sm text-red-500">{error}</p>}
|
|
193
|
+
|
|
194
|
+
<Button onClick={handleEmailSubmit} disabled={isLoading} className="w-full">
|
|
195
|
+
{isLoading ? "Loading..." : mode === "sign-in" ? "Sign in" : "Sign up"}
|
|
196
|
+
</Button>
|
|
197
|
+
|
|
198
|
+
{mode === "sign-in" && (
|
|
199
|
+
<button
|
|
200
|
+
onClick={() => {
|
|
201
|
+
setMode("forgot-password");
|
|
202
|
+
setError(null);
|
|
203
|
+
}}
|
|
204
|
+
disabled={isLoading}
|
|
205
|
+
className="w-full text-center text-xs text-gray-500 hover:text-gray-700"
|
|
206
|
+
>
|
|
207
|
+
Forgot password?
|
|
208
|
+
</button>
|
|
209
|
+
)}
|
|
210
|
+
|
|
211
|
+
<button
|
|
212
|
+
onClick={() => {
|
|
213
|
+
setMode(mode === "sign-in" ? "sign-up" : "sign-in");
|
|
214
|
+
setError(null);
|
|
215
|
+
}}
|
|
216
|
+
disabled={isLoading}
|
|
217
|
+
className="w-full text-center text-sm text-gray-500 hover:text-gray-700"
|
|
218
|
+
>
|
|
219
|
+
{mode === "sign-in" ? "Don't have an account? Sign up" : "Already have an account? Sign in"}
|
|
220
|
+
</button>
|
|
221
|
+
|
|
222
|
+
<Button
|
|
223
|
+
variant="outline"
|
|
224
|
+
onClick={() => {
|
|
225
|
+
setShowEmailForm(false);
|
|
226
|
+
setError(null);
|
|
227
|
+
}}
|
|
228
|
+
disabled={isLoading}
|
|
229
|
+
className="w-full"
|
|
230
|
+
>
|
|
231
|
+
Back
|
|
232
|
+
</Button>
|
|
233
|
+
</div>
|
|
234
|
+
) : (
|
|
235
|
+
<div className="mb-6 w-full space-y-4 px-3">
|
|
236
|
+
{/* Social providers */}
|
|
237
|
+
<div className="grid w-full grid-cols-3 gap-4">
|
|
238
|
+
{SOCIAL_PROVIDERS.map(provider => (
|
|
239
|
+
<AuthButton
|
|
240
|
+
key={provider.id}
|
|
241
|
+
strategy={provider.id}
|
|
242
|
+
onClick={() => handleSocialSignIn(provider.id)}
|
|
243
|
+
isLoading={isLoading}
|
|
244
|
+
/>
|
|
245
|
+
))}
|
|
246
|
+
</div>
|
|
247
|
+
|
|
248
|
+
{/* Divider */}
|
|
249
|
+
<div className="flex items-center gap-3">
|
|
250
|
+
<div className="h-px flex-1 bg-gray-200" />
|
|
251
|
+
<span className="text-xs text-gray-400">or</span>
|
|
252
|
+
<div className="h-px flex-1 bg-gray-200" />
|
|
253
|
+
</div>
|
|
254
|
+
|
|
255
|
+
{/* Email option */}
|
|
256
|
+
<AuthButton strategy="email" onClick={() => setShowEmailForm(true)} isLoading={isLoading} />
|
|
257
|
+
|
|
258
|
+
{error && <p className="text-center text-sm text-red-500">{error}</p>}
|
|
259
|
+
</div>
|
|
260
|
+
)}
|
|
261
|
+
</LoginStepContainer>
|
|
262
|
+
);
|
|
263
|
+
}
|