@insforge/react 1.0.2-dev.3 → 1.0.2-dev.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
@@ -473,9 +473,9 @@ var InsforgeManager = class _InsforgeManager {
473
473
  // State storage
474
474
  user = void 0;
475
475
  isLoaded = false;
476
- isInitializing = false;
477
476
  sdk;
478
477
  listeners = /* @__PURE__ */ new Set();
478
+ authSubscription = null;
479
479
  // Config
480
480
  config;
481
481
  refreshIntervalRef = null;
@@ -485,6 +485,93 @@ var InsforgeManager = class _InsforgeManager {
485
485
  this.sdk = createClient({ baseUrl: config.baseUrl });
486
486
  this.user = void 0;
487
487
  this.isLoaded = false;
488
+ this.setupAuthSubscription();
489
+ }
490
+ // Setup subscription to SDK auth state changes
491
+ setupAuthSubscription() {
492
+ const { data } = this.sdk.auth.onAuthStateChange(
493
+ async (event, session) => {
494
+ await this.handleAuthStateChange(event, session);
495
+ }
496
+ );
497
+ this.authSubscription = data.subscription;
498
+ }
499
+ // Handle auth state changes from SDK
500
+ async handleAuthStateChange(event, session) {
501
+ switch (event) {
502
+ case "INITIAL_SESSION":
503
+ if (session?.accessToken) {
504
+ await this.loadFullUserProfile();
505
+ } else {
506
+ this.user = null;
507
+ this.isLoaded = true;
508
+ this.notifyListeners();
509
+ }
510
+ break;
511
+ case "SIGNED_IN":
512
+ if (session?.accessToken) {
513
+ await this.loadFullUserProfile();
514
+ if (this.config.onSignIn) {
515
+ try {
516
+ await this.config.onSignIn(session.accessToken);
517
+ } catch (error) {
518
+ console.error("[InsforgeManager] Error in onSignIn callback:", error);
519
+ }
520
+ }
521
+ }
522
+ break;
523
+ case "SIGNED_OUT":
524
+ this.user = null;
525
+ this.isLoaded = true;
526
+ if (this.config.onAuthChange) {
527
+ this.config.onAuthChange(null);
528
+ }
529
+ if (this.config.onSignOut) {
530
+ try {
531
+ await this.config.onSignOut();
532
+ } catch (error) {
533
+ console.error("[InsforgeManager] Error in onSignOut callback:", error);
534
+ }
535
+ }
536
+ this.notifyListeners();
537
+ break;
538
+ }
539
+ }
540
+ // Load full user profile from API
541
+ async loadFullUserProfile() {
542
+ try {
543
+ const userResult = await this.sdk.auth.getCurrentUser();
544
+ if (userResult.data) {
545
+ const profile = userResult.data.profile;
546
+ const userData = {
547
+ id: userResult.data.user.id,
548
+ email: userResult.data.user.email,
549
+ name: profile?.nickname || "",
550
+ avatarUrl: profile?.avatarUrl || ""
551
+ };
552
+ this.user = userData;
553
+ this.isLoaded = true;
554
+ if (this.config.onAuthChange) {
555
+ this.config.onAuthChange(userData);
556
+ }
557
+ this.notifyListeners();
558
+ } else {
559
+ this.user = null;
560
+ this.isLoaded = true;
561
+ if (this.config.onAuthChange) {
562
+ this.config.onAuthChange(null);
563
+ }
564
+ this.notifyListeners();
565
+ }
566
+ } catch (error) {
567
+ console.error("[InsforgeManager] Failed to load user profile:", error);
568
+ this.user = null;
569
+ this.isLoaded = true;
570
+ if (this.config.onAuthChange) {
571
+ this.config.onAuthChange(null);
572
+ }
573
+ this.notifyListeners();
574
+ }
488
575
  }
489
576
  // Public access method (Singleton core)
490
577
  static getInstance(config) {
@@ -506,28 +593,6 @@ var InsforgeManager = class _InsforgeManager {
506
593
  updateConfig(config) {
507
594
  this.config = { ...this.config, ...config };
508
595
  }
509
- // Public initialization method
510
- // Even if we have initialState (isLoaded=true from cookies), we still need to load full user data from SDK/API
511
- async initialize() {
512
- if (this.isInitializing) {
513
- return;
514
- }
515
- this.isInitializing = true;
516
- try {
517
- await this.sdk.initialize();
518
- const sessionResult = this.sdk.auth.getCurrentSession();
519
- const hasToken = !!sessionResult.data?.session?.accessToken;
520
- if (hasToken) {
521
- await this.loadAuthState();
522
- } else if (this.user === void 0) {
523
- this.user = null;
524
- this.isLoaded = true;
525
- this.notifyListeners();
526
- }
527
- } finally {
528
- this.isInitializing = false;
529
- }
530
- }
531
596
  // Get current state
532
597
  getState() {
533
598
  const userId = this.user === void 0 ? void 0 : this.user === null ? null : this.user.id;
@@ -548,84 +613,7 @@ var InsforgeManager = class _InsforgeManager {
548
613
  const state = this.getState();
549
614
  this.listeners.forEach((listener) => listener(state));
550
615
  }
551
- // Load auth state
552
- // This loads the FULL user data from SDK (including complete profile)
553
- // Called after hydration to get complete user information beyond what's in cookies
554
- async loadAuthState() {
555
- try {
556
- const sessionResult = this.sdk.auth.getCurrentSession();
557
- const session = sessionResult.data?.session;
558
- const token2 = session?.accessToken || null;
559
- if (!token2) {
560
- this.user = null;
561
- if (this.config.onAuthChange) {
562
- this.config.onAuthChange(null);
563
- }
564
- this.isLoaded = true;
565
- this.notifyListeners();
566
- return { success: false, error: "no_session" };
567
- }
568
- const userResult = await this.sdk.auth.getCurrentUser();
569
- if (userResult.data) {
570
- const profile = userResult.data.profile;
571
- const userData = {
572
- id: userResult.data.user.id,
573
- email: userResult.data.user.email,
574
- name: profile?.nickname || "",
575
- avatarUrl: profile?.avatarUrl || ""
576
- // You can add more profile fields here as needed
577
- };
578
- this.user = userData;
579
- if (this.config.onAuthChange) {
580
- this.config.onAuthChange(userData);
581
- }
582
- this.isLoaded = true;
583
- this.notifyListeners();
584
- return { success: true };
585
- } else {
586
- await this.sdk.auth.signOut();
587
- if (this.config.onSignOut) {
588
- try {
589
- await this.config.onSignOut();
590
- } catch (error) {
591
- if (error instanceof Error) {
592
- console.error("[InsforgeManager] Error clearing cookie:", error.message);
593
- }
594
- }
595
- }
596
- this.user = null;
597
- if (this.config.onAuthChange) {
598
- this.config.onAuthChange(null);
599
- }
600
- this.isLoaded = true;
601
- this.notifyListeners();
602
- return { success: false, error: "invalid_token" };
603
- }
604
- } catch (error) {
605
- console.error("[InsforgeManager] Token validation failed:", error);
606
- await this.sdk.auth.signOut();
607
- if (this.config.onSignOut) {
608
- try {
609
- await this.config.onSignOut();
610
- } catch (error2) {
611
- if (error2 instanceof Error) {
612
- console.error("[InsforgeManager] Error clearing cookie:", error2.message);
613
- }
614
- }
615
- }
616
- this.user = null;
617
- if (this.config.onAuthChange) {
618
- this.config.onAuthChange(null);
619
- }
620
- this.isLoaded = true;
621
- this.notifyListeners();
622
- return {
623
- success: false,
624
- error: error instanceof Error ? error.message : "Authentication failed"
625
- };
626
- }
627
- }
628
- // Helper to handle auth success
616
+ // Helper to handle auth success (for signIn/signUp methods)
629
617
  async handleAuthSuccess(authToken, fallbackUser) {
630
618
  const userResult = await this.sdk.auth.getCurrentUser();
631
619
  if (userResult.data) {
@@ -712,24 +700,10 @@ var InsforgeManager = class _InsforgeManager {
712
700
  }
713
701
  async signOut() {
714
702
  await this.sdk.auth.signOut();
715
- if (this.config.onSignOut) {
716
- try {
717
- await this.config.onSignOut();
718
- } catch (error) {
719
- if (error instanceof Error) {
720
- console.error("[InsforgeManager] Error clearing cookie:", error.message);
721
- }
722
- }
723
- }
724
703
  if (this.refreshIntervalRef) {
725
704
  clearInterval(this.refreshIntervalRef);
726
705
  this.refreshIntervalRef = null;
727
706
  }
728
- this.user = null;
729
- if (this.config.onAuthChange) {
730
- this.config.onAuthChange(null);
731
- }
732
- this.notifyListeners();
733
707
  }
734
708
  async updateUser(data) {
735
709
  if (!this.user) {
@@ -762,7 +736,24 @@ var InsforgeManager = class _InsforgeManager {
762
736
  return { error: error?.message || "Failed to update user" };
763
737
  }
764
738
  async reloadAuth() {
765
- return await this.loadAuthState();
739
+ try {
740
+ const sessionResult = this.sdk.auth.getCurrentSession();
741
+ const token2 = sessionResult.data?.session?.accessToken;
742
+ if (token2) {
743
+ await this.loadFullUserProfile();
744
+ return { success: true };
745
+ } else {
746
+ this.user = null;
747
+ this.isLoaded = true;
748
+ this.notifyListeners();
749
+ return { success: false, error: "no_session" };
750
+ }
751
+ } catch (error) {
752
+ return {
753
+ success: false,
754
+ error: error instanceof Error ? error.message : "Failed to reload auth"
755
+ };
756
+ }
766
757
  }
767
758
  async sendVerificationEmail(email) {
768
759
  const sdkResult = await this.sdk.auth.sendVerificationEmail({ email });
@@ -835,9 +826,6 @@ var InsforgeManager = class _InsforgeManager {
835
826
  this.notifyListeners();
836
827
  }
837
828
  // Set initial state from server (for SSR hydration)
838
- // This is ONLY basic user info from cookies, not full profile
839
- // Following Clerk's pattern: initialState prevents hydration mismatch
840
- // but full user data is still loaded via initialize()
841
829
  setInitialState(initialState) {
842
830
  if (this.user !== void 0) {
843
831
  return;
@@ -864,6 +852,10 @@ var InsforgeManager = class _InsforgeManager {
864
852
  }
865
853
  // Cleanup
866
854
  cleanup() {
855
+ if (this.authSubscription) {
856
+ this.authSubscription.unsubscribe();
857
+ this.authSubscription = null;
858
+ }
867
859
  if (this.refreshIntervalRef) {
868
860
  clearInterval(this.refreshIntervalRef);
869
861
  this.refreshIntervalRef = null;
@@ -2220,7 +2212,6 @@ function InsforgeProviderCore({
2220
2212
  const unsubscribe = manager.subscribe((newState) => {
2221
2213
  setState(newState);
2222
2214
  });
2223
- void manager.initialize();
2224
2215
  return () => {
2225
2216
  unsubscribe();
2226
2217
  };