@orangecheck/auth-client 2.11.0 → 2.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,5 +1,11 @@
1
1
  import * as React from 'react';
2
2
 
3
+ declare const DISPLAY_IDENTITY_KINDS: readonly ["did", "btc", "email", "nostr"];
4
+ type DisplayIdentityKind = (typeof DISPLAY_IDENTITY_KINDS)[number];
5
+ interface DisplayIdentity {
6
+ kind: DisplayIdentityKind;
7
+ value: string;
8
+ }
3
9
  interface OcAccount {
4
10
  accountId: string;
5
11
  didOc: string;
@@ -10,6 +16,7 @@ interface OcAccount {
10
16
  homeFederation?: string | null;
11
17
  signingMethod?: 'fedimint_threshold' | 'fedimint_client' | 'bip322' | null;
12
18
  isOwner?: boolean;
19
+ displayIdentity: DisplayIdentity;
13
20
  }
14
21
  type OcSessionStatus = 'loading' | 'authenticated' | 'anonymous' | 'error';
15
22
  interface OcSessionState {
@@ -18,6 +25,7 @@ interface OcSessionState {
18
25
  error: Error | null;
19
26
  refresh: () => Promise<void>;
20
27
  signOut: () => Promise<void>;
28
+ setDisplayIdentity: (kind: DisplayIdentityKind) => Promise<void>;
21
29
  signInUrl: string;
22
30
  }
23
31
  interface OcAuthConfig {
@@ -119,6 +127,9 @@ interface OcLinkedIdentity {
119
127
  verification_method: 'email-otp' | 'bip322' | null;
120
128
  is_primary: boolean;
121
129
  }
130
+ declare function fetchOcLinkedIdentities(opts?: {
131
+ authOrigin?: string;
132
+ }): Promise<OcLinkedIdentity[]>;
122
133
  interface OcLinkedIdentitiesProps {
123
134
  authOrigin?: string;
124
135
  className?: string;
@@ -208,4 +219,4 @@ declare function handleSudoRequired(body: {
208
219
  returnTo?: string;
209
220
  }): boolean;
210
221
 
211
- export { DEFAULT_CONFIG, type OcAccount, OcAccountChip, type OcAccountChipProps, OcAccountPill, type OcAccountPillProps, OcAddressInput, type OcAddressInputProps, type OcAuthConfig, OcLinkedIdentities, type OcLinkedIdentitiesProps, type OcLinkedIdentity, OcSessionProvider, type OcSessionState, type OcSessionStatus, OcSignIn, OcSignInButton, type OcSignInButtonProps, type OcSignInProps, type RedirectToSudoArgs, type UseOcAddressSuggestionOptions, type UseOcAddressSuggestionReturn, type UseStepUpAuthReturn, type UseWebAuthnListReturn, type UseWebAuthnRegisterReturn, type WebAuthnAssertionStatus, type WebAuthnCredentialPublic, type WebAuthnListStatus, type WebAuthnRegisterResult, type WebAuthnRegisterStatus, type WebAuthnRemoveResult, type WebAuthnRenameResult, type WebAuthnStepUpResult, buildSignInUrl, handleSudoRequired, redirectToSudo, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister };
222
+ export { DEFAULT_CONFIG, DISPLAY_IDENTITY_KINDS, type DisplayIdentity, type DisplayIdentityKind, type OcAccount, OcAccountChip, type OcAccountChipProps, OcAccountPill, type OcAccountPillProps, OcAddressInput, type OcAddressInputProps, type OcAuthConfig, OcLinkedIdentities, type OcLinkedIdentitiesProps, type OcLinkedIdentity, OcSessionProvider, type OcSessionState, type OcSessionStatus, OcSignIn, OcSignInButton, type OcSignInButtonProps, type OcSignInProps, type RedirectToSudoArgs, type UseOcAddressSuggestionOptions, type UseOcAddressSuggestionReturn, type UseStepUpAuthReturn, type UseWebAuthnListReturn, type UseWebAuthnRegisterReturn, type WebAuthnAssertionStatus, type WebAuthnCredentialPublic, type WebAuthnListStatus, type WebAuthnRegisterResult, type WebAuthnRegisterStatus, type WebAuthnRemoveResult, type WebAuthnRenameResult, type WebAuthnStepUpResult, buildSignInUrl, fetchOcLinkedIdentities, handleSudoRequired, redirectToSudo, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import * as React from 'react';
2
2
 
3
+ declare const DISPLAY_IDENTITY_KINDS: readonly ["did", "btc", "email", "nostr"];
4
+ type DisplayIdentityKind = (typeof DISPLAY_IDENTITY_KINDS)[number];
5
+ interface DisplayIdentity {
6
+ kind: DisplayIdentityKind;
7
+ value: string;
8
+ }
3
9
  interface OcAccount {
4
10
  accountId: string;
5
11
  didOc: string;
@@ -10,6 +16,7 @@ interface OcAccount {
10
16
  homeFederation?: string | null;
11
17
  signingMethod?: 'fedimint_threshold' | 'fedimint_client' | 'bip322' | null;
12
18
  isOwner?: boolean;
19
+ displayIdentity: DisplayIdentity;
13
20
  }
14
21
  type OcSessionStatus = 'loading' | 'authenticated' | 'anonymous' | 'error';
15
22
  interface OcSessionState {
@@ -18,6 +25,7 @@ interface OcSessionState {
18
25
  error: Error | null;
19
26
  refresh: () => Promise<void>;
20
27
  signOut: () => Promise<void>;
28
+ setDisplayIdentity: (kind: DisplayIdentityKind) => Promise<void>;
21
29
  signInUrl: string;
22
30
  }
23
31
  interface OcAuthConfig {
@@ -119,6 +127,9 @@ interface OcLinkedIdentity {
119
127
  verification_method: 'email-otp' | 'bip322' | null;
120
128
  is_primary: boolean;
121
129
  }
130
+ declare function fetchOcLinkedIdentities(opts?: {
131
+ authOrigin?: string;
132
+ }): Promise<OcLinkedIdentity[]>;
122
133
  interface OcLinkedIdentitiesProps {
123
134
  authOrigin?: string;
124
135
  className?: string;
@@ -208,4 +219,4 @@ declare function handleSudoRequired(body: {
208
219
  returnTo?: string;
209
220
  }): boolean;
210
221
 
211
- export { DEFAULT_CONFIG, type OcAccount, OcAccountChip, type OcAccountChipProps, OcAccountPill, type OcAccountPillProps, OcAddressInput, type OcAddressInputProps, type OcAuthConfig, OcLinkedIdentities, type OcLinkedIdentitiesProps, type OcLinkedIdentity, OcSessionProvider, type OcSessionState, type OcSessionStatus, OcSignIn, OcSignInButton, type OcSignInButtonProps, type OcSignInProps, type RedirectToSudoArgs, type UseOcAddressSuggestionOptions, type UseOcAddressSuggestionReturn, type UseStepUpAuthReturn, type UseWebAuthnListReturn, type UseWebAuthnRegisterReturn, type WebAuthnAssertionStatus, type WebAuthnCredentialPublic, type WebAuthnListStatus, type WebAuthnRegisterResult, type WebAuthnRegisterStatus, type WebAuthnRemoveResult, type WebAuthnRenameResult, type WebAuthnStepUpResult, buildSignInUrl, handleSudoRequired, redirectToSudo, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister };
222
+ export { DEFAULT_CONFIG, DISPLAY_IDENTITY_KINDS, type DisplayIdentity, type DisplayIdentityKind, type OcAccount, OcAccountChip, type OcAccountChipProps, OcAccountPill, type OcAccountPillProps, OcAddressInput, type OcAddressInputProps, type OcAuthConfig, OcLinkedIdentities, type OcLinkedIdentitiesProps, type OcLinkedIdentity, OcSessionProvider, type OcSessionState, type OcSessionStatus, OcSignIn, OcSignInButton, type OcSignInButtonProps, type OcSignInProps, type RedirectToSudoArgs, type UseOcAddressSuggestionOptions, type UseOcAddressSuggestionReturn, type UseStepUpAuthReturn, type UseWebAuthnListReturn, type UseWebAuthnRegisterReturn, type WebAuthnAssertionStatus, type WebAuthnCredentialPublic, type WebAuthnListStatus, type WebAuthnRegisterResult, type WebAuthnRegisterStatus, type WebAuthnRemoveResult, type WebAuthnRenameResult, type WebAuthnStepUpResult, buildSignInUrl, fetchOcLinkedIdentities, handleSudoRequired, redirectToSudo, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister };
package/dist/index.js CHANGED
@@ -27,6 +27,7 @@ var React3__namespace = /*#__PURE__*/_interopNamespace(React3);
27
27
  // src/provider.tsx
28
28
 
29
29
  // src/types.ts
30
+ var DISPLAY_IDENTITY_KINDS = ["did", "btc", "email", "nostr"];
30
31
  var DEFAULT_CONFIG = {
31
32
  authOrigin: "https://ochk.io",
32
33
  signInPath: "/signin",
@@ -44,6 +45,13 @@ function buildSignInUrl(cfg, returnTo) {
44
45
  return u.toString();
45
46
  }
46
47
  var SessionContext = React3__namespace.createContext(null);
48
+ function normalizeDisplayIdentity(raw, didOc) {
49
+ const di = raw.display_identity ?? raw.displayIdentity;
50
+ if (di && typeof di === "object" && typeof di.value === "string" && di.value.length > 0 && typeof di.kind === "string" && DISPLAY_IDENTITY_KINDS.includes(di.kind)) {
51
+ return { kind: di.kind, value: di.value };
52
+ }
53
+ return { kind: "did", value: didOc };
54
+ }
47
55
  function normalizeAccount(raw) {
48
56
  if (!raw) return null;
49
57
  const didOc = raw.did_oc ?? raw.didOc;
@@ -58,7 +66,8 @@ function normalizeAccount(raw) {
58
66
  nostrNpub: raw.nostr_npub ?? raw.nostrNpub ?? null,
59
67
  homeFederation: raw.home_federation_slug ?? raw.homeFederation ?? null,
60
68
  signingMethod: raw.signing_method ?? raw.signingMethod ?? null,
61
- isOwner: Boolean(raw.is_owner ?? raw.isOwner ?? false)
69
+ isOwner: Boolean(raw.is_owner ?? raw.isOwner ?? false),
70
+ displayIdentity: normalizeDisplayIdentity(raw, didOc)
62
71
  };
63
72
  }
64
73
  function OcSessionProvider({
@@ -106,13 +115,41 @@ function OcSessionProvider({
106
115
  try {
107
116
  await fetch(`${cfg.authOrigin}${cfg.logoutPath}`, {
108
117
  method: "POST",
109
- credentials: "include"
118
+ credentials: "include",
119
+ // `keepalive` lets the logout round-trip complete even if
120
+ // the caller hard-navigates away in the same tick (e.g.
121
+ // `<OcAccountMenu>` redirects home immediately on sign-out).
122
+ // Without it the in-flight request is cancelled on unload
123
+ // and the `.ochk.io` cookie may never get cleared.
124
+ keepalive: true
110
125
  });
111
126
  } catch {
112
127
  }
113
128
  setAccount(null);
114
129
  setStatus("anonymous");
115
130
  }, [cfg.authOrigin, cfg.logoutPath]);
131
+ const setDisplayIdentity = React3__namespace.useCallback(
132
+ async (kind) => {
133
+ if (typeof window === "undefined") return;
134
+ const res = await fetch(`${cfg.authOrigin}/api/auth/account`, {
135
+ method: "PATCH",
136
+ credentials: "include",
137
+ headers: { "Content-Type": "application/json" },
138
+ body: JSON.stringify({ display_identity: kind })
139
+ });
140
+ if (!res.ok) {
141
+ let reason = `http_${res.status}`;
142
+ try {
143
+ const body = await res.json();
144
+ if (body.reason) reason = body.reason;
145
+ } catch {
146
+ }
147
+ throw new Error(`[@orangecheck/auth-client] setDisplayIdentity failed: ${reason}`);
148
+ }
149
+ await refresh();
150
+ },
151
+ [cfg.authOrigin, refresh]
152
+ );
116
153
  const value = React3__namespace.useMemo(() => {
117
154
  const returnTo = defaultReturnTo ?? (typeof window !== "undefined" ? window.location.href : void 0);
118
155
  return {
@@ -121,9 +158,10 @@ function OcSessionProvider({
121
158
  error,
122
159
  refresh,
123
160
  signOut,
161
+ setDisplayIdentity,
124
162
  signInUrl: buildSignInUrl(cfg, returnTo)
125
163
  };
126
- }, [status, account, error, refresh, signOut, cfg, defaultReturnTo]);
164
+ }, [status, account, error, refresh, signOut, setDisplayIdentity, cfg, defaultReturnTo]);
127
165
  return /* @__PURE__ */ jsxRuntime.jsx(SessionContext.Provider, { value, children });
128
166
  }
129
167
  function useOcSession() {
@@ -771,6 +809,16 @@ var OcAddressInput = React3__namespace.forwardRef(
771
809
  }
772
810
  );
773
811
  var DEFAULT_AUTH_ORIGIN = "https://ochk.io";
812
+ async function fetchOcLinkedIdentities(opts) {
813
+ const authOrigin = opts?.authOrigin ?? DEFAULT_AUTH_ORIGIN;
814
+ const r = await fetch(`${authOrigin}/api/auth/identities`, {
815
+ credentials: "include"
816
+ });
817
+ if (r.status === 401) return [];
818
+ if (!r.ok) throw new Error(`identities fetch failed: http_${r.status}`);
819
+ const body = await r.json();
820
+ return body.linked ?? [];
821
+ }
774
822
  function OcLinkedIdentities({
775
823
  authOrigin = DEFAULT_AUTH_ORIGIN,
776
824
  className,
@@ -2471,6 +2519,7 @@ function handleSudoRequired(body, args = {}) {
2471
2519
  }
2472
2520
 
2473
2521
  exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
2522
+ exports.DISPLAY_IDENTITY_KINDS = DISPLAY_IDENTITY_KINDS;
2474
2523
  exports.OcAccountChip = OcAccountChip;
2475
2524
  exports.OcAccountPill = OcAccountPill;
2476
2525
  exports.OcAddressInput = OcAddressInput;
@@ -2479,6 +2528,7 @@ exports.OcSessionProvider = OcSessionProvider;
2479
2528
  exports.OcSignIn = OcSignIn;
2480
2529
  exports.OcSignInButton = OcSignInButton;
2481
2530
  exports.buildSignInUrl = buildSignInUrl;
2531
+ exports.fetchOcLinkedIdentities = fetchOcLinkedIdentities;
2482
2532
  exports.handleSudoRequired = handleSudoRequired;
2483
2533
  exports.redirectToSudo = redirectToSudo;
2484
2534
  exports.useOcAddressSuggestion = useOcAddressSuggestion;