@fluid-app/rep-sdk 0.1.1 → 0.1.2

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.js CHANGED
@@ -193,6 +193,7 @@ function createFluidClient(config) {
193
193
  }
194
194
  if (response.status === 401) {
195
195
  effectiveOnAuthError();
196
+ return null;
196
197
  }
197
198
  if (!response.ok) {
198
199
  try {
@@ -353,6 +354,16 @@ function createFluidClient(config) {
353
354
  var ThemeContext = createContext(null);
354
355
  function applyThemeVariables(theme, container) {
355
356
  const target = container ?? document.documentElement;
357
+ const toRemove = [];
358
+ for (let i = 0; i < target.style.length; i++) {
359
+ const prop = target.style[i];
360
+ if (prop.startsWith("--fluid-")) {
361
+ toRemove.push(prop);
362
+ }
363
+ }
364
+ for (const prop of toRemove) {
365
+ target.style.removeProperty(prop);
366
+ }
356
367
  for (const [key, value] of Object.entries(theme.config)) {
357
368
  target.style.setProperty(`--fluid-${key}`, value);
358
369
  }
@@ -419,7 +430,11 @@ function FluidProvider({
419
430
  const configRef = useRef(config);
420
431
  configRef.current = config;
421
432
  const client = useMemo(
422
- () => createFluidClient(configRef.current),
433
+ () => createFluidClient({
434
+ ...configRef.current,
435
+ getAuthToken: () => configRef.current.getAuthToken?.() ?? null,
436
+ onAuthError: () => configRef.current.onAuthError?.()
437
+ }),
423
438
  // eslint-disable-next-line react-hooks/exhaustive-deps
424
439
  [config.baseUrl]
425
440
  );
@@ -472,10 +487,12 @@ var URL_PARAMS = {
472
487
  COMPANY_TOKEN: "fluidCompanyToken"
473
488
  };
474
489
 
475
- // src/auth/url-token.ts
490
+ // src/auth/browser-utils.ts
476
491
  function isBrowser() {
477
- return typeof window !== "undefined";
492
+ return typeof window !== "undefined" && typeof document !== "undefined";
478
493
  }
494
+
495
+ // src/auth/url-token.ts
479
496
  function extractTokenFromUrl(tokenKey = URL_PARAMS.USER_TOKEN) {
480
497
  if (!isBrowser()) {
481
498
  return null;
@@ -546,11 +563,8 @@ function extractAllTokensFromUrl(userTokenKey = URL_PARAMS.USER_TOKEN, companyTo
546
563
  }
547
564
 
548
565
  // src/auth/token-storage.ts
549
- function isBrowser2() {
550
- return typeof window !== "undefined" && typeof document !== "undefined";
551
- }
552
566
  function parseCookies() {
553
- if (!isBrowser2()) {
567
+ if (!isBrowser()) {
554
568
  return {};
555
569
  }
556
570
  const cookies = {};
@@ -567,7 +581,7 @@ function parseCookies() {
567
581
  return cookies;
568
582
  }
569
583
  function setCookie(name, value, options = {}) {
570
- if (!isBrowser2()) {
584
+ if (!isBrowser()) {
571
585
  return;
572
586
  }
573
587
  const {
@@ -586,13 +600,13 @@ function setCookie(name, value, options = {}) {
586
600
  document.cookie = cookieString;
587
601
  }
588
602
  function deleteCookie(name, path = "/") {
589
- if (!isBrowser2()) {
603
+ if (!isBrowser()) {
590
604
  return;
591
605
  }
592
606
  document.cookie = `${name}=; path=${path}; max-age=0`;
593
607
  }
594
608
  function getStoredToken(config) {
595
- if (!isBrowser2()) {
609
+ if (!isBrowser()) {
596
610
  return null;
597
611
  }
598
612
  const cookieKey = config?.cookieKey ?? STORAGE_KEYS.AUTH_COOKIE;
@@ -609,7 +623,7 @@ function getStoredToken(config) {
609
623
  }
610
624
  }
611
625
  function storeToken(token, config) {
612
- if (!isBrowser2()) {
626
+ if (!isBrowser()) {
613
627
  return;
614
628
  }
615
629
  const cookieKey = config?.cookieKey ?? STORAGE_KEYS.AUTH_COOKIE;
@@ -632,7 +646,7 @@ function storeToken(token, config) {
632
646
  }
633
647
  }
634
648
  function clearTokens(config) {
635
- if (!isBrowser2()) {
649
+ if (!isBrowser()) {
636
650
  return;
637
651
  }
638
652
  const cookieKey = config?.cookieKey ?? STORAGE_KEYS.AUTH_COOKIE;
@@ -649,16 +663,31 @@ function clearTokens(config) {
649
663
  function hasStoredToken(config) {
650
664
  return getStoredToken(config) !== null;
651
665
  }
666
+
667
+ // src/auth/types.ts
668
+ var USER_TYPES = {
669
+ admin: "admin",
670
+ rep: "rep",
671
+ root_admin: "root_admin",
672
+ customer: "customer"
673
+ };
674
+ function isUserType(value) {
675
+ return Object.values(USER_TYPES).includes(value);
676
+ }
677
+
678
+ // src/auth/token-utils.ts
652
679
  function extractPayloadFromJose(decoded) {
680
+ const rawUserType = decoded.user_type;
681
+ const rawOgUserType = decoded.og_user_type;
653
682
  return {
654
- id: decoded.id,
655
- email: decoded.email,
656
- full_name: decoded.full_name,
657
- user_type: decoded.user_type ?? "rep",
658
- og_user_type: decoded.og_user_type,
659
- company_id: decoded.company_id,
683
+ id: typeof decoded.id === "number" ? decoded.id : void 0,
684
+ email: typeof decoded.email === "string" ? decoded.email : void 0,
685
+ full_name: typeof decoded.full_name === "string" ? decoded.full_name : void 0,
686
+ user_type: typeof rawUserType === "string" && isUserType(rawUserType) ? rawUserType : "rep",
687
+ og_user_type: typeof rawOgUserType === "string" && isUserType(rawOgUserType) ? rawOgUserType : void 0,
688
+ company_id: typeof decoded.company_id === "number" ? decoded.company_id : void 0,
660
689
  exp: decoded.exp,
661
- auth_type: decoded.auth_type
690
+ auth_type: typeof decoded.auth_type === "string" ? decoded.auth_type : void 0
662
691
  };
663
692
  }
664
693
  function decodeToken(token) {
@@ -748,17 +777,6 @@ async function verifyToken(token, jwksUrl) {
748
777
  }
749
778
  }
750
779
 
751
- // src/auth/types.ts
752
- var USER_TYPES = {
753
- admin: "admin",
754
- rep: "rep",
755
- root_admin: "root_admin",
756
- customer: "customer"
757
- };
758
- function isUserType(value) {
759
- return Object.values(USER_TYPES).includes(value);
760
- }
761
-
762
780
  // src/auth/dev-utils.ts
763
781
  function isDevBypassActive(devBypass) {
764
782
  if (!devBypass) return false;
@@ -770,12 +788,14 @@ function isDevBypassActive(devBypass) {
770
788
  }
771
789
  function createDevUser() {
772
790
  return {
773
- id: 0,
791
+ id: 99999,
792
+ // Dev placeholder — avoids falsy 0
774
793
  email: "dev@localhost",
775
794
  full_name: "Dev User",
776
795
  user_type: USER_TYPES.rep,
777
796
  og_user_type: void 0,
778
- company_id: 0,
797
+ company_id: 99999,
798
+ // Dev placeholder — avoids falsy 0
779
799
  exp: void 0,
780
800
  // Never expires
781
801
  auth_type: "dev_bypass"
@@ -794,7 +814,11 @@ function FluidAuthProvider({
794
814
  const [error, setError] = useState(null);
795
815
  useEffect(() => {
796
816
  const initializeAuth = async () => {
797
- const handleAuthFailure = resolveAuthFailureHandler(config?.onAuthFailure, config?.authUrl);
817
+ const handleAuthFailure = () => {
818
+ const current = configRef.current;
819
+ const handler = resolveAuthFailureHandler(current?.onAuthFailure, current?.authUrl);
820
+ handler();
821
+ };
798
822
  try {
799
823
  if (isDevBypassActive(config?.devBypass)) {
800
824
  const envToken = import.meta.env.VITE_DEV_TOKEN;
@@ -1917,7 +1941,19 @@ function AuthError({
1917
1941
  }
1918
1942
  );
1919
1943
  }
1944
+ var SPIN_STYLE_ID = "fluid-auth-loading-spin";
1945
+ function ensureSpinStyle() {
1946
+ if (typeof document === "undefined") return;
1947
+ if (document.getElementById(SPIN_STYLE_ID)) return;
1948
+ const style = document.createElement("style");
1949
+ style.id = SPIN_STYLE_ID;
1950
+ style.textContent = `@keyframes spin { to { transform: rotate(360deg); } }`;
1951
+ document.head.appendChild(style);
1952
+ }
1920
1953
  function AuthLoading() {
1954
+ useEffect(() => {
1955
+ ensureSpinStyle();
1956
+ }, []);
1921
1957
  return /* @__PURE__ */ jsxs(
1922
1958
  "div",
1923
1959
  {
@@ -1954,12 +1990,7 @@ function AuthLoading() {
1954
1990
  },
1955
1991
  children: "Authenticating..."
1956
1992
  }
1957
- ),
1958
- /* @__PURE__ */ jsx("style", { children: `
1959
- @keyframes spin {
1960
- to { transform: rotate(360deg); }
1961
- }
1962
- ` })
1993
+ )
1963
1994
  ]
1964
1995
  }
1965
1996
  );