@insforge/react 1.0.2-dev.5 → 1.0.2-dev.7

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;
476
477
  sdk;
477
478
  listeners = /* @__PURE__ */ new Set();
478
- authSubscription = null;
479
479
  // Config
480
480
  config;
481
481
  refreshIntervalRef = null;
@@ -485,93 +485,6 @@ 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
- }
575
488
  }
576
489
  // Public access method (Singleton core)
577
490
  static getInstance(config) {
@@ -593,6 +506,19 @@ var InsforgeManager = class _InsforgeManager {
593
506
  updateConfig(config) {
594
507
  this.config = { ...this.config, ...config };
595
508
  }
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.loadAuthState();
518
+ } finally {
519
+ this.isInitializing = false;
520
+ }
521
+ }
596
522
  // Get current state
597
523
  getState() {
598
524
  const userId = this.user === void 0 ? void 0 : this.user === null ? null : this.user.id;
@@ -613,7 +539,83 @@ var InsforgeManager = class _InsforgeManager {
613
539
  const state = this.getState();
614
540
  this.listeners.forEach((listener) => listener(state));
615
541
  }
616
- // Helper to handle auth success (for signIn/signUp methods)
542
+ // Load auth state
543
+ // This loads the FULL user data from SDK (including complete profile)
544
+ // Called after hydration to get complete user information beyond what's in cookies
545
+ async loadAuthState() {
546
+ try {
547
+ const sessionResult = this.sdk.auth.getCurrentSession();
548
+ const token2 = sessionResult.data?.session?.accessToken || null;
549
+ if (!token2) {
550
+ this.user = null;
551
+ if (this.config.onAuthChange) {
552
+ this.config.onAuthChange(null);
553
+ }
554
+ this.isLoaded = true;
555
+ this.notifyListeners();
556
+ return { success: false, error: "no_session" };
557
+ }
558
+ const userResult = await this.sdk.auth.getCurrentUser();
559
+ if (userResult.data) {
560
+ const profile = userResult.data.profile;
561
+ const userData = {
562
+ id: userResult.data.user.id,
563
+ email: userResult.data.user.email,
564
+ name: profile?.nickname || "",
565
+ avatarUrl: profile?.avatarUrl || ""
566
+ // You can add more profile fields here as needed
567
+ };
568
+ this.user = userData;
569
+ if (this.config.onAuthChange) {
570
+ this.config.onAuthChange(userData);
571
+ }
572
+ this.isLoaded = true;
573
+ this.notifyListeners();
574
+ return { success: true };
575
+ } else {
576
+ await this.sdk.auth.signOut();
577
+ if (this.config.onSignOut) {
578
+ try {
579
+ await this.config.onSignOut();
580
+ } catch (error) {
581
+ if (error instanceof Error) {
582
+ console.error("[InsforgeManager] Error clearing cookie:", error.message);
583
+ }
584
+ }
585
+ }
586
+ this.user = null;
587
+ if (this.config.onAuthChange) {
588
+ this.config.onAuthChange(null);
589
+ }
590
+ this.isLoaded = true;
591
+ this.notifyListeners();
592
+ return { success: false, error: "invalid_token" };
593
+ }
594
+ } catch (error) {
595
+ console.error("[InsforgeManager] Token validation failed:", error);
596
+ await this.sdk.auth.signOut();
597
+ if (this.config.onSignOut) {
598
+ try {
599
+ await this.config.onSignOut();
600
+ } catch (error2) {
601
+ if (error2 instanceof Error) {
602
+ console.error("[InsforgeManager] Error clearing cookie:", error2.message);
603
+ }
604
+ }
605
+ }
606
+ this.user = null;
607
+ if (this.config.onAuthChange) {
608
+ this.config.onAuthChange(null);
609
+ }
610
+ this.isLoaded = true;
611
+ this.notifyListeners();
612
+ return {
613
+ success: false,
614
+ error: error instanceof Error ? error.message : "Authentication failed"
615
+ };
616
+ }
617
+ }
618
+ // Helper to handle auth success
617
619
  async handleAuthSuccess(authToken, fallbackUser) {
618
620
  const userResult = await this.sdk.auth.getCurrentUser();
619
621
  if (userResult.data) {
@@ -700,10 +702,24 @@ var InsforgeManager = class _InsforgeManager {
700
702
  }
701
703
  async signOut() {
702
704
  await this.sdk.auth.signOut();
705
+ if (this.config.onSignOut) {
706
+ try {
707
+ await this.config.onSignOut();
708
+ } catch (error) {
709
+ if (error instanceof Error) {
710
+ console.error("[InsforgeManager] Error clearing cookie:", error.message);
711
+ }
712
+ }
713
+ }
703
714
  if (this.refreshIntervalRef) {
704
715
  clearInterval(this.refreshIntervalRef);
705
716
  this.refreshIntervalRef = null;
706
717
  }
718
+ this.user = null;
719
+ if (this.config.onAuthChange) {
720
+ this.config.onAuthChange(null);
721
+ }
722
+ this.notifyListeners();
707
723
  }
708
724
  async updateUser(data) {
709
725
  if (!this.user) {
@@ -736,24 +752,7 @@ var InsforgeManager = class _InsforgeManager {
736
752
  return { error: error?.message || "Failed to update user" };
737
753
  }
738
754
  async reloadAuth() {
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
- }
755
+ return await this.loadAuthState();
757
756
  }
758
757
  async sendVerificationEmail(email) {
759
758
  const sdkResult = await this.sdk.auth.sendVerificationEmail({ email });
@@ -826,6 +825,9 @@ var InsforgeManager = class _InsforgeManager {
826
825
  this.notifyListeners();
827
826
  }
828
827
  // Set initial state from server (for SSR hydration)
828
+ // This is ONLY basic user info from cookies, not full profile
829
+ // Following Clerk's pattern: initialState prevents hydration mismatch
830
+ // but full user data is still loaded via initialize()
829
831
  setInitialState(initialState) {
830
832
  if (this.user !== void 0) {
831
833
  return;
@@ -852,10 +854,6 @@ var InsforgeManager = class _InsforgeManager {
852
854
  }
853
855
  // Cleanup
854
856
  cleanup() {
855
- if (this.authSubscription) {
856
- this.authSubscription.unsubscribe();
857
- this.authSubscription = null;
858
- }
859
857
  if (this.refreshIntervalRef) {
860
858
  clearInterval(this.refreshIntervalRef);
861
859
  this.refreshIntervalRef = null;
@@ -2212,6 +2210,7 @@ function InsforgeProviderCore({
2212
2210
  const unsubscribe = manager.subscribe((newState) => {
2213
2211
  setState(newState);
2214
2212
  });
2213
+ void manager.initialize();
2215
2214
  return () => {
2216
2215
  unsubscribe();
2217
2216
  };