@huaqiu/dsh-auth 0.3.15 → 0.3.17

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/lib/client.js CHANGED
@@ -89,6 +89,18 @@ window.__ModuleLoader__.load({
89
89
  headers: { accept: "application/json" }
90
90
  });
91
91
  if (!res.ok) throw new Error(`auth login trigger failed: HTTP ${res.status}`);
92
+ },
93
+ async fetchUserInfo() {
94
+ try {
95
+ const res = await doFetch(`${base}/user-info`, {
96
+ method: "GET",
97
+ headers: { accept: "application/json" }
98
+ });
99
+ if (!res.ok) return null;
100
+ return (await res.json()).userInfo ?? null;
101
+ } catch {
102
+ return null;
103
+ }
92
104
  }
93
105
  };
94
106
  }
@@ -767,6 +779,11 @@ window.__ModuleLoader__.load({
767
779
  if (hostMode && !hostSessionLoaded) await resolveHost();
768
780
  return hostMode ? hostSession : hostSession ?? storage.get();
769
781
  },
782
+ getUserProfile: async () => {
783
+ if (hostMode && !hostSessionLoaded) await resolveHost();
784
+ if (!await auth.getAccessToken()) return null;
785
+ return transport.fetchUserInfo();
786
+ },
770
787
  login: async (options) => {
771
788
  if (hostMode) {
772
789
  const prevToken = hostSession?.token;
@@ -780,9 +797,10 @@ window.__ModuleLoader__.load({
780
797
  },
781
798
  logout: async () => {
782
799
  if (hostMode) {
783
- hostSession = null;
784
- storage.clear();
785
- emit(null);
800
+ await transport.pushLogout();
801
+ hostSessionLoaded = false;
802
+ await resolveHost();
803
+ emit(hostSession);
786
804
  return;
787
805
  }
788
806
  storage.clear();
@@ -934,6 +952,17 @@ window.__ModuleLoader__.load({
934
952
  listeners.add(callback);
935
953
  return () => listeners.delete(callback);
936
954
  }
955
+ /**
956
+ * Merge the rich eda.cn profile (nickname + headimage) into the store —
957
+ * sidebar components call this after `auth.getUserProfile()` in host mode.
958
+ * The trigger then renders the real name/photo instead of the bare HQ icon.
959
+ */
960
+ function setProfile(profile) {
961
+ setState({
962
+ ...state,
963
+ profile: profile ?? void 0
964
+ });
965
+ }
937
966
  /** Register the node re-sync hook (wired in apply(); called by the login card on mount). */
938
967
  function registerAuthSync(fn) {
939
968
  syncNow = fn;
@@ -1293,14 +1322,36 @@ window.__ModuleLoader__.load({
1293
1322
  const menuRef = (0, react.useRef)(null);
1294
1323
  const palette = dark ? DARK_PALETTE : LIGHT_PALETTE;
1295
1324
  const authenticated = authState.authenticated;
1296
- const avatar = authenticated && !avatarBroken ? authState.avatar : void 0;
1297
1325
  const showLabel = wide !== false;
1298
1326
  const hostMode = auth?.isHostMode?.() ?? false;
1299
1327
  const targetHost = hostMode ? getHqEdge()?.context?.getTargetHost?.() ?? "" : "";
1300
1328
  if (hostMode && targetHost === "hq-eda") return null;
1329
+ const profile = authState.profile;
1330
+ (0, react.useEffect)(() => {
1331
+ if (!hostMode || targetHost === "hq-eda") return;
1332
+ if (!authenticated) {
1333
+ setProfile(null);
1334
+ return;
1335
+ }
1336
+ let cancelled = false;
1337
+ auth?.getUserProfile?.().then((info) => {
1338
+ if (cancelled) return;
1339
+ setProfile(info);
1340
+ }).catch(() => {});
1341
+ return () => {
1342
+ cancelled = true;
1343
+ };
1344
+ }, [
1345
+ hostMode,
1346
+ targetHost,
1347
+ authenticated
1348
+ ]);
1349
+ const displayName = profile?.nickname ?? authState.nickname;
1350
+ const displayAvatar = profile?.headimage ?? authState.avatar;
1351
+ const avatar = authenticated && !avatarBroken ? displayAvatar : void 0;
1301
1352
  (0, react.useEffect)(() => {
1302
1353
  setAvatarBroken(false);
1303
- }, [authState.avatar]);
1354
+ }, [displayAvatar]);
1304
1355
  (0, react.useEffect)(() => {
1305
1356
  if (!authenticated) setMenuOpen(false);
1306
1357
  }, [authenticated]);
@@ -1367,7 +1418,7 @@ window.__ModuleLoader__.load({
1367
1418
  window.open(buildProfileUrl(info), "_blank", "noopener,noreferrer");
1368
1419
  })();
1369
1420
  };
1370
- const label = authenticated ? authState.nickname ?? t("sidebar.account") : t("sidebar.login");
1421
+ const label = authenticated ? displayName ?? t("sidebar.account") : t("sidebar.login");
1371
1422
  const title = authenticated ? t("sidebar.accountTitle") : t("sidebar.loginTitle");
1372
1423
  const triggerBackground = menuOpen || hovered ? palette.hover : "transparent";
1373
1424
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
@@ -1434,13 +1485,13 @@ window.__ModuleLoader__.load({
1434
1485
  role: "menu",
1435
1486
  style: menuStyle,
1436
1487
  children: [
1437
- authState.nickname ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1488
+ displayName ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1438
1489
  style: {
1439
1490
  ...MENU_HEADER_BASE,
1440
1491
  color: palette.muted
1441
1492
  },
1442
- title: authState.nickname,
1443
- children: authState.nickname
1493
+ title: displayName,
1494
+ children: displayName
1444
1495
  }) : null,
1445
1496
  /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MenuItem, {
1446
1497
  label: t("menu.profile"),
@@ -1455,7 +1506,7 @@ window.__ModuleLoader__.load({
1455
1506
  palette,
1456
1507
  onSelect: () => {
1457
1508
  setMenuOpen(false);
1458
- auth.logout();
1509
+ auth.logout().catch(() => {});
1459
1510
  }
1460
1511
  })
1461
1512
  ]