@orangecheck/auth-client 2.12.0 → 2.14.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.mjs CHANGED
@@ -5,6 +5,7 @@ import { startRegistration, startAuthentication } from '@simplewebauthn/browser'
5
5
  // src/provider.tsx
6
6
 
7
7
  // src/types.ts
8
+ var DISPLAY_IDENTITY_KINDS = ["did", "btc", "email", "nostr"];
8
9
  var DEFAULT_CONFIG = {
9
10
  authOrigin: "https://ochk.io",
10
11
  signInPath: "/signin",
@@ -22,6 +23,13 @@ function buildSignInUrl(cfg, returnTo) {
22
23
  return u.toString();
23
24
  }
24
25
  var SessionContext = React3.createContext(null);
26
+ function normalizeDisplayIdentity(raw, didOc) {
27
+ const di = raw.display_identity ?? raw.displayIdentity;
28
+ if (di && typeof di === "object" && typeof di.value === "string" && di.value.length > 0 && typeof di.kind === "string" && DISPLAY_IDENTITY_KINDS.includes(di.kind)) {
29
+ return { kind: di.kind, value: di.value };
30
+ }
31
+ return { kind: "did", value: didOc };
32
+ }
25
33
  function normalizeAccount(raw) {
26
34
  if (!raw) return null;
27
35
  const didOc = raw.did_oc ?? raw.didOc;
@@ -36,7 +44,8 @@ function normalizeAccount(raw) {
36
44
  nostrNpub: raw.nostr_npub ?? raw.nostrNpub ?? null,
37
45
  homeFederation: raw.home_federation_slug ?? raw.homeFederation ?? null,
38
46
  signingMethod: raw.signing_method ?? raw.signingMethod ?? null,
39
- isOwner: Boolean(raw.is_owner ?? raw.isOwner ?? false)
47
+ isOwner: Boolean(raw.is_owner ?? raw.isOwner ?? false),
48
+ displayIdentity: normalizeDisplayIdentity(raw, didOc)
40
49
  };
41
50
  }
42
51
  function OcSessionProvider({
@@ -97,6 +106,28 @@ function OcSessionProvider({
97
106
  setAccount(null);
98
107
  setStatus("anonymous");
99
108
  }, [cfg.authOrigin, cfg.logoutPath]);
109
+ const setDisplayIdentity = React3.useCallback(
110
+ async (kind) => {
111
+ if (typeof window === "undefined") return;
112
+ const res = await fetch(`${cfg.authOrigin}/api/auth/account`, {
113
+ method: "PATCH",
114
+ credentials: "include",
115
+ headers: { "Content-Type": "application/json" },
116
+ body: JSON.stringify({ display_identity: kind })
117
+ });
118
+ if (!res.ok) {
119
+ let reason = `http_${res.status}`;
120
+ try {
121
+ const body = await res.json();
122
+ if (body.reason) reason = body.reason;
123
+ } catch {
124
+ }
125
+ throw new Error(`[@orangecheck/auth-client] setDisplayIdentity failed: ${reason}`);
126
+ }
127
+ await refresh();
128
+ },
129
+ [cfg.authOrigin, refresh]
130
+ );
100
131
  const value = React3.useMemo(() => {
101
132
  const returnTo = defaultReturnTo ?? (typeof window !== "undefined" ? window.location.href : void 0);
102
133
  return {
@@ -105,9 +136,10 @@ function OcSessionProvider({
105
136
  error,
106
137
  refresh,
107
138
  signOut,
139
+ setDisplayIdentity,
108
140
  signInUrl: buildSignInUrl(cfg, returnTo)
109
141
  };
110
- }, [status, account, error, refresh, signOut, cfg, defaultReturnTo]);
142
+ }, [status, account, error, refresh, signOut, setDisplayIdentity, cfg, defaultReturnTo]);
111
143
  return /* @__PURE__ */ jsx(SessionContext.Provider, { value, children });
112
144
  }
113
145
  function useOcSession() {
@@ -755,6 +787,16 @@ var OcAddressInput = React3.forwardRef(
755
787
  }
756
788
  );
757
789
  var DEFAULT_AUTH_ORIGIN = "https://ochk.io";
790
+ async function fetchOcLinkedIdentities(opts) {
791
+ const authOrigin = opts?.authOrigin ?? DEFAULT_AUTH_ORIGIN;
792
+ const r = await fetch(`${authOrigin}/api/auth/identities`, {
793
+ credentials: "include"
794
+ });
795
+ if (r.status === 401) return [];
796
+ if (!r.ok) throw new Error(`identities fetch failed: http_${r.status}`);
797
+ const body = await r.json();
798
+ return body.linked ?? [];
799
+ }
758
800
  function OcLinkedIdentities({
759
801
  authOrigin = DEFAULT_AUTH_ORIGIN,
760
802
  className,
@@ -1635,6 +1677,13 @@ function OcSignIn({
1635
1677
  const [path, setPath] = React3.useState(initialPath);
1636
1678
  const [signedIn, setSignedIn] = React3.useState(null);
1637
1679
  const [linkAlso, setLinkAlso] = React3.useState(false);
1680
+ const [oauthError, setOauthError] = React3.useState(false);
1681
+ React3.useEffect(() => {
1682
+ if (typeof window === "undefined") return;
1683
+ if (new URLSearchParams(window.location.search).has("oauth_error")) {
1684
+ setOauthError(true);
1685
+ }
1686
+ }, []);
1638
1687
  const navigate = React3.useCallback(
1639
1688
  async (account) => {
1640
1689
  if (resolveReturnTo) {
@@ -1720,6 +1769,22 @@ function OcSignIn({
1720
1769
  const bothEnabled = walletEnabled && emailEnabled;
1721
1770
  const activeMethod = !emailEnabled ? "wallet" : !walletEnabled ? "email" : path;
1722
1771
  return /* @__PURE__ */ jsxs("div", { className, "data-oc-signin": "", children: [
1772
+ oauthError && /* @__PURE__ */ jsx(
1773
+ "div",
1774
+ {
1775
+ "data-oc-signin-oauth-error": "",
1776
+ style: {
1777
+ marginBottom: 14,
1778
+ padding: "0.6rem 0.75rem",
1779
+ border: "1px solid var(--destructive, #ef4444)",
1780
+ borderRadius: 6,
1781
+ color: "var(--destructive, #ef4444)",
1782
+ fontFamily: "ui-monospace, SFMono-Regular, monospace",
1783
+ fontSize: 11
1784
+ },
1785
+ children: "That sign-in didn't complete. Please try again."
1786
+ }
1787
+ ),
1723
1788
  bothEnabled && /* @__PURE__ */ jsxs(
1724
1789
  "div",
1725
1790
  {
@@ -1762,6 +1827,7 @@ function OcSignIn({
1762
1827
  }
1763
1828
  )
1764
1829
  ] }),
1830
+ /* @__PURE__ */ jsx(ProviderSignIn, { authOrigin, returnTo: safeReturn }),
1765
1831
  linkPrompt && /* @__PURE__ */ jsxs("label", { "data-oc-signin-linkalso": "", style: linkAlsoStyle, children: [
1766
1832
  /* @__PURE__ */ jsx(
1767
1833
  "input",
@@ -1812,6 +1878,63 @@ function SigninTab({
1812
1878
  }
1813
1879
  );
1814
1880
  }
1881
+ var OAUTH_PROVIDERS = [
1882
+ { id: "google", label: "Continue with Google" }
1883
+ ];
1884
+ function ProviderSignIn({
1885
+ authOrigin,
1886
+ returnTo
1887
+ }) {
1888
+ const line = { flex: 1, height: 1, background: "var(--border, #27272a)" };
1889
+ return /* @__PURE__ */ jsxs("div", { "data-oc-signin-providers": "", style: { marginTop: 20 }, children: [
1890
+ /* @__PURE__ */ jsxs(
1891
+ "div",
1892
+ {
1893
+ style: {
1894
+ display: "flex",
1895
+ alignItems: "center",
1896
+ gap: 10,
1897
+ margin: "4px 0 12px",
1898
+ color: "var(--muted-foreground, #a1a1aa)",
1899
+ fontFamily: "ui-monospace, SFMono-Regular, monospace",
1900
+ fontSize: 10,
1901
+ letterSpacing: "0.16em",
1902
+ textTransform: "uppercase"
1903
+ },
1904
+ children: [
1905
+ /* @__PURE__ */ jsx("span", { style: line }),
1906
+ "or",
1907
+ /* @__PURE__ */ jsx("span", { style: line })
1908
+ ]
1909
+ }
1910
+ ),
1911
+ OAUTH_PROVIDERS.map((p) => /* @__PURE__ */ jsx(
1912
+ "a",
1913
+ {
1914
+ href: `${authOrigin}/api/auth/${p.id}/start?return_to=${encodeURIComponent(
1915
+ returnTo
1916
+ )}`,
1917
+ "data-oc-signin-provider": p.id,
1918
+ style: {
1919
+ display: "block",
1920
+ boxSizing: "border-box",
1921
+ width: "100%",
1922
+ padding: "0.6rem 0.875rem",
1923
+ textAlign: "center",
1924
+ border: "1px solid var(--border, #27272a)",
1925
+ borderRadius: 6,
1926
+ background: "transparent",
1927
+ color: "var(--muted-foreground, #a1a1aa)",
1928
+ fontFamily: "ui-monospace, SFMono-Regular, monospace",
1929
+ fontSize: 12,
1930
+ textDecoration: "none"
1931
+ },
1932
+ children: p.label
1933
+ },
1934
+ p.id
1935
+ ))
1936
+ ] });
1937
+ }
1815
1938
  function WalletFlow({ authOrigin, audience, onSuccess }) {
1816
1939
  const [address, setAddress] = React3.useState("");
1817
1940
  const [error, setError] = React3.useState(null);
@@ -2454,6 +2577,6 @@ function handleSudoRequired(body, args = {}) {
2454
2577
  return false;
2455
2578
  }
2456
2579
 
2457
- export { DEFAULT_CONFIG, OcAccountChip, OcAccountPill, OcAddressInput, OcLinkedIdentities, OcSessionProvider, OcSignIn, OcSignInButton, buildSignInUrl, handleSudoRequired, redirectToSudo, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister };
2580
+ export { DEFAULT_CONFIG, DISPLAY_IDENTITY_KINDS, OcAccountChip, OcAccountPill, OcAddressInput, OcLinkedIdentities, OcSessionProvider, OcSignIn, OcSignInButton, buildSignInUrl, fetchOcLinkedIdentities, handleSudoRequired, redirectToSudo, useOcAddressSuggestion, useOcSession, useOptionalOcSession, useStepUpAuth, useWebAuthnList, useWebAuthnRegister };
2458
2581
  //# sourceMappingURL=index.mjs.map
2459
2582
  //# sourceMappingURL=index.mjs.map