@rspress-theme-anatole/theme-default 0.7.26 → 0.7.28

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.
Files changed (2) hide show
  1. package/dist/bundle.js +37 -39
  2. package/package.json +3 -3
package/dist/bundle.js CHANGED
@@ -339,72 +339,70 @@ function normalizeRoleList(value) {
339
339
  const list = Array.isArray(value) ? value : String(value).split(',');
340
340
  return list.map((role) => String(role).trim().toLowerCase()).filter(Boolean);
341
341
  }
342
- // Cache for user roles fetched from userContext endpoint
343
- let _cachedUserRoles = null;
344
- let _userRolesFetchPromise = null;
342
+ // Cache for user context fetched from userContext endpoint
343
+ let _cachedUserContext = null;
344
+ let _userContextFetchPromise = null;
345
345
 
346
- function getUserRoles() {
347
- // Return cached roles if available
348
- if (_cachedUserRoles !== null) return _cachedUserRoles;
349
- return [];
346
+ function getUserContext() {
347
+ // Return cached user context if available
348
+ if (_cachedUserContext !== null) return _cachedUserContext;
349
+ return { isAuthenticated: false, userId: null };
350
350
  }
351
351
 
352
- async function fetchUserRoles(userContextUrl) {
353
- if (!userContextUrl) return [];
354
- if (_cachedUserRoles !== null) return _cachedUserRoles;
355
- if (_userRolesFetchPromise) return _userRolesFetchPromise;
352
+ async function fetchUserContext(userContextUrl) {
353
+ if (!userContextUrl) return { isAuthenticated: false, userId: null };
354
+ if (_cachedUserContext !== null) return _cachedUserContext;
355
+ if (_userContextFetchPromise) return _userContextFetchPromise;
356
356
 
357
- _userRolesFetchPromise = (async () => {
357
+ _userContextFetchPromise = (async () => {
358
358
  try {
359
359
  const response = await fetch(userContextUrl, { credentials: 'include' });
360
360
  if (!response.ok) {
361
361
  console.warn('Failed to fetch user context:', response.status);
362
- _cachedUserRoles = [];
363
- return [];
362
+ _cachedUserContext = { isAuthenticated: false, userId: null };
363
+ return _cachedUserContext;
364
364
  }
365
365
  const data = await response.json();
366
- const authMethods = ["anonymous", "internal-ad", "aos-sso", "azure-b2c"];
367
- const userauthMethod = data?.authMethod || "";
368
- const roles = userauthMethod === "anonymous"
369
- ? ["anonymous"]
370
- : authMethods.filter(m => m !== "anonymous");
371
- _cachedUserRoles = normalizeRoleList(roles);
366
+ _cachedUserContext = {
367
+ isAuthenticated: data?.isAuthenticated || false,
368
+ userId: data?.userId || null
369
+ };
372
370
  // Dispatch event to trigger sidebar re-render
373
371
  if (typeof window !== 'undefined') {
374
- window.dispatchEvent(new Event('UserRolesReady'));
372
+ window.dispatchEvent(new Event('UserContextReady'));
375
373
  }
376
- return _cachedUserRoles;
374
+ return _cachedUserContext;
377
375
  } catch (e) {
378
376
  console.warn('Error fetching user context:', e);
379
- _cachedUserRoles = [];
380
- return [];
377
+ _cachedUserContext = { isAuthenticated: false, userId: null };
378
+ return _cachedUserContext;
381
379
  } finally {
382
- _userRolesFetchPromise = null;
380
+ _userContextFetchPromise = null;
383
381
  }
384
382
  })();
385
383
 
386
- return _userRolesFetchPromise;
384
+ return _userContextFetchPromise;
387
385
  }
388
- function isItemLocked(item, userRoles) {
386
+ function isItemLocked(item, userContext) {
389
387
  const requiredRoles = normalizeRoleList(item?.roles);
390
388
  // No roles required = public content (not locked)
391
389
  if (!requiredRoles.length) return false;
392
- // Has roles required but user has matching role = unlocked
393
- if (userRoles.length && requiredRoles.some((role) => userRoles.includes(role))) return false;
394
- // Has roles required but user doesn't have matching role = locked
390
+ // User is authenticated with valid userId = unlocked (regardless of roles)
391
+ if (userContext?.isAuthenticated && userContext?.userId) return false;
392
+ // Has roles required but user is not authenticated = locked
395
393
  return true;
396
394
  }
397
- function markLockedSidebarItems(items, userRoles) {
395
+ function markLockedSidebarItems(items, userContext) {
398
396
  if (!Array.isArray(items)) return items;
399
397
  return items.map((item) => {
400
398
  if (!item) return null;
401
399
  let nextItem = item;
402
- const locked = isItemLocked(item, userRoles);
400
+ const locked = isItemLocked(item, userContext);
403
401
  if (locked) {
404
402
  nextItem = { ...nextItem, locked: true };
405
403
  }
406
404
  if ('items' in item && Array.isArray(item.items)) {
407
- const markedItems = markLockedSidebarItems(item.items, userRoles);
405
+ const markedItems = markLockedSidebarItems(item.items, userContext);
408
406
  nextItem = { ...nextItem, items: markedItems };
409
407
  }
410
408
  return nextItem;
@@ -417,11 +415,11 @@ function useSidebarData() {
417
415
  const pathname = decodeURIComponent(rawPathname);
418
416
  const [roleVersion, setRoleVersion] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(0);
419
417
 
420
- // Fetch user roles from userContext endpoint
418
+ // Fetch user context from userContext endpoint
421
419
  (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(() => {
422
420
  const userContextUrl = siteData?.auth?.endpoints?.userContext;
423
421
  if (userContextUrl) {
424
- fetchUserRoles(userContextUrl);
422
+ fetchUserContext(userContextUrl);
425
423
  }
426
424
  }, [siteData?.auth?.endpoints?.userContext]);
427
425
 
@@ -429,15 +427,15 @@ function useSidebarData() {
429
427
  if ('undefined' == typeof window) return;
430
428
  const handler = () => setRoleVersion((v) => v + 1);
431
429
  window.addEventListener('MetaRolesReady', handler);
432
- window.addEventListener('UserRolesReady', handler);
430
+ window.addEventListener('UserContextReady', handler);
433
431
  return () => {
434
432
  window.removeEventListener('MetaRolesReady', handler);
435
- window.removeEventListener('UserRolesReady', handler);
433
+ window.removeEventListener('UserContextReady', handler);
436
434
  };
437
435
  }, []);
438
436
  const sidebarData = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(() => {
439
437
  const data = getSidebarDataGroup(sidebar, pathname);
440
- const userRoles = getUserRoles();
438
+ const userContext = getUserContext();
441
439
  let dataWithRoles = data;
442
440
  try {
443
441
  const roleMap = 'undefined' != typeof window ? window.__META_ROLE_MAP__ : null;
@@ -470,7 +468,7 @@ function useSidebarData() {
470
468
  }
471
469
  } catch (e) {
472
470
  }
473
- return markLockedSidebarItems(dataWithRoles, userRoles);
471
+ return markLockedSidebarItems(dataWithRoles, userContext);
474
472
  }, [
475
473
  sidebar,
476
474
  pathname,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rspress-theme-anatole/theme-default",
3
3
  "author": "Anatole Tong",
4
- "version": "0.7.26",
4
+ "version": "0.7.28",
5
5
  "license": "MIT",
6
6
  "sideEffects": [
7
7
  "*.css",
@@ -21,8 +21,8 @@
21
21
  "types": "./dist/bundle.d.ts",
22
22
  "dependencies": {
23
23
  "@mdx-js/react": "2.3.0",
24
- "@rspress-theme-anatole/rspress-plugin-mermaid": "0.7.26",
25
- "@rspress-theme-anatole/shared": "0.7.26",
24
+ "@rspress-theme-anatole/rspress-plugin-mermaid": "0.7.28",
25
+ "@rspress-theme-anatole/shared": "0.7.28",
26
26
  "@rspress/runtime": "1.43.8",
27
27
  "body-scroll-lock": "4.0.0-beta.0",
28
28
  "copy-to-clipboard": "^3.3.3",