@useauthio/react 0.2.2 → 0.2.4

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/index.d.ts CHANGED
@@ -103,6 +103,14 @@ interface AuthioContextValue {
103
103
  user: AuthioUser | null;
104
104
  status: AuthioStatus;
105
105
  accessToken: string | null;
106
+ /** Same `apiUrl` passed to `<AuthioProvider>`. */
107
+ apiUrl: string;
108
+ /** Same `projectId` passed to `<AuthioProvider>`. */
109
+ projectId: string;
110
+ /** Resolved hosted sign-in URL (custom domain or Lobby default). */
111
+ signInUrl: string;
112
+ /** Custom fetch from provider props, if any. */
113
+ fetchImpl?: typeof fetch;
106
114
  /**
107
115
  * Returns the current access token, silently refreshing if it's
108
116
  * within 10 seconds of expiry. Returns `null` when no session
@@ -169,7 +177,7 @@ type AuthioTokenVerifier = (token: string) => Promise<AuthioTokenVerification |
169
177
  interface AuthioProviderProps {
170
178
  /** Authio auth-core base URL, e.g. `"https://auth-api.authio.com"`. */
171
179
  apiUrl: string;
172
- /** Project ID (`proj_…`). Sent as `X-Authio-Project` on every call. */
180
+ /** Project ID (`proj_…`). Dashboard environment ID; API field `project_id`. Sent as `X-Authio-Project` on every call. */
173
181
  projectId: string;
174
182
  /**
175
183
  * Where the access token lives. Defaults to `"memory"`. NEVER stores
@@ -437,6 +445,116 @@ interface SignInWithPasskeyResult {
437
445
  */
438
446
  declare function signInWithPasskey(opts: SignInWithPasskeyOptions): Promise<SignInWithPasskeyResult>;
439
447
 
448
+ interface AuthioPasskey {
449
+ id: string;
450
+ nickname: string | null;
451
+ aaguid: string | null;
452
+ authenticator_name: string;
453
+ transports: string[];
454
+ sign_count: number;
455
+ last_used_at: string | null;
456
+ created_at: string;
457
+ }
458
+ /** Shared options for authenticated `/v1/me/passkeys` calls. */
459
+ interface PasskeyApiOptions {
460
+ apiUrl: string;
461
+ projectId: string;
462
+ accessToken: string;
463
+ signal?: AbortSignal;
464
+ fetchImpl?: typeof fetch;
465
+ }
466
+ interface AddPasskeyOptions extends PasskeyApiOptions {
467
+ email: string;
468
+ /**
469
+ * WebAuthn ceremony origin (sent as `X-Authio-Origin`). Defaults to
470
+ * `window.location.origin` in the browser.
471
+ */
472
+ origin?: string;
473
+ }
474
+ interface EnrollPasskeyOptions extends PasskeyApiOptions {
475
+ email: string;
476
+ /** Hosted sign-in UI base URL (lobby / custom domain). */
477
+ signInUrl: string;
478
+ /** Where to return after enrollment (`redirect_uri` query param). */
479
+ returnUrl?: string;
480
+ /** Post-login path on the return host (`next` query param). */
481
+ next?: string;
482
+ }
483
+ interface BuildEnrollPasskeyUrlOptions {
484
+ signInUrl: string;
485
+ projectId: string;
486
+ email: string;
487
+ registerToken: string;
488
+ redirectUri?: string;
489
+ next?: string;
490
+ }
491
+ /** Normalize a hosted-UI base URL (no trailing slash). */
492
+ declare function signInOrigin(signInUrl: string): string;
493
+ /**
494
+ * True when inline WebAuthn registration can run on the current page
495
+ * (same origin as the hosted sign-in UI / custom auth domain).
496
+ */
497
+ declare function canEnrollPasskeyEmbedded(signInUrl: string): boolean;
498
+ /** Build the hosted-UI URL for `mode=add_credential` passkey enrollment. */
499
+ declare function buildEnrollPasskeyUrl(opts: BuildEnrollPasskeyUrlOptions): string;
500
+ /** Mint a short-lived JWT for add-credential WebAuthn (`register-intent`). */
501
+ declare function mintPasskeyRegisterIntent(opts: PasskeyApiOptions): Promise<{
502
+ token: string;
503
+ expiresIn: number;
504
+ }>;
505
+ /**
506
+ * Enroll a passkey by redirecting to the hosted sign-in UI on the
507
+ * sign-in origin (`mode=add_credential`). Use when the SPA origin
508
+ * differs from the WebAuthn RP ID.
509
+ */
510
+ declare function enrollPasskey(opts: EnrollPasskeyOptions): Promise<void>;
511
+ /**
512
+ * Enroll a passkey inline on the current origin via WebAuthn.
513
+ *
514
+ * 1. `POST /v1/me/passkeys/register-intent`
515
+ * 2. `POST /v1/auth/passkey/register/options` + ceremony
516
+ * 3. `POST /v1/auth/passkey/register/verify`
517
+ */
518
+ declare function addPasskey(opts: AddPasskeyOptions): Promise<void>;
519
+ /** List passkeys for the signed-in user (`GET /v1/me/passkeys`). */
520
+ declare function listPasskeys(opts: PasskeyApiOptions): Promise<AuthioPasskey[]>;
521
+ /** Rename a passkey (`PATCH /v1/me/passkeys/{credentialId}`). */
522
+ declare function renamePasskey(opts: PasskeyApiOptions, credentialId: string, name: string): Promise<void>;
523
+ /** Revoke a passkey (`DELETE /v1/me/passkeys/{credentialId}`). */
524
+ declare function revokePasskey(opts: PasskeyApiOptions, credentialId: string): Promise<void>;
525
+
526
+ interface UsePasskeysResult {
527
+ passkeys: AuthioPasskey[];
528
+ loading: boolean;
529
+ error: string | null;
530
+ refresh: () => Promise<void>;
531
+ rename: (credentialId: string, nickname: string) => Promise<void>;
532
+ revoke: (credentialId: string) => Promise<void>;
533
+ enroll: (opts?: {
534
+ returnUrl?: string;
535
+ next?: string;
536
+ }) => Promise<void>;
537
+ }
538
+ /**
539
+ * Load and mutate the signed-in user's passkeys via `/v1/me/passkeys`.
540
+ * Requires `<AuthioProvider>` with a live session.
541
+ */
542
+ declare function usePasskeys(): UsePasskeysResult;
543
+
544
+ interface PasskeyManagerProps {
545
+ className?: string;
546
+ /** Passed to {@link enrollPasskey} as `returnUrl`. */
547
+ enrollReturnUrl?: string;
548
+ enrollNext?: string;
549
+ }
550
+ /**
551
+ * Drop-in passkey device list for customer apps. Lists, renames, and
552
+ * revokes via `/v1/me/passkeys`; "Add passkey" redirects to the hosted
553
+ * sign-in UI (`mode=add_credential`) so the WebAuthn ceremony runs on
554
+ * the sign-in origin.
555
+ */
556
+ declare function PasskeyManager({ className, enrollReturnUrl, enrollNext, }: PasskeyManagerProps): React.ReactElement;
557
+
440
558
  /**
441
559
  * Browser helpers for Authio Locate client attestation.
442
560
  *
@@ -468,4 +586,4 @@ declare function captureClientLocation(opts?: CaptureClientLocationOptions): Pro
468
586
  */
469
587
  declare function wrapFetchError(err: unknown, fallbackCode?: string): AuthioError;
470
588
 
471
- export { AuthioContext, type AuthioContextValue, AuthioProvider, type AuthioProviderProps, type AuthioStatus, type AuthioStorageMode, type AuthioTelemetryEvent, type AuthioTokenVerification, type AuthioTokenVerifier, type AuthioUser, type CaptureClientLocationOptions, type ClientLocationCapture, RedirectToSignIn, type RedirectToSignInProps, type SignInWithMagicLinkOptions, type SignInWithPasskeyOptions, type SignInWithPasskeyResult, SignedIn, type SignedInProps, SignedOut, type SignedOutProps, type UseAuthioRequiredOptions, type UseAuthioRequiredResult, captureClientLocation, signInWithMagicLink, signInWithPasskey, useAuthio, useAuthioRequired, wrapFetchError };
589
+ export { type AddPasskeyOptions, AuthioContext, type AuthioContextValue, type AuthioPasskey, AuthioProvider, type AuthioProviderProps, type AuthioStatus, type AuthioStorageMode, type AuthioTelemetryEvent, type AuthioTokenVerification, type AuthioTokenVerifier, type AuthioUser, type CaptureClientLocationOptions, type ClientLocationCapture, type EnrollPasskeyOptions, type PasskeyApiOptions, PasskeyManager, type PasskeyManagerProps, RedirectToSignIn, type RedirectToSignInProps, type SignInWithMagicLinkOptions, type SignInWithPasskeyOptions, type SignInWithPasskeyResult, SignedIn, type SignedInProps, SignedOut, type SignedOutProps, type UseAuthioRequiredOptions, type UseAuthioRequiredResult, type UsePasskeysResult, addPasskey, buildEnrollPasskeyUrl, canEnrollPasskeyEmbedded, captureClientLocation, enrollPasskey, listPasskeys, mintPasskeyRegisterIntent, renamePasskey, revokePasskey, signInOrigin, signInWithMagicLink, signInWithPasskey, useAuthio, useAuthioRequired, usePasskeys, wrapFetchError };
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { createContext, useRef, useEffect, useState, useCallback, useMemo, useContext } from 'react';
2
2
  import { AuthioError, JwtVerifier } from '@useauthio/node';
3
3
  export { AuthioError } from '@useauthio/node';
4
- import { jsx, Fragment } from 'react/jsx-runtime';
4
+ import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
5
+ import { startRegistration } from '@simplewebauthn/browser';
5
6
 
6
7
  // src/provider.tsx
7
8
  function wrapFetchError(err, fallbackCode = "network_error") {
@@ -286,6 +287,49 @@ function createTokenStorage(mode = "memory") {
286
287
  return new MemoryStorage();
287
288
  }
288
289
 
290
+ // src/lobby-context.ts
291
+ async function mintLobbySignInUrl(opts) {
292
+ const target = new URL(opts.hostedUiUrl.replace(/\/$/, "") + "/");
293
+ if (opts.projectId) {
294
+ try {
295
+ const fetchImpl = opts.fetchImpl ?? globalThis.fetch.bind(globalThis);
296
+ const res = await fetchImpl(
297
+ `${opts.apiUrl.replace(/\/$/, "")}/v1/auth/lobby-context`,
298
+ {
299
+ method: "POST",
300
+ headers: {
301
+ Accept: "application/json",
302
+ "Content-Type": "application/json",
303
+ "X-Authio-Project": opts.projectId,
304
+ "X-Authio-SDK": SDK_USER_AGENT
305
+ },
306
+ body: JSON.stringify({
307
+ project_id: opts.projectId,
308
+ redirect_uri: opts.redirectUri,
309
+ ...opts.next ? { next: opts.next } : {}
310
+ })
311
+ }
312
+ );
313
+ if (res.ok) {
314
+ const body = await res.json();
315
+ if (typeof body.ctx === "string" && body.ctx.trim()) {
316
+ target.searchParams.set("ctx", body.ctx.trim());
317
+ return target.toString();
318
+ }
319
+ }
320
+ } catch {
321
+ }
322
+ }
323
+ if (opts.projectId) {
324
+ target.searchParams.set("project_id", opts.projectId);
325
+ }
326
+ target.searchParams.set("redirect_uri", opts.redirectUri);
327
+ if (opts.next) {
328
+ target.searchParams.set("next", opts.next);
329
+ }
330
+ return target.toString();
331
+ }
332
+
289
333
  // src/telemetry.ts
290
334
  var noopEmitter = () => {
291
335
  };
@@ -523,8 +567,8 @@ function AuthioProvider(props) {
523
567
  const ok = await performRefresh();
524
568
  return ok ? storageRef.current.get() : null;
525
569
  }, [accessToken, performRefresh]);
526
- const signIn = useCallback(
527
- (opts = {}) => {
570
+ const signIn = useCallback((opts = {}) => {
571
+ void (async () => {
528
572
  const p = propsRef.current;
529
573
  p.emit({
530
574
  kind: "sign_in_started",
@@ -537,19 +581,16 @@ function AuthioProvider(props) {
537
581
  '[@useauthio/react] AuthioProvider projectId is missing \u2014 Lobby cannot resolve your project. Set projectId="proj_\u2026" on <AuthioProvider>.'
538
582
  );
539
583
  }
540
- const base = p.signInUrl ?? DEFAULT_SIGN_IN_URL;
541
- const url = new URL(base);
542
- if (p.projectId) {
543
- url.searchParams.set("project_id", p.projectId);
544
- }
545
- url.searchParams.set(
546
- "redirect_uri",
547
- opts.returnTo ?? window.location.href
548
- );
549
- window.location.assign(url.toString());
550
- },
551
- []
552
- );
584
+ const url = await mintLobbySignInUrl({
585
+ apiUrl: p.apiUrl,
586
+ projectId: p.projectId,
587
+ hostedUiUrl: p.signInUrl ?? DEFAULT_SIGN_IN_URL,
588
+ redirectUri: opts.returnTo ?? window.location.href,
589
+ fetchImpl: p.fetchImpl
590
+ });
591
+ window.location.assign(url);
592
+ })();
593
+ }, []);
553
594
  const signOut = useCallback(async () => {
554
595
  const p = propsRef.current;
555
596
  const token = storageRef.current.get();
@@ -622,11 +663,16 @@ function AuthioProvider(props) {
622
663
  },
623
664
  []
624
665
  );
666
+ const resolvedSignInUrl = signInUrl ?? DEFAULT_SIGN_IN_URL;
625
667
  const value = useMemo(
626
668
  () => ({
627
669
  user,
628
670
  status,
629
671
  accessToken,
672
+ apiUrl,
673
+ projectId,
674
+ signInUrl: resolvedSignInUrl,
675
+ fetchImpl,
630
676
  getAccessToken,
631
677
  signIn,
632
678
  signOut,
@@ -637,6 +683,10 @@ function AuthioProvider(props) {
637
683
  user,
638
684
  status,
639
685
  accessToken,
686
+ apiUrl,
687
+ projectId,
688
+ resolvedSignInUrl,
689
+ fetchImpl,
640
690
  getAccessToken,
641
691
  signIn,
642
692
  signOut,
@@ -830,6 +880,364 @@ function bufferToBase64Url(buf) {
830
880
  }
831
881
  return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
832
882
  }
883
+ function signInOrigin(signInUrl) {
884
+ return signInUrl.replace(/\/$/, "");
885
+ }
886
+ function canEnrollPasskeyEmbedded(signInUrl) {
887
+ if (!isBrowser()) return false;
888
+ return window.location.origin === signInOrigin(signInUrl);
889
+ }
890
+ function buildEnrollPasskeyUrl(opts) {
891
+ const url = new URL(signInOrigin(opts.signInUrl));
892
+ url.searchParams.set("mode", "add_credential");
893
+ url.searchParams.set("project_id", opts.projectId);
894
+ url.searchParams.set("email", opts.email);
895
+ url.searchParams.set("token", opts.registerToken);
896
+ if (opts.redirectUri) {
897
+ url.searchParams.set("redirect_uri", opts.redirectUri);
898
+ }
899
+ if (opts.next) {
900
+ url.searchParams.set("next", opts.next);
901
+ }
902
+ return url.toString();
903
+ }
904
+ function bearerHeaders(accessToken, origin) {
905
+ const h = {
906
+ Authorization: `Bearer ${accessToken}`
907
+ };
908
+ if (origin) h["X-Authio-Origin"] = origin;
909
+ return h;
910
+ }
911
+ async function mintPasskeyRegisterIntent(opts) {
912
+ const body = await authioFetch({
913
+ apiUrl: opts.apiUrl,
914
+ projectId: opts.projectId,
915
+ path: "/v1/me/passkeys/register-intent",
916
+ method: "POST",
917
+ body: {},
918
+ signal: opts.signal,
919
+ fetchImpl: opts.fetchImpl,
920
+ extraHeaders: bearerHeaders(opts.accessToken)
921
+ });
922
+ return { token: body.token, expiresIn: body.expires_in };
923
+ }
924
+ async function enrollPasskey(opts) {
925
+ if (!isBrowser()) {
926
+ throw new AuthioError({
927
+ code: "not_in_browser",
928
+ message: "enrollPasskey can only be invoked in a browser environment.",
929
+ status: 0
930
+ });
931
+ }
932
+ const intent = await mintPasskeyRegisterIntent(opts);
933
+ const redirectUri = opts.returnUrl ?? (typeof window !== "undefined" ? window.location.href : "");
934
+ const target = buildEnrollPasskeyUrl({
935
+ signInUrl: opts.signInUrl,
936
+ projectId: opts.projectId,
937
+ email: opts.email,
938
+ registerToken: intent.token,
939
+ redirectUri,
940
+ next: opts.next
941
+ });
942
+ window.location.assign(target);
943
+ }
944
+ async function addPasskey(opts) {
945
+ if (!isBrowser()) {
946
+ throw new AuthioError({
947
+ code: "not_in_browser",
948
+ message: "addPasskey can only be invoked in a browser environment.",
949
+ status: 0
950
+ });
951
+ }
952
+ const origin = opts.origin ?? window.location.origin;
953
+ const intent = await mintPasskeyRegisterIntent(opts);
954
+ const options = await authioFetch({
955
+ apiUrl: opts.apiUrl,
956
+ projectId: opts.projectId,
957
+ path: "/v1/auth/passkey/register/options",
958
+ method: "POST",
959
+ body: { email: opts.email },
960
+ signal: opts.signal,
961
+ fetchImpl: opts.fetchImpl,
962
+ extraHeaders: bearerHeaders(intent.token, origin)
963
+ });
964
+ let attestation;
965
+ try {
966
+ attestation = await startRegistration({ optionsJSON: options });
967
+ } catch (err) {
968
+ throw new AuthioError({
969
+ code: "webauthn_cancelled",
970
+ message: err instanceof Error ? err.message : "Passkey registration cancelled by user.",
971
+ status: 0
972
+ });
973
+ }
974
+ await authioFetch({
975
+ apiUrl: opts.apiUrl,
976
+ projectId: opts.projectId,
977
+ path: "/v1/auth/passkey/register/verify",
978
+ method: "POST",
979
+ body: { email: opts.email, credential: attestation },
980
+ signal: opts.signal,
981
+ fetchImpl: opts.fetchImpl,
982
+ extraHeaders: bearerHeaders(intent.token, origin)
983
+ });
984
+ }
985
+ async function listPasskeys(opts) {
986
+ const res = await authioFetch({
987
+ apiUrl: opts.apiUrl,
988
+ projectId: opts.projectId,
989
+ path: "/v1/me/passkeys",
990
+ signal: opts.signal,
991
+ fetchImpl: opts.fetchImpl,
992
+ extraHeaders: bearerHeaders(opts.accessToken)
993
+ });
994
+ return res.data ?? [];
995
+ }
996
+ async function renamePasskey(opts, credentialId, name) {
997
+ await authioFetch({
998
+ apiUrl: opts.apiUrl,
999
+ projectId: opts.projectId,
1000
+ path: `/v1/me/passkeys/${encodeURIComponent(credentialId)}`,
1001
+ method: "PATCH",
1002
+ body: { nickname: name },
1003
+ signal: opts.signal,
1004
+ fetchImpl: opts.fetchImpl,
1005
+ extraHeaders: bearerHeaders(opts.accessToken)
1006
+ });
1007
+ }
1008
+ async function revokePasskey(opts, credentialId) {
1009
+ await authioFetch({
1010
+ apiUrl: opts.apiUrl,
1011
+ projectId: opts.projectId,
1012
+ path: `/v1/me/passkeys/${encodeURIComponent(credentialId)}`,
1013
+ method: "DELETE",
1014
+ signal: opts.signal,
1015
+ fetchImpl: opts.fetchImpl,
1016
+ extraHeaders: bearerHeaders(opts.accessToken)
1017
+ });
1018
+ }
1019
+ function usePasskeys() {
1020
+ const ctx = useAuthio();
1021
+ const [passkeys, setPasskeys] = useState([]);
1022
+ const [loading, setLoading] = useState(true);
1023
+ const [error, setError] = useState(null);
1024
+ const refresh = useCallback(async () => {
1025
+ if (ctx.status !== "authenticated") {
1026
+ setPasskeys([]);
1027
+ setLoading(false);
1028
+ return;
1029
+ }
1030
+ setLoading(true);
1031
+ setError(null);
1032
+ try {
1033
+ const token = await ctx.getAccessToken();
1034
+ if (!token) {
1035
+ setPasskeys([]);
1036
+ return;
1037
+ }
1038
+ const rows = await listPasskeys({
1039
+ apiUrl: ctx.apiUrl,
1040
+ projectId: ctx.projectId,
1041
+ accessToken: token,
1042
+ fetchImpl: ctx.fetchImpl
1043
+ });
1044
+ setPasskeys(rows);
1045
+ } catch (err) {
1046
+ setError(err instanceof Error ? err.message : "Failed to load passkeys");
1047
+ setPasskeys([]);
1048
+ } finally {
1049
+ setLoading(false);
1050
+ }
1051
+ }, [
1052
+ ctx.status,
1053
+ ctx.getAccessToken,
1054
+ ctx.apiUrl,
1055
+ ctx.projectId,
1056
+ ctx.fetchImpl
1057
+ ]);
1058
+ useEffect(() => {
1059
+ void refresh();
1060
+ }, [refresh, ctx.status]);
1061
+ const rename = useCallback(
1062
+ async (credentialId, nickname) => {
1063
+ const token = await ctx.getAccessToken();
1064
+ if (!token) throw new Error("Not signed in");
1065
+ await renamePasskey(
1066
+ {
1067
+ apiUrl: ctx.apiUrl,
1068
+ projectId: ctx.projectId,
1069
+ accessToken: token,
1070
+ fetchImpl: ctx.fetchImpl
1071
+ },
1072
+ credentialId,
1073
+ nickname
1074
+ );
1075
+ await refresh();
1076
+ },
1077
+ [ctx.getAccessToken, ctx.apiUrl, ctx.projectId, ctx.fetchImpl, refresh]
1078
+ );
1079
+ const revoke = useCallback(
1080
+ async (credentialId) => {
1081
+ const token = await ctx.getAccessToken();
1082
+ if (!token) throw new Error("Not signed in");
1083
+ await revokePasskey(
1084
+ {
1085
+ apiUrl: ctx.apiUrl,
1086
+ projectId: ctx.projectId,
1087
+ accessToken: token,
1088
+ fetchImpl: ctx.fetchImpl
1089
+ },
1090
+ credentialId
1091
+ );
1092
+ await refresh();
1093
+ },
1094
+ [ctx.getAccessToken, ctx.apiUrl, ctx.projectId, ctx.fetchImpl, refresh]
1095
+ );
1096
+ const enroll = useCallback(
1097
+ async (opts) => {
1098
+ const token = await ctx.getAccessToken();
1099
+ const email = ctx.user?.email;
1100
+ if (!token || !email) throw new Error("Not signed in");
1101
+ await enrollPasskey({
1102
+ apiUrl: ctx.apiUrl,
1103
+ projectId: ctx.projectId,
1104
+ accessToken: token,
1105
+ fetchImpl: ctx.fetchImpl,
1106
+ email,
1107
+ signInUrl: ctx.signInUrl,
1108
+ returnUrl: opts?.returnUrl,
1109
+ next: opts?.next
1110
+ });
1111
+ },
1112
+ [
1113
+ ctx.getAccessToken,
1114
+ ctx.user?.email,
1115
+ ctx.apiUrl,
1116
+ ctx.projectId,
1117
+ ctx.fetchImpl,
1118
+ ctx.signInUrl
1119
+ ]
1120
+ );
1121
+ return { passkeys, loading, error, refresh, rename, revoke, enroll };
1122
+ }
1123
+ function PasskeyManager({
1124
+ className,
1125
+ enrollReturnUrl,
1126
+ enrollNext
1127
+ }) {
1128
+ const { passkeys, loading, error, rename, revoke, enroll } = usePasskeys();
1129
+ const [busyId, setBusyId] = useState(null);
1130
+ const [actionError, setActionError] = useState(null);
1131
+ const [enrolling, setEnrolling] = useState(false);
1132
+ async function onRename(id, nickname) {
1133
+ setBusyId(id);
1134
+ setActionError(null);
1135
+ try {
1136
+ await rename(id, nickname);
1137
+ } catch (err) {
1138
+ setActionError(err instanceof Error ? err.message : "Rename failed");
1139
+ } finally {
1140
+ setBusyId(null);
1141
+ }
1142
+ }
1143
+ async function onRevoke(id) {
1144
+ setBusyId(id);
1145
+ setActionError(null);
1146
+ try {
1147
+ await revoke(id);
1148
+ } catch (err) {
1149
+ setActionError(err instanceof Error ? err.message : "Revoke failed");
1150
+ } finally {
1151
+ setBusyId(null);
1152
+ }
1153
+ }
1154
+ async function onEnroll() {
1155
+ setEnrolling(true);
1156
+ setActionError(null);
1157
+ try {
1158
+ await enroll({ returnUrl: enrollReturnUrl, next: enrollNext });
1159
+ } catch (err) {
1160
+ setEnrolling(false);
1161
+ setActionError(err instanceof Error ? err.message : "Could not start enrollment");
1162
+ }
1163
+ }
1164
+ return /* @__PURE__ */ jsxs("section", { className, "aria-labelledby": "authio-passkey-manager-title", children: [
1165
+ /* @__PURE__ */ jsxs("div", { className: "authio-passkey-manager__header", children: [
1166
+ /* @__PURE__ */ jsx("h2", { id: "authio-passkey-manager-title", children: "Passkeys" }),
1167
+ /* @__PURE__ */ jsx(
1168
+ "button",
1169
+ {
1170
+ type: "button",
1171
+ onClick: () => void onEnroll(),
1172
+ disabled: enrolling || loading,
1173
+ children: enrolling ? "Redirecting\u2026" : "Add passkey"
1174
+ }
1175
+ )
1176
+ ] }),
1177
+ (error || actionError) && /* @__PURE__ */ jsx("p", { role: "alert", className: "authio-passkey-manager__error", children: error ?? actionError }),
1178
+ loading ? /* @__PURE__ */ jsx("p", { className: "authio-passkey-manager__loading", children: "Loading passkeys\u2026" }) : passkeys.length === 0 ? /* @__PURE__ */ jsx("p", { className: "authio-passkey-manager__empty", children: "No passkeys yet. Add one to sign in faster on this account." }) : /* @__PURE__ */ jsx("ul", { className: "authio-passkey-manager__list", children: passkeys.map((p) => /* @__PURE__ */ jsx("li", { className: "authio-passkey-manager__item", children: /* @__PURE__ */ jsx(
1179
+ PasskeyRow,
1180
+ {
1181
+ passkey: p,
1182
+ busy: busyId === p.id,
1183
+ onRename,
1184
+ onRevoke
1185
+ }
1186
+ ) }, p.id)) })
1187
+ ] });
1188
+ }
1189
+ function PasskeyRow({
1190
+ passkey,
1191
+ busy,
1192
+ onRename,
1193
+ onRevoke
1194
+ }) {
1195
+ const [nickname, setNickname] = useState(passkey.nickname ?? "");
1196
+ return /* @__PURE__ */ jsxs("div", { className: "authio-passkey-manager__row", children: [
1197
+ /* @__PURE__ */ jsxs("div", { children: [
1198
+ /* @__PURE__ */ jsx("strong", { children: passkey.authenticator_name }),
1199
+ /* @__PURE__ */ jsxs("div", { className: "authio-passkey-manager__meta", children: [
1200
+ "Added ",
1201
+ new Date(passkey.created_at).toLocaleDateString(),
1202
+ passkey.last_used_at ? ` \xB7 Last used ${new Date(passkey.last_used_at).toLocaleDateString()}` : ""
1203
+ ] })
1204
+ ] }),
1205
+ /* @__PURE__ */ jsxs(
1206
+ "form",
1207
+ {
1208
+ className: "authio-passkey-manager__rename",
1209
+ onSubmit: (e) => {
1210
+ e.preventDefault();
1211
+ void onRename(passkey.id, nickname.trim());
1212
+ },
1213
+ children: [
1214
+ /* @__PURE__ */ jsx(
1215
+ "input",
1216
+ {
1217
+ type: "text",
1218
+ value: nickname,
1219
+ onChange: (e) => setNickname(e.target.value),
1220
+ placeholder: "Nickname",
1221
+ maxLength: 64,
1222
+ disabled: busy
1223
+ }
1224
+ ),
1225
+ /* @__PURE__ */ jsx("button", { type: "submit", disabled: busy, children: "Save" })
1226
+ ]
1227
+ }
1228
+ ),
1229
+ /* @__PURE__ */ jsx(
1230
+ "button",
1231
+ {
1232
+ type: "button",
1233
+ disabled: busy,
1234
+ onClick: () => void onRevoke(passkey.id),
1235
+ className: "authio-passkey-manager__revoke",
1236
+ children: "Revoke"
1237
+ }
1238
+ )
1239
+ ] });
1240
+ }
833
1241
 
834
1242
  // src/locate.ts
835
1243
  function captureClientLocation(opts = {}) {
@@ -863,6 +1271,6 @@ function captureClientLocation(opts = {}) {
863
1271
  });
864
1272
  }
865
1273
 
866
- export { AuthioContext, AuthioProvider, RedirectToSignIn, SignedIn, SignedOut, captureClientLocation, signInWithMagicLink, signInWithPasskey, useAuthio, useAuthioRequired, wrapFetchError };
1274
+ export { AuthioContext, AuthioProvider, PasskeyManager, RedirectToSignIn, SignedIn, SignedOut, addPasskey, buildEnrollPasskeyUrl, canEnrollPasskeyEmbedded, captureClientLocation, enrollPasskey, listPasskeys, mintPasskeyRegisterIntent, renamePasskey, revokePasskey, signInOrigin, signInWithMagicLink, signInWithPasskey, useAuthio, useAuthioRequired, usePasskeys, wrapFetchError };
867
1275
  //# sourceMappingURL=index.js.map
868
1276
  //# sourceMappingURL=index.js.map