bwh-auth 0.2.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/LICENSE +21 -0
- package/README.md +216 -0
- package/dist/index.cjs +852 -0
- package/dist/index.d.cts +166 -0
- package/dist/index.d.ts +166 -0
- package/dist/index.js +800 -0
- package/package.json +40 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,852 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
ChangePasswordForm: () => ChangePasswordForm,
|
|
34
|
+
LoginForm: () => LoginForm,
|
|
35
|
+
PasskeyLoginButton: () => PasskeyLoginButton,
|
|
36
|
+
PasskeySection: () => PasskeySection,
|
|
37
|
+
PasswordResetRequestForm: () => PasswordResetRequestForm,
|
|
38
|
+
ResetPasswordForm: () => ResetPasswordForm,
|
|
39
|
+
SignupForm: () => SignupForm,
|
|
40
|
+
TwoFactorForm: () => TwoFactorForm,
|
|
41
|
+
arrayBufferToBase64url: () => arrayBufferToBase64url,
|
|
42
|
+
authenticateWithPasskey: () => authenticateWithPasskey,
|
|
43
|
+
base64urlToArrayBuffer: () => base64urlToArrayBuffer,
|
|
44
|
+
getCsrfToken: () => getCsrfToken,
|
|
45
|
+
getDefaultPasskeyName: () => getDefaultPasskeyName,
|
|
46
|
+
isAbortError: () => isAbortError,
|
|
47
|
+
isConditionalMediationAvailable: () => isConditionalMediationAvailable,
|
|
48
|
+
registerPasskey: () => registerPasskey
|
|
49
|
+
});
|
|
50
|
+
module.exports = __toCommonJS(index_exports);
|
|
51
|
+
|
|
52
|
+
// src/forms.tsx
|
|
53
|
+
var React2 = __toESM(require("react"), 1);
|
|
54
|
+
|
|
55
|
+
// src/components.ts
|
|
56
|
+
var requiredComponentKeys = [
|
|
57
|
+
"Button",
|
|
58
|
+
"Card",
|
|
59
|
+
"CardContent",
|
|
60
|
+
"CardDescription",
|
|
61
|
+
"CardHeader",
|
|
62
|
+
"CardTitle",
|
|
63
|
+
"Input",
|
|
64
|
+
"Label"
|
|
65
|
+
];
|
|
66
|
+
function resolveAuthComponents(components) {
|
|
67
|
+
const missing = requiredComponentKeys.filter((key) => !components?.[key]);
|
|
68
|
+
if (missing.length > 0) {
|
|
69
|
+
throw new Error(`bwh-auth requires injected components: ${missing.join(", ")}`);
|
|
70
|
+
}
|
|
71
|
+
return components;
|
|
72
|
+
}
|
|
73
|
+
function resolveAuthButtonComponent(components) {
|
|
74
|
+
if (!components?.Button) {
|
|
75
|
+
throw new Error("bwh-auth requires an injected Button component");
|
|
76
|
+
}
|
|
77
|
+
return { Button: components.Button };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/passkey-login-button.tsx
|
|
81
|
+
var import_lucide_react = require("lucide-react");
|
|
82
|
+
var React = __toESM(require("react"), 1);
|
|
83
|
+
|
|
84
|
+
// src/webauthn-utils.ts
|
|
85
|
+
function getCsrfToken(explicitToken) {
|
|
86
|
+
if (explicitToken) {
|
|
87
|
+
return explicitToken;
|
|
88
|
+
}
|
|
89
|
+
return document.querySelector('meta[name="csrf-token"]')?.content ?? "";
|
|
90
|
+
}
|
|
91
|
+
function base64urlToArrayBuffer(b64) {
|
|
92
|
+
const base64 = b64.replace(/-/g, "+").replace(/_/g, "/");
|
|
93
|
+
const padded = base64.padEnd(base64.length + (4 - base64.length % 4) % 4, "=");
|
|
94
|
+
const binary = window.atob(padded);
|
|
95
|
+
const bytes = new Uint8Array(binary.length);
|
|
96
|
+
for (let i = 0; i < binary.length; i += 1) {
|
|
97
|
+
bytes[i] = binary.charCodeAt(i);
|
|
98
|
+
}
|
|
99
|
+
return bytes.buffer;
|
|
100
|
+
}
|
|
101
|
+
function arrayBufferToBase64url(buffer) {
|
|
102
|
+
const bytes = new Uint8Array(buffer);
|
|
103
|
+
let binary = "";
|
|
104
|
+
for (let i = 0; i < bytes.byteLength; i += 1) {
|
|
105
|
+
binary += String.fromCharCode(bytes[i] ?? 0);
|
|
106
|
+
}
|
|
107
|
+
return window.btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
108
|
+
}
|
|
109
|
+
function isAbortError(error) {
|
|
110
|
+
return error instanceof Error && error.name === "AbortError";
|
|
111
|
+
}
|
|
112
|
+
function getDefaultPasskeyName() {
|
|
113
|
+
if (typeof window === "undefined") return "Passkey";
|
|
114
|
+
const ua = window.navigator.userAgent;
|
|
115
|
+
let browser = "Unknown Browser";
|
|
116
|
+
let os = "Unknown OS";
|
|
117
|
+
if (ua.includes("Firefox")) browser = "Firefox";
|
|
118
|
+
else if (ua.includes("Edg")) browser = "Edge";
|
|
119
|
+
else if (ua.includes("Chrome")) browser = "Chrome";
|
|
120
|
+
else if (ua.includes("Safari")) browser = "Safari";
|
|
121
|
+
if (ua.includes("Mac OS X")) os = "macOS";
|
|
122
|
+
else if (ua.includes("Windows")) os = "Windows";
|
|
123
|
+
else if (ua.includes("Android")) os = "Android";
|
|
124
|
+
else if (ua.includes("iPhone") || ua.includes("iPad")) os = "iOS";
|
|
125
|
+
else if (ua.includes("Linux")) os = "Linux";
|
|
126
|
+
return `Passkey (${browser} on ${os})`;
|
|
127
|
+
}
|
|
128
|
+
async function isConditionalMediationAvailable() {
|
|
129
|
+
if (typeof window === "undefined" || !window.PublicKeyCredential) return false;
|
|
130
|
+
const credentialConstructor = window.PublicKeyCredential;
|
|
131
|
+
if (!credentialConstructor.isConditionalMediationAvailable) return false;
|
|
132
|
+
try {
|
|
133
|
+
return await credentialConstructor.isConditionalMediationAvailable();
|
|
134
|
+
} catch {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
async function authenticateWithPasskey({ endpoints = {}, mediation, signal } = {}) {
|
|
139
|
+
const authOptionsUrl = endpoints.passkeyAuthOptions ?? "/api/passkeys/auth/options";
|
|
140
|
+
const authUrl = endpoints.passkeyAuth ?? "/api/passkeys/auth";
|
|
141
|
+
const optRes = await fetch(authOptionsUrl, {
|
|
142
|
+
method: "POST",
|
|
143
|
+
headers: {
|
|
144
|
+
"Content-Type": "application/json",
|
|
145
|
+
"X-CSRF-TOKEN": getCsrfToken(endpoints.csrfToken)
|
|
146
|
+
},
|
|
147
|
+
signal
|
|
148
|
+
});
|
|
149
|
+
if (!optRes.ok) {
|
|
150
|
+
throw new Error("Failed to get authentication options");
|
|
151
|
+
}
|
|
152
|
+
const options = await optRes.json();
|
|
153
|
+
const publicKey = {
|
|
154
|
+
...options,
|
|
155
|
+
challenge: base64urlToArrayBuffer(options.challenge),
|
|
156
|
+
allowCredentials: (options.allowCredentials || []).map((credential2) => ({
|
|
157
|
+
...credential2,
|
|
158
|
+
id: base64urlToArrayBuffer(credential2.id)
|
|
159
|
+
}))
|
|
160
|
+
};
|
|
161
|
+
const credential = await navigator.credentials.get({ publicKey, mediation, signal });
|
|
162
|
+
if (!credential || credential.type !== "public-key") {
|
|
163
|
+
throw new Error("No passkey selected");
|
|
164
|
+
}
|
|
165
|
+
const pkCredential = credential;
|
|
166
|
+
const response = pkCredential.response;
|
|
167
|
+
const credentialData = {
|
|
168
|
+
id: pkCredential.id,
|
|
169
|
+
rawId: arrayBufferToBase64url(pkCredential.rawId),
|
|
170
|
+
type: pkCredential.type,
|
|
171
|
+
response: {
|
|
172
|
+
clientDataJSON: arrayBufferToBase64url(response.clientDataJSON),
|
|
173
|
+
authenticatorData: arrayBufferToBase64url(response.authenticatorData),
|
|
174
|
+
signature: arrayBufferToBase64url(response.signature),
|
|
175
|
+
userHandle: response.userHandle ? arrayBufferToBase64url(response.userHandle) : null
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
const authRes = await fetch(authUrl, {
|
|
179
|
+
method: "POST",
|
|
180
|
+
headers: {
|
|
181
|
+
"Content-Type": "application/json",
|
|
182
|
+
"X-CSRF-TOKEN": getCsrfToken(endpoints.csrfToken)
|
|
183
|
+
},
|
|
184
|
+
body: JSON.stringify({ credential: credentialData }),
|
|
185
|
+
signal
|
|
186
|
+
});
|
|
187
|
+
const result = await authRes.json();
|
|
188
|
+
if (!authRes.ok) {
|
|
189
|
+
throw new Error(result.error || result.message || "Authentication failed");
|
|
190
|
+
}
|
|
191
|
+
return {
|
|
192
|
+
result,
|
|
193
|
+
redirectUrl: result.redirect || "/"
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
async function registerPasskey({ endpoints = {}, name, signal } = {}) {
|
|
197
|
+
const registerOptionsUrl = endpoints.passkeyRegisterOptions ?? "/api/passkeys/register/options";
|
|
198
|
+
const registerUrl = endpoints.passkeyRegister ?? "/api/passkeys/register";
|
|
199
|
+
const passkeyName = name || getDefaultPasskeyName();
|
|
200
|
+
const optRes = await fetch(registerOptionsUrl, {
|
|
201
|
+
method: "POST",
|
|
202
|
+
headers: {
|
|
203
|
+
"Content-Type": "application/json",
|
|
204
|
+
"X-CSRF-TOKEN": getCsrfToken(endpoints.csrfToken)
|
|
205
|
+
},
|
|
206
|
+
signal
|
|
207
|
+
});
|
|
208
|
+
if (!optRes.ok) {
|
|
209
|
+
throw new Error("Failed to get registration options");
|
|
210
|
+
}
|
|
211
|
+
const options = await optRes.json();
|
|
212
|
+
const publicKey = {
|
|
213
|
+
...options,
|
|
214
|
+
challenge: base64urlToArrayBuffer(options.challenge),
|
|
215
|
+
user: {
|
|
216
|
+
...options.user,
|
|
217
|
+
id: base64urlToArrayBuffer(options.user.id)
|
|
218
|
+
},
|
|
219
|
+
excludeCredentials: (options.excludeCredentials || []).map((credential2) => ({
|
|
220
|
+
...credential2,
|
|
221
|
+
id: base64urlToArrayBuffer(credential2.id)
|
|
222
|
+
}))
|
|
223
|
+
};
|
|
224
|
+
const credential = await navigator.credentials.create({ publicKey, signal });
|
|
225
|
+
if (!credential || credential.type !== "public-key") {
|
|
226
|
+
throw new Error("Failed to create credential");
|
|
227
|
+
}
|
|
228
|
+
const pkCredential = credential;
|
|
229
|
+
const response = pkCredential.response;
|
|
230
|
+
const credentialData = {
|
|
231
|
+
id: pkCredential.id,
|
|
232
|
+
rawId: arrayBufferToBase64url(pkCredential.rawId),
|
|
233
|
+
type: pkCredential.type,
|
|
234
|
+
response: {
|
|
235
|
+
clientDataJSON: arrayBufferToBase64url(response.clientDataJSON),
|
|
236
|
+
attestationObject: arrayBufferToBase64url(response.attestationObject),
|
|
237
|
+
transports: response.getTransports ? response.getTransports() : []
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
const verifyRes = await fetch(registerUrl, {
|
|
241
|
+
method: "POST",
|
|
242
|
+
headers: {
|
|
243
|
+
"Content-Type": "application/json",
|
|
244
|
+
"X-CSRF-TOKEN": getCsrfToken(endpoints.csrfToken)
|
|
245
|
+
},
|
|
246
|
+
body: JSON.stringify({ credential: credentialData, name: passkeyName }),
|
|
247
|
+
signal
|
|
248
|
+
});
|
|
249
|
+
const result = await verifyRes.json();
|
|
250
|
+
if (!verifyRes.ok) {
|
|
251
|
+
throw new Error(result.error || result.message || "Registration failed");
|
|
252
|
+
}
|
|
253
|
+
return { result };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// src/passkey-login-button.tsx
|
|
257
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
258
|
+
function PasskeyLoginButton({ endpoints = {}, components, className, onSuccess, onError }) {
|
|
259
|
+
const { Button } = resolveAuthButtonComponent(components);
|
|
260
|
+
const [loading, setLoading] = React.useState(false);
|
|
261
|
+
const [error, setError] = React.useState(null);
|
|
262
|
+
async function handlePasskeyLogin() {
|
|
263
|
+
setError(null);
|
|
264
|
+
setLoading(true);
|
|
265
|
+
try {
|
|
266
|
+
const { redirectUrl, result } = await authenticateWithPasskey({ endpoints });
|
|
267
|
+
if (onSuccess) {
|
|
268
|
+
onSuccess(redirectUrl, result);
|
|
269
|
+
} else {
|
|
270
|
+
window.location.href = redirectUrl;
|
|
271
|
+
}
|
|
272
|
+
} catch (caughtError) {
|
|
273
|
+
if (isAbortError(caughtError)) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const message = caughtError instanceof Error ? caughtError.message : "Passkey login failed";
|
|
277
|
+
setError(message);
|
|
278
|
+
onError?.(message);
|
|
279
|
+
} finally {
|
|
280
|
+
setLoading(false);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
if (typeof window === "undefined" || !window.PublicKeyCredential) {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className, children: [
|
|
287
|
+
error ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { className: "mb-2 text-sm text-destructive", children: error }) : null,
|
|
288
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Button, { type: "button", variant: "outline", className: "w-full", disabled: loading, onClick: handlePasskeyLogin, children: [
|
|
289
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_lucide_react.KeyRound, { "aria-hidden": "true" }),
|
|
290
|
+
loading ? "Verifying..." : "Sign in with Passkey"
|
|
291
|
+
] })
|
|
292
|
+
] });
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// src/forms.tsx
|
|
296
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
297
|
+
var AuthRequestError = class extends Error {
|
|
298
|
+
constructor(message, result) {
|
|
299
|
+
super(message);
|
|
300
|
+
this.result = result;
|
|
301
|
+
this.name = "AuthRequestError";
|
|
302
|
+
}
|
|
303
|
+
result;
|
|
304
|
+
};
|
|
305
|
+
async function postForm(url, body, csrfToken) {
|
|
306
|
+
const response = await fetch(url, {
|
|
307
|
+
method: "POST",
|
|
308
|
+
headers: {
|
|
309
|
+
"Content-Type": "application/json",
|
|
310
|
+
"Accept": "application/json",
|
|
311
|
+
"X-CSRF-TOKEN": getCsrfToken(csrfToken)
|
|
312
|
+
},
|
|
313
|
+
body: JSON.stringify(body)
|
|
314
|
+
});
|
|
315
|
+
const result = await response.json();
|
|
316
|
+
if (!response.ok) {
|
|
317
|
+
throw new AuthRequestError(result.message || result.error || "Request failed", result);
|
|
318
|
+
}
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
function LoginForm({
|
|
322
|
+
endpoints = {},
|
|
323
|
+
components,
|
|
324
|
+
onSuccess,
|
|
325
|
+
onError,
|
|
326
|
+
onTwoFactorRequired,
|
|
327
|
+
onPasskeySuccess,
|
|
328
|
+
enablePasskeys = false,
|
|
329
|
+
enablePasskeyAutofill = enablePasskeys
|
|
330
|
+
}) {
|
|
331
|
+
const { Button, Card, CardContent, CardHeader, CardTitle, Input, Label } = resolveAuthComponents(components);
|
|
332
|
+
const [email, setEmail] = React2.useState("");
|
|
333
|
+
const [password, setPassword] = React2.useState("");
|
|
334
|
+
const [remember, setRemember] = React2.useState(false);
|
|
335
|
+
const [loading, setLoading] = React2.useState(false);
|
|
336
|
+
const [conditionalPasskeyAvailable, setConditionalPasskeyAvailable] = React2.useState(false);
|
|
337
|
+
const passkeyEndpoints = React2.useMemo(() => ({
|
|
338
|
+
csrfToken: endpoints.csrfToken,
|
|
339
|
+
passkeyAuth: endpoints.passkeyAuth,
|
|
340
|
+
passkeyAuthOptions: endpoints.passkeyAuthOptions
|
|
341
|
+
}), [endpoints.csrfToken, endpoints.passkeyAuth, endpoints.passkeyAuthOptions]);
|
|
342
|
+
React2.useEffect(() => {
|
|
343
|
+
if (!enablePasskeyAutofill) return;
|
|
344
|
+
const abortController = new AbortController();
|
|
345
|
+
let active = true;
|
|
346
|
+
async function startConditionalPasskeyLogin() {
|
|
347
|
+
const available = await isConditionalMediationAvailable();
|
|
348
|
+
if (!available || !active) return;
|
|
349
|
+
setConditionalPasskeyAvailable(true);
|
|
350
|
+
try {
|
|
351
|
+
const { redirectUrl, result } = await authenticateWithPasskey({
|
|
352
|
+
endpoints: passkeyEndpoints,
|
|
353
|
+
mediation: "conditional",
|
|
354
|
+
signal: abortController.signal
|
|
355
|
+
});
|
|
356
|
+
if (!active) return;
|
|
357
|
+
if (onPasskeySuccess) {
|
|
358
|
+
onPasskeySuccess(redirectUrl, result);
|
|
359
|
+
} else {
|
|
360
|
+
window.location.href = redirectUrl;
|
|
361
|
+
}
|
|
362
|
+
} catch (error) {
|
|
363
|
+
if (!isAbortError(error) && active) {
|
|
364
|
+
onError?.(error instanceof Error ? error.message : "Passkey login failed");
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
void startConditionalPasskeyLogin();
|
|
369
|
+
return () => {
|
|
370
|
+
active = false;
|
|
371
|
+
abortController.abort();
|
|
372
|
+
};
|
|
373
|
+
}, [enablePasskeyAutofill, onError, onPasskeySuccess, passkeyEndpoints]);
|
|
374
|
+
async function onSubmit(event) {
|
|
375
|
+
event.preventDefault();
|
|
376
|
+
setLoading(true);
|
|
377
|
+
try {
|
|
378
|
+
const result = await postForm(endpoints.login ?? "/login", { email, password, remember }, endpoints.csrfToken);
|
|
379
|
+
if (result.requires_2fa) {
|
|
380
|
+
onTwoFactorRequired?.(result);
|
|
381
|
+
if (!onTwoFactorRequired && typeof result.attempt_token === "string") {
|
|
382
|
+
window.location.href = `/login/two-factor/${encodeURIComponent(result.attempt_token)}`;
|
|
383
|
+
}
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
onSuccess?.(result);
|
|
387
|
+
if (!onSuccess && result.redirect) window.location.href = result.redirect;
|
|
388
|
+
} catch (error) {
|
|
389
|
+
onError?.(error instanceof Error ? error.message : "Login failed");
|
|
390
|
+
} finally {
|
|
391
|
+
setLoading(false);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(Card, { children: [
|
|
395
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(CardTitle, { children: "Sign In" }) }),
|
|
396
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(CardContent, { children: [
|
|
397
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("form", { className: "space-y-4", onSubmit: (event) => void onSubmit(event), children: [
|
|
398
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "space-y-1", children: [
|
|
399
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Label, { htmlFor: "login-email", children: "Email" }),
|
|
400
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Input, { id: "login-email", type: "email", autoComplete: conditionalPasskeyAvailable ? "username webauthn" : "email", required: true, value: email, onChange: (event) => setEmail(event.target.value) })
|
|
401
|
+
] }),
|
|
402
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "space-y-1", children: [
|
|
403
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Label, { htmlFor: "login-password", children: "Password" }),
|
|
404
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Input, { id: "login-password", type: "password", autoComplete: "current-password", required: true, value: password, onChange: (event) => setPassword(event.target.value) })
|
|
405
|
+
] }),
|
|
406
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("label", { className: "flex items-center gap-2 text-sm", children: [
|
|
407
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { type: "checkbox", checked: remember, onChange: (event) => setRemember(event.target.checked) }),
|
|
408
|
+
"Keep me signed in"
|
|
409
|
+
] }),
|
|
410
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Button, { type: "submit", className: "w-full", disabled: loading, children: loading ? "Signing in..." : "Sign In" })
|
|
411
|
+
] }),
|
|
412
|
+
enablePasskeys ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "mt-4", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
413
|
+
PasskeyLoginButton,
|
|
414
|
+
{
|
|
415
|
+
components: { Button },
|
|
416
|
+
endpoints,
|
|
417
|
+
onSuccess: (redirectUrl, result) => {
|
|
418
|
+
if (onPasskeySuccess) {
|
|
419
|
+
onPasskeySuccess(redirectUrl, result);
|
|
420
|
+
} else {
|
|
421
|
+
window.location.href = redirectUrl;
|
|
422
|
+
}
|
|
423
|
+
},
|
|
424
|
+
onError
|
|
425
|
+
}
|
|
426
|
+
) }) : null
|
|
427
|
+
] })
|
|
428
|
+
] });
|
|
429
|
+
}
|
|
430
|
+
function defaultSignupFields() {
|
|
431
|
+
return [
|
|
432
|
+
{ name: "name", label: "Name", required: true, autoComplete: "name" },
|
|
433
|
+
{ name: "email", label: "Email", type: "email", required: true, autoComplete: "email" },
|
|
434
|
+
{ name: "password", label: "Password", type: "password", required: true, autoComplete: "new-password" },
|
|
435
|
+
{ name: "password_confirmation", label: "Confirm Password", type: "password", required: true, autoComplete: "new-password" }
|
|
436
|
+
];
|
|
437
|
+
}
|
|
438
|
+
function initialSignupValue(field, initialValues) {
|
|
439
|
+
if (Object.prototype.hasOwnProperty.call(initialValues, field.name)) return initialValues[field.name];
|
|
440
|
+
if (field.initialValue !== void 0) return field.initialValue;
|
|
441
|
+
return field.type === "checkbox" ? false : "";
|
|
442
|
+
}
|
|
443
|
+
function SignupForm({
|
|
444
|
+
endpoints = {},
|
|
445
|
+
components,
|
|
446
|
+
onSuccess,
|
|
447
|
+
onError,
|
|
448
|
+
fields = defaultSignupFields(),
|
|
449
|
+
initialValues = {},
|
|
450
|
+
errors = {},
|
|
451
|
+
submitMode = "fetch",
|
|
452
|
+
title = "Create Account",
|
|
453
|
+
description,
|
|
454
|
+
submitLabel = "Create Account",
|
|
455
|
+
submittingLabel = "Creating account..."
|
|
456
|
+
}) {
|
|
457
|
+
const { Button, Card, CardContent, CardDescription, CardHeader, CardTitle, Input, Label } = resolveAuthComponents(components);
|
|
458
|
+
const [values, setValues] = React2.useState(() => Object.fromEntries(
|
|
459
|
+
fields.map((field) => [field.name, initialSignupValue(field, initialValues)])
|
|
460
|
+
));
|
|
461
|
+
const [fieldErrors, setFieldErrors] = React2.useState(errors);
|
|
462
|
+
const [loading, setLoading] = React2.useState(false);
|
|
463
|
+
async function onSubmit(event) {
|
|
464
|
+
setLoading(true);
|
|
465
|
+
if (submitMode === "native") {
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
event.preventDefault();
|
|
469
|
+
setFieldErrors({});
|
|
470
|
+
try {
|
|
471
|
+
const result = await postForm(endpoints.signup ?? "/register", values, endpoints.csrfToken);
|
|
472
|
+
await onSuccess?.(result, values);
|
|
473
|
+
if (!onSuccess && result.redirect) window.location.href = result.redirect;
|
|
474
|
+
} catch (error) {
|
|
475
|
+
if (error instanceof AuthRequestError && error.result.errors && typeof error.result.errors === "object") {
|
|
476
|
+
setFieldErrors(error.result.errors);
|
|
477
|
+
}
|
|
478
|
+
onError?.(error instanceof Error ? error.message : "Signup failed");
|
|
479
|
+
} finally {
|
|
480
|
+
setLoading(false);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
function setValue(name, value) {
|
|
484
|
+
setValues((current) => ({ ...current, [name]: value }));
|
|
485
|
+
}
|
|
486
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(Card, { children: [
|
|
487
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(CardHeader, { children: [
|
|
488
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(CardTitle, { children: title }),
|
|
489
|
+
description ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(CardDescription, { children: description }) : null
|
|
490
|
+
] }),
|
|
491
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(CardContent, { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("form", { className: "space-y-4", method: submitMode === "native" ? "POST" : void 0, action: endpoints.signup ?? "/register", onSubmit: (event) => void onSubmit(event), children: [
|
|
492
|
+
submitMode === "native" ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { type: "hidden", name: "_token", value: getCsrfToken(endpoints.csrfToken) }) : null,
|
|
493
|
+
fields.map((field) => {
|
|
494
|
+
if (field.hiddenWhen?.(values)) return null;
|
|
495
|
+
const error = fieldErrors[field.name]?.[0];
|
|
496
|
+
const value = values[field.name];
|
|
497
|
+
if (field.type === "checkbox") {
|
|
498
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: field.containerClassName ?? "space-y-1", children: [
|
|
499
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("label", { className: "flex items-start gap-2 text-sm", children: [
|
|
500
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
501
|
+
"input",
|
|
502
|
+
{
|
|
503
|
+
id: `signup-${field.name}`,
|
|
504
|
+
name: field.name,
|
|
505
|
+
type: "checkbox",
|
|
506
|
+
value: "1",
|
|
507
|
+
checked: Boolean(value),
|
|
508
|
+
onChange: (event) => setValue(field.name, event.target.checked),
|
|
509
|
+
required: field.required
|
|
510
|
+
}
|
|
511
|
+
),
|
|
512
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: field.label })
|
|
513
|
+
] }),
|
|
514
|
+
field.helpText ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: "text-xs text-muted-foreground", children: field.helpText }) : null,
|
|
515
|
+
error ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: "text-sm text-destructive", children: error }) : null
|
|
516
|
+
] }, field.name);
|
|
517
|
+
}
|
|
518
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: field.containerClassName ?? "space-y-1", children: [
|
|
519
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Label, { htmlFor: `signup-${field.name}`, children: field.label }),
|
|
520
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
521
|
+
Input,
|
|
522
|
+
{
|
|
523
|
+
id: `signup-${field.name}`,
|
|
524
|
+
name: field.name,
|
|
525
|
+
type: field.type ?? "text",
|
|
526
|
+
placeholder: field.placeholder,
|
|
527
|
+
required: field.required,
|
|
528
|
+
autoComplete: field.autoComplete,
|
|
529
|
+
minLength: field.minLength,
|
|
530
|
+
maxLength: field.maxLength,
|
|
531
|
+
pattern: field.pattern,
|
|
532
|
+
inputMode: field.inputMode,
|
|
533
|
+
className: field.className,
|
|
534
|
+
"aria-invalid": Boolean(error),
|
|
535
|
+
value: String(value ?? ""),
|
|
536
|
+
onChange: (event) => setValue(field.name, event.target.value)
|
|
537
|
+
}
|
|
538
|
+
),
|
|
539
|
+
field.helpText ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: "text-xs text-muted-foreground", children: field.helpText }) : null,
|
|
540
|
+
error ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: "text-sm text-destructive", children: error }) : null
|
|
541
|
+
] }, field.name);
|
|
542
|
+
}),
|
|
543
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Button, { type: "submit", className: "w-full", disabled: loading, children: loading ? submittingLabel : submitLabel })
|
|
544
|
+
] }) })
|
|
545
|
+
] });
|
|
546
|
+
}
|
|
547
|
+
function PasswordResetRequestForm({ endpoints = {}, components, onSuccess, onError }) {
|
|
548
|
+
const { Button, Input, Label } = resolveAuthComponents(components);
|
|
549
|
+
const [email, setEmail] = React2.useState("");
|
|
550
|
+
const [loading, setLoading] = React2.useState(false);
|
|
551
|
+
async function onSubmit(event) {
|
|
552
|
+
event.preventDefault();
|
|
553
|
+
setLoading(true);
|
|
554
|
+
try {
|
|
555
|
+
const result = await postForm(endpoints.forgotPassword ?? "/api/auth/forgot-password", { email }, endpoints.csrfToken);
|
|
556
|
+
onSuccess?.(result);
|
|
557
|
+
} catch (error) {
|
|
558
|
+
onError?.(error instanceof Error ? error.message : "Password reset request failed");
|
|
559
|
+
} finally {
|
|
560
|
+
setLoading(false);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("form", { className: "space-y-4", onSubmit: (event) => void onSubmit(event), children: [
|
|
564
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "space-y-1", children: [
|
|
565
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Label, { htmlFor: "reset-email", children: "Email" }),
|
|
566
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Input, { id: "reset-email", type: "email", autoComplete: "email", required: true, value: email, onChange: (event) => setEmail(event.target.value) })
|
|
567
|
+
] }),
|
|
568
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Button, { type: "submit", disabled: loading, children: loading ? "Sending..." : "Send Reset Link" })
|
|
569
|
+
] });
|
|
570
|
+
}
|
|
571
|
+
function ResetPasswordForm({ endpoints = {}, components, onSuccess, onError, token: initialToken = "", email: initialEmail = "" }) {
|
|
572
|
+
const { Button, Input, Label } = resolveAuthComponents(components);
|
|
573
|
+
const [email, setEmail] = React2.useState(initialEmail);
|
|
574
|
+
const [token, setToken] = React2.useState(initialToken);
|
|
575
|
+
const [password, setPassword] = React2.useState("");
|
|
576
|
+
const [passwordConfirmation, setPasswordConfirmation] = React2.useState("");
|
|
577
|
+
async function onSubmit(event) {
|
|
578
|
+
event.preventDefault();
|
|
579
|
+
try {
|
|
580
|
+
const result = await postForm(endpoints.resetPassword ?? "/api/auth/reset-password", {
|
|
581
|
+
email,
|
|
582
|
+
token,
|
|
583
|
+
password,
|
|
584
|
+
password_confirmation: passwordConfirmation
|
|
585
|
+
}, endpoints.csrfToken);
|
|
586
|
+
onSuccess?.(result);
|
|
587
|
+
} catch (error) {
|
|
588
|
+
onError?.(error instanceof Error ? error.message : "Password reset failed");
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("form", { className: "space-y-4", onSubmit: (event) => void onSubmit(event), children: [
|
|
592
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Input, { type: "hidden", value: token, onChange: (event) => setToken(event.target.value) }),
|
|
593
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "space-y-1", children: [
|
|
594
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Label, { htmlFor: "reset-password-email", children: "Email" }),
|
|
595
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Input, { id: "reset-password-email", type: "email", required: true, value: email, onChange: (event) => setEmail(event.target.value) })
|
|
596
|
+
] }),
|
|
597
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "space-y-1", children: [
|
|
598
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Label, { htmlFor: "reset-password", children: "Password" }),
|
|
599
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Input, { id: "reset-password", type: "password", required: true, value: password, onChange: (event) => setPassword(event.target.value) })
|
|
600
|
+
] }),
|
|
601
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "space-y-1", children: [
|
|
602
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Label, { htmlFor: "reset-password-confirmation", children: "Confirm Password" }),
|
|
603
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Input, { id: "reset-password-confirmation", type: "password", required: true, value: passwordConfirmation, onChange: (event) => setPasswordConfirmation(event.target.value) })
|
|
604
|
+
] }),
|
|
605
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Button, { type: "submit", children: "Reset Password" })
|
|
606
|
+
] });
|
|
607
|
+
}
|
|
608
|
+
function ChangePasswordForm({ endpoints = {}, components, onSuccess, onError }) {
|
|
609
|
+
const { Button, Input, Label } = resolveAuthComponents(components);
|
|
610
|
+
const [currentPassword, setCurrentPassword] = React2.useState("");
|
|
611
|
+
const [password, setPassword] = React2.useState("");
|
|
612
|
+
const [passwordConfirmation, setPasswordConfirmation] = React2.useState("");
|
|
613
|
+
const [loading, setLoading] = React2.useState(false);
|
|
614
|
+
async function onSubmit(event) {
|
|
615
|
+
event.preventDefault();
|
|
616
|
+
setLoading(true);
|
|
617
|
+
if (password !== passwordConfirmation) {
|
|
618
|
+
onError?.("New passwords do not match.");
|
|
619
|
+
setLoading(false);
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
try {
|
|
623
|
+
const result = await postForm(endpoints.changePassword ?? "/api/change-password", {
|
|
624
|
+
current_password: currentPassword,
|
|
625
|
+
password,
|
|
626
|
+
password_confirmation: passwordConfirmation
|
|
627
|
+
}, endpoints.csrfToken);
|
|
628
|
+
setCurrentPassword("");
|
|
629
|
+
setPassword("");
|
|
630
|
+
setPasswordConfirmation("");
|
|
631
|
+
onSuccess?.(result);
|
|
632
|
+
} catch (error) {
|
|
633
|
+
onError?.(error instanceof Error ? error.message : "Password change failed");
|
|
634
|
+
} finally {
|
|
635
|
+
setLoading(false);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("form", { className: "space-y-4", onSubmit: (event) => void onSubmit(event), children: [
|
|
639
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "space-y-1", children: [
|
|
640
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Label, { htmlFor: "current-password", children: "Current Password" }),
|
|
641
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Input, { id: "current-password", type: "password", autoComplete: "current-password", required: true, value: currentPassword, onChange: (event) => setCurrentPassword(event.target.value) })
|
|
642
|
+
] }),
|
|
643
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "space-y-1", children: [
|
|
644
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Label, { htmlFor: "new-password", children: "New Password" }),
|
|
645
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Input, { id: "new-password", type: "password", autoComplete: "new-password", minLength: 8, required: true, value: password, onChange: (event) => setPassword(event.target.value) })
|
|
646
|
+
] }),
|
|
647
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "space-y-1", children: [
|
|
648
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Label, { htmlFor: "confirm-password", children: "Confirm New Password" }),
|
|
649
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Input, { id: "confirm-password", type: "password", autoComplete: "new-password", minLength: 8, required: true, value: passwordConfirmation, onChange: (event) => setPasswordConfirmation(event.target.value) })
|
|
650
|
+
] }),
|
|
651
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Button, { type: "submit", disabled: loading, children: loading ? "Changing..." : "Change Password" })
|
|
652
|
+
] });
|
|
653
|
+
}
|
|
654
|
+
function TwoFactorForm({ endpoints = {}, components, attemptToken, appEnv, onSuccess, onError, onReportSuspicious }) {
|
|
655
|
+
const { Button, Card, CardContent, CardDescription, CardHeader, CardTitle, Input, Label } = resolveAuthComponents(components);
|
|
656
|
+
const [currentAttemptToken, setCurrentAttemptToken] = React2.useState(attemptToken);
|
|
657
|
+
const [code, setCode] = React2.useState("");
|
|
658
|
+
const [loading, setLoading] = React2.useState(false);
|
|
659
|
+
const [resending, setResending] = React2.useState(false);
|
|
660
|
+
const [message, setMessage] = React2.useState("");
|
|
661
|
+
React2.useEffect(() => setCurrentAttemptToken(attemptToken), [attemptToken]);
|
|
662
|
+
async function onSubmit(event) {
|
|
663
|
+
event.preventDefault();
|
|
664
|
+
setLoading(true);
|
|
665
|
+
setMessage("");
|
|
666
|
+
try {
|
|
667
|
+
const result = await postForm(endpoints.twoFactorVerify ?? "/api/auth/two-factor/verify", {
|
|
668
|
+
attempt_token: currentAttemptToken,
|
|
669
|
+
code: code.trim()
|
|
670
|
+
}, endpoints.csrfToken);
|
|
671
|
+
onSuccess?.(result);
|
|
672
|
+
if (!onSuccess && result.redirect) window.location.href = result.redirect;
|
|
673
|
+
} catch (error) {
|
|
674
|
+
setCode("");
|
|
675
|
+
onError?.(error instanceof Error ? error.message : "Verification failed");
|
|
676
|
+
} finally {
|
|
677
|
+
setLoading(false);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
async function resend() {
|
|
681
|
+
setResending(true);
|
|
682
|
+
setMessage("");
|
|
683
|
+
try {
|
|
684
|
+
const result = await postForm(endpoints.twoFactorResend ?? "/api/auth/two-factor/resend", {
|
|
685
|
+
attempt_token: currentAttemptToken
|
|
686
|
+
}, endpoints.csrfToken);
|
|
687
|
+
if (typeof result.attempt_token === "string") setCurrentAttemptToken(result.attempt_token);
|
|
688
|
+
setMessage(result.message ?? "A new verification code has been sent.");
|
|
689
|
+
} catch (error) {
|
|
690
|
+
onError?.(error instanceof Error ? error.message : "Could not resend verification code");
|
|
691
|
+
} finally {
|
|
692
|
+
setResending(false);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
async function reportSuspicious() {
|
|
696
|
+
const url = endpoints.twoFactorReport?.(currentAttemptToken) ?? `/api/auth/two-factor/report/${encodeURIComponent(currentAttemptToken)}`;
|
|
697
|
+
try {
|
|
698
|
+
const result = await postForm(url, {}, endpoints.csrfToken);
|
|
699
|
+
setMessage(result.message ?? "This login attempt has been reported.");
|
|
700
|
+
onReportSuspicious?.(result);
|
|
701
|
+
} catch (error) {
|
|
702
|
+
onError?.(error instanceof Error ? error.message : "Could not report the login attempt");
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(Card, { children: [
|
|
706
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(CardHeader, { children: [
|
|
707
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(CardTitle, { children: "Verify Your Login" }),
|
|
708
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(CardDescription, { children: "Enter the 6-digit code sent to your email address." })
|
|
709
|
+
] }),
|
|
710
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(CardContent, { children: [
|
|
711
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("form", { className: "space-y-4", onSubmit: (event) => void onSubmit(event), children: [
|
|
712
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "space-y-1", children: [
|
|
713
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Label, { htmlFor: "two-factor-code", children: "Verification Code" }),
|
|
714
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
715
|
+
Input,
|
|
716
|
+
{
|
|
717
|
+
id: "two-factor-code",
|
|
718
|
+
type: "text",
|
|
719
|
+
inputMode: "numeric",
|
|
720
|
+
pattern: "[0-9]{6}",
|
|
721
|
+
maxLength: 6,
|
|
722
|
+
autoComplete: "one-time-code",
|
|
723
|
+
required: true,
|
|
724
|
+
value: code,
|
|
725
|
+
onChange: (event) => setCode(event.target.value.replace(/\D/g, ""))
|
|
726
|
+
}
|
|
727
|
+
)
|
|
728
|
+
] }),
|
|
729
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Button, { type: "submit", className: "w-full", disabled: loading || code.length !== 6, children: loading ? "Verifying..." : "Verify Code" })
|
|
730
|
+
] }),
|
|
731
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "mt-4 space-y-2 text-sm", children: [
|
|
732
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Button, { type: "button", variant: "outline", className: "w-full", disabled: resending, onClick: () => void resend(), children: resending ? "Sending..." : "Send a new code" }),
|
|
733
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { type: "button", className: "text-sm underline", onClick: () => void reportSuspicious(), children: "This was not me" }),
|
|
734
|
+
message ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { children: message }) : null,
|
|
735
|
+
appEnv && appEnv !== "production" ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { children: "Dev mode: use 999999 to bypass 2FA." }) : null
|
|
736
|
+
] })
|
|
737
|
+
] })
|
|
738
|
+
] });
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
// src/passkey-section.tsx
|
|
742
|
+
var import_lucide_react2 = require("lucide-react");
|
|
743
|
+
var React3 = __toESM(require("react"), 1);
|
|
744
|
+
var import_react_dom = require("react-dom");
|
|
745
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
746
|
+
function PasskeySection({ endpoints = {}, components, onSuccess, onError }) {
|
|
747
|
+
const { Button, Card, CardContent, CardDescription, CardHeader, CardTitle, Input, Label } = resolveAuthComponents(components);
|
|
748
|
+
const [passkeys, setPasskeys] = React3.useState([]);
|
|
749
|
+
const [loading, setLoading] = React3.useState(true);
|
|
750
|
+
const [registering, setRegistering] = React3.useState(false);
|
|
751
|
+
const [pendingName, setPendingName] = React3.useState("");
|
|
752
|
+
const listUrl = endpoints.passkeyList ?? "/api/passkeys";
|
|
753
|
+
const registerOptionsUrl = endpoints.passkeyRegisterOptions ?? "/api/passkeys/register/options";
|
|
754
|
+
const registerUrl = endpoints.passkeyRegister ?? "/api/passkeys/register";
|
|
755
|
+
const deleteUrl = endpoints.passkeyDelete ?? ((id) => `/api/passkeys/${id}`);
|
|
756
|
+
const fetchPasskeys = React3.useCallback(async () => {
|
|
757
|
+
try {
|
|
758
|
+
const res = await fetch(listUrl);
|
|
759
|
+
if (res.ok) {
|
|
760
|
+
setPasskeys(await res.json());
|
|
761
|
+
}
|
|
762
|
+
} catch {
|
|
763
|
+
onError?.("passkeys", "Failed to load passkeys");
|
|
764
|
+
} finally {
|
|
765
|
+
setLoading(false);
|
|
766
|
+
}
|
|
767
|
+
}, [listUrl, onError]);
|
|
768
|
+
React3.useEffect(() => {
|
|
769
|
+
void fetchPasskeys();
|
|
770
|
+
}, [fetchPasskeys]);
|
|
771
|
+
async function registerPasskey2() {
|
|
772
|
+
const name = pendingName || getDefaultPasskeyName();
|
|
773
|
+
(0, import_react_dom.flushSync)(() => setPendingName(name));
|
|
774
|
+
setRegistering(true);
|
|
775
|
+
try {
|
|
776
|
+
await registerPasskey({ endpoints: { ...endpoints, passkeyRegisterOptions: registerOptionsUrl, passkeyRegister: registerUrl }, name });
|
|
777
|
+
setPendingName("");
|
|
778
|
+
onSuccess?.("Passkey registered successfully.");
|
|
779
|
+
await fetchPasskeys();
|
|
780
|
+
} catch (caughtError) {
|
|
781
|
+
if (!isAbortError(caughtError)) {
|
|
782
|
+
onError?.("passkeys", caughtError instanceof Error ? caughtError.message : "Passkey registration failed");
|
|
783
|
+
}
|
|
784
|
+
} finally {
|
|
785
|
+
setRegistering(false);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
async function deletePasskey(id) {
|
|
789
|
+
try {
|
|
790
|
+
const res = await fetch(deleteUrl(id), {
|
|
791
|
+
method: "DELETE",
|
|
792
|
+
headers: { "X-CSRF-TOKEN": getCsrfToken(endpoints.csrfToken) }
|
|
793
|
+
});
|
|
794
|
+
if (!res.ok) {
|
|
795
|
+
throw new Error("Delete failed");
|
|
796
|
+
}
|
|
797
|
+
setPasskeys((current) => current.filter((passkey) => passkey.id !== id));
|
|
798
|
+
onSuccess?.("Passkey removed.");
|
|
799
|
+
} catch {
|
|
800
|
+
onError?.("passkeys", "Failed to delete passkey");
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
const isWebAuthnSupported = typeof window !== "undefined" && !!window.PublicKeyCredential;
|
|
804
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(Card, { children: [
|
|
805
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(CardHeader, { children: [
|
|
806
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(CardTitle, { className: "flex items-center gap-2", children: [
|
|
807
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_lucide_react2.Key, { className: "h-5 w-5" }),
|
|
808
|
+
"Passkeys"
|
|
809
|
+
] }),
|
|
810
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(CardDescription, { children: "Manage passkeys for passwordless login." })
|
|
811
|
+
] }),
|
|
812
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(CardContent, { className: "space-y-4", children: [
|
|
813
|
+
!isWebAuthnSupported ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "text-sm text-muted-foreground", children: "Your browser does not support passkeys." }) : null,
|
|
814
|
+
loading ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "text-sm text-muted-foreground", children: "Loading passkeys..." }) : passkeys.length === 0 ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "text-sm text-muted-foreground", children: "No passkeys registered yet." }) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "divide-y rounded-md border", children: passkeys.map((passkey) => /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "flex items-center justify-between gap-3 p-3", children: [
|
|
815
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { children: [
|
|
816
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "font-medium", children: passkey.name }),
|
|
817
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "text-sm text-muted-foreground", children: new Date(passkey.created_at).toLocaleDateString() })
|
|
818
|
+
] }),
|
|
819
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Button, { type: "button", variant: "ghost", size: "icon", "aria-label": "Delete passkey", onClick: () => void deletePasskey(passkey.id), children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_lucide_react2.Trash2, {}) })
|
|
820
|
+
] }, passkey.id)) }),
|
|
821
|
+
isWebAuthnSupported ? /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "flex flex-col gap-2 sm:flex-row", children: [
|
|
822
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "flex-1 space-y-1", children: [
|
|
823
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Label, { htmlFor: "passkey-name", children: "Passkey name" }),
|
|
824
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Input, { id: "passkey-name", value: pendingName, onChange: (event) => setPendingName(event.target.value), placeholder: getDefaultPasskeyName() })
|
|
825
|
+
] }),
|
|
826
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(Button, { type: "button", className: "self-end", variant: "outline", disabled: registering, onClick: () => void registerPasskey2(), children: [
|
|
827
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_lucide_react2.Plus, {}),
|
|
828
|
+
registering ? "Registering..." : "Add Passkey"
|
|
829
|
+
] })
|
|
830
|
+
] }) : null
|
|
831
|
+
] })
|
|
832
|
+
] });
|
|
833
|
+
}
|
|
834
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
835
|
+
0 && (module.exports = {
|
|
836
|
+
ChangePasswordForm,
|
|
837
|
+
LoginForm,
|
|
838
|
+
PasskeyLoginButton,
|
|
839
|
+
PasskeySection,
|
|
840
|
+
PasswordResetRequestForm,
|
|
841
|
+
ResetPasswordForm,
|
|
842
|
+
SignupForm,
|
|
843
|
+
TwoFactorForm,
|
|
844
|
+
arrayBufferToBase64url,
|
|
845
|
+
authenticateWithPasskey,
|
|
846
|
+
base64urlToArrayBuffer,
|
|
847
|
+
getCsrfToken,
|
|
848
|
+
getDefaultPasskeyName,
|
|
849
|
+
isAbortError,
|
|
850
|
+
isConditionalMediationAvailable,
|
|
851
|
+
registerPasskey
|
|
852
|
+
});
|