@insforge/react 0.6.3 → 0.6.5

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
@@ -67,8 +67,9 @@ var InsforgeManager = class _InsforgeManager {
67
67
  // Static private instance
68
68
  static instance = null;
69
69
  // State storage
70
- user = null;
70
+ user = void 0;
71
71
  isLoaded = false;
72
+ isInitializing = false;
72
73
  sdk;
73
74
  listeners = /* @__PURE__ */ new Set();
74
75
  // Config
@@ -79,6 +80,8 @@ var InsforgeManager = class _InsforgeManager {
79
80
  constructor(config) {
80
81
  this.config = config;
81
82
  this.sdk = createClient({ baseUrl: config.baseUrl });
83
+ this.user = void 0;
84
+ this.isLoaded = false;
82
85
  }
83
86
  // Public access method (Singleton core)
84
87
  static getInstance(config) {
@@ -102,16 +105,28 @@ var InsforgeManager = class _InsforgeManager {
102
105
  }
103
106
  // Public initialization method
104
107
  async initialize() {
105
- if (!this.isLoaded) {
108
+ if (this.isLoaded) {
109
+ return;
110
+ }
111
+ if (this.isInitializing) {
112
+ return;
113
+ }
114
+ this.isInitializing = true;
115
+ try {
106
116
  await this.loadAuthState();
117
+ } finally {
118
+ this.isInitializing = false;
107
119
  }
108
120
  }
109
121
  // Get current state
110
122
  getState() {
123
+ const userId = this.user === void 0 ? void 0 : this.user === null ? null : this.user.id;
124
+ const isSignedIn = this.user === void 0 ? void 0 : this.user !== null;
111
125
  return {
112
126
  user: this.user,
127
+ userId,
113
128
  isLoaded: this.isLoaded,
114
- isSignedIn: !!this.user
129
+ isSignedIn
115
130
  };
116
131
  }
117
132
  // Subscription mechanism
@@ -412,22 +427,22 @@ var InsforgeManager = class _InsforgeManager {
412
427
  getSDK() {
413
428
  return this.sdk;
414
429
  }
415
- // Handle OAuth callback
416
- handleOAuthCallback(isLoaded, user) {
430
+ // Handle auth redirect after successful authentication
431
+ // Works for all auth sources: OAuth providers, cloud hosting sign-in, email verification
432
+ handleAuthRedirect(isLoaded, user) {
417
433
  if (!isLoaded || this.hasProcessedCallbackRef) {
418
434
  return false;
419
435
  }
420
- const searchParams = new URLSearchParams(window.location.search);
421
- const accessToken = searchParams.get("access_token");
422
- if (accessToken && !!user) {
423
- this.hasProcessedCallbackRef = true;
424
- const url = new URL(window.location.href);
425
- url.search = "";
426
- window.history.replaceState({}, "", url.toString());
427
- setTimeout(() => {
428
- window.location.href = this.config.afterSignInUrl || "/";
429
- }, 100);
430
- return true;
436
+ if (user && this.config.afterSignInUrl) {
437
+ const currentPath = window.location.pathname + window.location.search;
438
+ const targetPath = this.config.afterSignInUrl;
439
+ if (currentPath !== targetPath && !this.hasProcessedCallbackRef) {
440
+ this.hasProcessedCallbackRef = true;
441
+ setTimeout(() => {
442
+ window.location.href = targetPath;
443
+ }, 100);
444
+ return true;
445
+ }
431
446
  }
432
447
  return false;
433
448
  }
@@ -464,17 +479,20 @@ function InsforgeProviderCore({
464
479
  setState(newState);
465
480
  });
466
481
  void manager.initialize();
467
- return unsubscribe;
482
+ return () => {
483
+ unsubscribe();
484
+ };
468
485
  }, [manager]);
469
486
  useEffect(() => {
470
487
  if (typeof window !== "undefined") {
471
- manager.handleOAuthCallback(state.isLoaded, state.user);
488
+ manager.handleAuthRedirect(state.isLoaded, state.user);
472
489
  }
473
490
  }, [manager, state.isLoaded, state.user]);
474
491
  const contextValue = useMemo(
475
492
  () => ({
476
493
  // State from Manager
477
494
  user: state.user,
495
+ userId: state.userId,
478
496
  isLoaded: state.isLoaded,
479
497
  isSignedIn: state.isSignedIn,
480
498
  // Methods delegated to Manager
@@ -506,9 +524,10 @@ function useInsforge() {
506
524
  const context = useContext(InsforgeContext);
507
525
  if (!context) {
508
526
  return {
509
- user: null,
527
+ user: void 0,
528
+ userId: void 0,
510
529
  isLoaded: false,
511
- isSignedIn: false,
530
+ isSignedIn: void 0,
512
531
  setUser: () => {
513
532
  },
514
533
  signIn: () => Promise.resolve({ error: "SSR mode" }),
@@ -2488,16 +2507,16 @@ function Protect({
2488
2507
  condition,
2489
2508
  onRedirect
2490
2509
  }) {
2491
- const { isSignedIn, isLoaded, user } = useInsforge();
2510
+ const { userId, user } = useInsforge();
2492
2511
  const resolvedRedirectTo = useMemo(() => resolveAuthPath(redirectTo), [redirectTo]);
2493
2512
  useEffect(() => {
2494
- if (isLoaded && !isSignedIn) {
2513
+ if (userId === null) {
2495
2514
  if (onRedirect) {
2496
2515
  onRedirect(resolvedRedirectTo);
2497
2516
  } else {
2498
2517
  window.location.href = resolvedRedirectTo;
2499
2518
  }
2500
- } else if (isLoaded && isSignedIn && condition && user) {
2519
+ } else if (userId && condition && user) {
2501
2520
  if (!condition(user)) {
2502
2521
  if (onRedirect) {
2503
2522
  onRedirect(resolvedRedirectTo);
@@ -2506,11 +2525,11 @@ function Protect({
2506
2525
  }
2507
2526
  }
2508
2527
  }
2509
- }, [isLoaded, isSignedIn, resolvedRedirectTo, condition, user, onRedirect]);
2510
- if (!isLoaded) {
2528
+ }, [userId, resolvedRedirectTo, condition, user, onRedirect]);
2529
+ if (userId === void 0) {
2511
2530
  return fallback || /* @__PURE__ */ jsx("div", { className: "insforge-loading", children: "Loading..." });
2512
2531
  }
2513
- if (!isSignedIn) {
2532
+ if (userId === null) {
2514
2533
  return fallback || null;
2515
2534
  }
2516
2535
  if (condition && user && !condition(user)) {
@@ -2519,24 +2538,18 @@ function Protect({
2519
2538
  return /* @__PURE__ */ jsx(Fragment, { children });
2520
2539
  }
2521
2540
  function SignedIn({ children }) {
2522
- const { isSignedIn, isLoaded } = useInsforge();
2523
- if (!isLoaded) {
2524
- return null;
2541
+ const { userId } = useInsforge();
2542
+ if (userId) {
2543
+ return /* @__PURE__ */ jsx(Fragment, { children });
2525
2544
  }
2526
- if (!isSignedIn) {
2527
- return null;
2528
- }
2529
- return /* @__PURE__ */ jsx(Fragment, { children });
2545
+ return null;
2530
2546
  }
2531
2547
  function SignedOut({ children }) {
2532
- const { isSignedIn, isLoaded } = useInsforge();
2533
- if (!isLoaded) {
2534
- return null;
2535
- }
2536
- if (isSignedIn) {
2537
- return null;
2548
+ const { userId } = useInsforge();
2549
+ if (userId === null) {
2550
+ return /* @__PURE__ */ jsx(Fragment, { children });
2538
2551
  }
2539
- return /* @__PURE__ */ jsx(Fragment, { children });
2552
+ return null;
2540
2553
  }
2541
2554
  function SignInButton({ children, className }) {
2542
2555
  const { afterSignInUrl, baseUrl } = useInsforge();