@insforge/react 0.6.2 → 0.6.4

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
@@ -69,6 +69,7 @@ var InsforgeManager = class _InsforgeManager {
69
69
  // State storage
70
70
  user = null;
71
71
  isLoaded = false;
72
+ isInitializing = false;
72
73
  sdk;
73
74
  listeners = /* @__PURE__ */ new Set();
74
75
  // Config
@@ -79,6 +80,11 @@ var InsforgeManager = class _InsforgeManager {
79
80
  constructor(config) {
80
81
  this.config = config;
81
82
  this.sdk = createClient({ baseUrl: config.baseUrl });
83
+ const session = this.sdk.auth.getCurrentSession();
84
+ if (!session.data?.session?.accessToken) {
85
+ this.isLoaded = true;
86
+ this.user = null;
87
+ }
82
88
  }
83
89
  // Public access method (Singleton core)
84
90
  static getInstance(config) {
@@ -102,8 +108,17 @@ var InsforgeManager = class _InsforgeManager {
102
108
  }
103
109
  // Public initialization method
104
110
  async initialize() {
105
- if (!this.isLoaded) {
111
+ if (this.isLoaded) {
112
+ return;
113
+ }
114
+ if (this.isInitializing) {
115
+ return;
116
+ }
117
+ this.isInitializing = true;
118
+ try {
106
119
  await this.loadAuthState();
120
+ } finally {
121
+ this.isInitializing = false;
107
122
  }
108
123
  }
109
124
  // Get current state
@@ -209,6 +224,7 @@ var InsforgeManager = class _InsforgeManager {
209
224
  avatarUrl: profile?.avatarUrl || ""
210
225
  };
211
226
  this.user = userData;
227
+ this.notifyListeners();
212
228
  if (this.config.onAuthChange) {
213
229
  this.config.onAuthChange(userData);
214
230
  }
@@ -221,7 +237,6 @@ var InsforgeManager = class _InsforgeManager {
221
237
  }
222
238
  }
223
239
  }
224
- this.notifyListeners();
225
240
  } else if (fallbackUser) {
226
241
  const userData = {
227
242
  id: fallbackUser.id || "",
@@ -230,10 +245,10 @@ var InsforgeManager = class _InsforgeManager {
230
245
  avatarUrl: ""
231
246
  };
232
247
  this.user = userData;
248
+ this.notifyListeners();
233
249
  if (this.config.onAuthChange) {
234
250
  this.config.onAuthChange(userData);
235
251
  }
236
- this.notifyListeners();
237
252
  }
238
253
  }
239
254
  // Business methods
@@ -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,11 +479,13 @@ 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(