@insforge/react 1.0.2-dev.5 → 1.0.2-dev.6
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.cjs +109 -111
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +109 -111
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -494,9 +494,9 @@ var InsforgeManager = class _InsforgeManager {
|
|
|
494
494
|
// State storage
|
|
495
495
|
user = void 0;
|
|
496
496
|
isLoaded = false;
|
|
497
|
+
isInitializing = false;
|
|
497
498
|
sdk;
|
|
498
499
|
listeners = /* @__PURE__ */ new Set();
|
|
499
|
-
authSubscription = null;
|
|
500
500
|
// Config
|
|
501
501
|
config;
|
|
502
502
|
refreshIntervalRef = null;
|
|
@@ -506,93 +506,6 @@ var InsforgeManager = class _InsforgeManager {
|
|
|
506
506
|
this.sdk = sdk.createClient({ baseUrl: config.baseUrl });
|
|
507
507
|
this.user = void 0;
|
|
508
508
|
this.isLoaded = false;
|
|
509
|
-
this.setupAuthSubscription();
|
|
510
|
-
}
|
|
511
|
-
// Setup subscription to SDK auth state changes
|
|
512
|
-
setupAuthSubscription() {
|
|
513
|
-
const { data } = this.sdk.auth.onAuthStateChange(
|
|
514
|
-
async (event, session) => {
|
|
515
|
-
await this.handleAuthStateChange(event, session);
|
|
516
|
-
}
|
|
517
|
-
);
|
|
518
|
-
this.authSubscription = data.subscription;
|
|
519
|
-
}
|
|
520
|
-
// Handle auth state changes from SDK
|
|
521
|
-
async handleAuthStateChange(event, session) {
|
|
522
|
-
switch (event) {
|
|
523
|
-
case "INITIAL_SESSION":
|
|
524
|
-
if (session?.accessToken) {
|
|
525
|
-
await this.loadFullUserProfile();
|
|
526
|
-
} else {
|
|
527
|
-
this.user = null;
|
|
528
|
-
this.isLoaded = true;
|
|
529
|
-
this.notifyListeners();
|
|
530
|
-
}
|
|
531
|
-
break;
|
|
532
|
-
case "SIGNED_IN":
|
|
533
|
-
if (session?.accessToken) {
|
|
534
|
-
await this.loadFullUserProfile();
|
|
535
|
-
if (this.config.onSignIn) {
|
|
536
|
-
try {
|
|
537
|
-
await this.config.onSignIn(session.accessToken);
|
|
538
|
-
} catch (error) {
|
|
539
|
-
console.error("[InsforgeManager] Error in onSignIn callback:", error);
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
break;
|
|
544
|
-
case "SIGNED_OUT":
|
|
545
|
-
this.user = null;
|
|
546
|
-
this.isLoaded = true;
|
|
547
|
-
if (this.config.onAuthChange) {
|
|
548
|
-
this.config.onAuthChange(null);
|
|
549
|
-
}
|
|
550
|
-
if (this.config.onSignOut) {
|
|
551
|
-
try {
|
|
552
|
-
await this.config.onSignOut();
|
|
553
|
-
} catch (error) {
|
|
554
|
-
console.error("[InsforgeManager] Error in onSignOut callback:", error);
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
this.notifyListeners();
|
|
558
|
-
break;
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
// Load full user profile from API
|
|
562
|
-
async loadFullUserProfile() {
|
|
563
|
-
try {
|
|
564
|
-
const userResult = await this.sdk.auth.getCurrentUser();
|
|
565
|
-
if (userResult.data) {
|
|
566
|
-
const profile = userResult.data.profile;
|
|
567
|
-
const userData = {
|
|
568
|
-
id: userResult.data.user.id,
|
|
569
|
-
email: userResult.data.user.email,
|
|
570
|
-
name: profile?.nickname || "",
|
|
571
|
-
avatarUrl: profile?.avatarUrl || ""
|
|
572
|
-
};
|
|
573
|
-
this.user = userData;
|
|
574
|
-
this.isLoaded = true;
|
|
575
|
-
if (this.config.onAuthChange) {
|
|
576
|
-
this.config.onAuthChange(userData);
|
|
577
|
-
}
|
|
578
|
-
this.notifyListeners();
|
|
579
|
-
} else {
|
|
580
|
-
this.user = null;
|
|
581
|
-
this.isLoaded = true;
|
|
582
|
-
if (this.config.onAuthChange) {
|
|
583
|
-
this.config.onAuthChange(null);
|
|
584
|
-
}
|
|
585
|
-
this.notifyListeners();
|
|
586
|
-
}
|
|
587
|
-
} catch (error) {
|
|
588
|
-
console.error("[InsforgeManager] Failed to load user profile:", error);
|
|
589
|
-
this.user = null;
|
|
590
|
-
this.isLoaded = true;
|
|
591
|
-
if (this.config.onAuthChange) {
|
|
592
|
-
this.config.onAuthChange(null);
|
|
593
|
-
}
|
|
594
|
-
this.notifyListeners();
|
|
595
|
-
}
|
|
596
509
|
}
|
|
597
510
|
// Public access method (Singleton core)
|
|
598
511
|
static getInstance(config) {
|
|
@@ -614,6 +527,19 @@ var InsforgeManager = class _InsforgeManager {
|
|
|
614
527
|
updateConfig(config) {
|
|
615
528
|
this.config = { ...this.config, ...config };
|
|
616
529
|
}
|
|
530
|
+
// Public initialization method
|
|
531
|
+
// Even if we have initialState (isLoaded=true from cookies), we still need to load full user data from SDK/API
|
|
532
|
+
async initialize() {
|
|
533
|
+
if (this.isInitializing) {
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
this.isInitializing = true;
|
|
537
|
+
try {
|
|
538
|
+
await this.loadAuthState();
|
|
539
|
+
} finally {
|
|
540
|
+
this.isInitializing = false;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
617
543
|
// Get current state
|
|
618
544
|
getState() {
|
|
619
545
|
const userId = this.user === void 0 ? void 0 : this.user === null ? null : this.user.id;
|
|
@@ -634,7 +560,83 @@ var InsforgeManager = class _InsforgeManager {
|
|
|
634
560
|
const state = this.getState();
|
|
635
561
|
this.listeners.forEach((listener) => listener(state));
|
|
636
562
|
}
|
|
637
|
-
//
|
|
563
|
+
// Load auth state
|
|
564
|
+
// This loads the FULL user data from SDK (including complete profile)
|
|
565
|
+
// Called after hydration to get complete user information beyond what's in cookies
|
|
566
|
+
async loadAuthState() {
|
|
567
|
+
try {
|
|
568
|
+
const sessionResult = this.sdk.auth.getCurrentSession();
|
|
569
|
+
const token2 = sessionResult.data?.session?.accessToken || null;
|
|
570
|
+
if (!token2) {
|
|
571
|
+
this.user = null;
|
|
572
|
+
if (this.config.onAuthChange) {
|
|
573
|
+
this.config.onAuthChange(null);
|
|
574
|
+
}
|
|
575
|
+
this.isLoaded = true;
|
|
576
|
+
this.notifyListeners();
|
|
577
|
+
return { success: false, error: "no_session" };
|
|
578
|
+
}
|
|
579
|
+
const userResult = await this.sdk.auth.getCurrentUser();
|
|
580
|
+
if (userResult.data) {
|
|
581
|
+
const profile = userResult.data.profile;
|
|
582
|
+
const userData = {
|
|
583
|
+
id: userResult.data.user.id,
|
|
584
|
+
email: userResult.data.user.email,
|
|
585
|
+
name: profile?.nickname || "",
|
|
586
|
+
avatarUrl: profile?.avatarUrl || ""
|
|
587
|
+
// You can add more profile fields here as needed
|
|
588
|
+
};
|
|
589
|
+
this.user = userData;
|
|
590
|
+
if (this.config.onAuthChange) {
|
|
591
|
+
this.config.onAuthChange(userData);
|
|
592
|
+
}
|
|
593
|
+
this.isLoaded = true;
|
|
594
|
+
this.notifyListeners();
|
|
595
|
+
return { success: true };
|
|
596
|
+
} else {
|
|
597
|
+
await this.sdk.auth.signOut();
|
|
598
|
+
if (this.config.onSignOut) {
|
|
599
|
+
try {
|
|
600
|
+
await this.config.onSignOut();
|
|
601
|
+
} catch (error) {
|
|
602
|
+
if (error instanceof Error) {
|
|
603
|
+
console.error("[InsforgeManager] Error clearing cookie:", error.message);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
this.user = null;
|
|
608
|
+
if (this.config.onAuthChange) {
|
|
609
|
+
this.config.onAuthChange(null);
|
|
610
|
+
}
|
|
611
|
+
this.isLoaded = true;
|
|
612
|
+
this.notifyListeners();
|
|
613
|
+
return { success: false, error: "invalid_token" };
|
|
614
|
+
}
|
|
615
|
+
} catch (error) {
|
|
616
|
+
console.error("[InsforgeManager] Token validation failed:", error);
|
|
617
|
+
await this.sdk.auth.signOut();
|
|
618
|
+
if (this.config.onSignOut) {
|
|
619
|
+
try {
|
|
620
|
+
await this.config.onSignOut();
|
|
621
|
+
} catch (error2) {
|
|
622
|
+
if (error2 instanceof Error) {
|
|
623
|
+
console.error("[InsforgeManager] Error clearing cookie:", error2.message);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
this.user = null;
|
|
628
|
+
if (this.config.onAuthChange) {
|
|
629
|
+
this.config.onAuthChange(null);
|
|
630
|
+
}
|
|
631
|
+
this.isLoaded = true;
|
|
632
|
+
this.notifyListeners();
|
|
633
|
+
return {
|
|
634
|
+
success: false,
|
|
635
|
+
error: error instanceof Error ? error.message : "Authentication failed"
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
// Helper to handle auth success
|
|
638
640
|
async handleAuthSuccess(authToken, fallbackUser) {
|
|
639
641
|
const userResult = await this.sdk.auth.getCurrentUser();
|
|
640
642
|
if (userResult.data) {
|
|
@@ -721,10 +723,24 @@ var InsforgeManager = class _InsforgeManager {
|
|
|
721
723
|
}
|
|
722
724
|
async signOut() {
|
|
723
725
|
await this.sdk.auth.signOut();
|
|
726
|
+
if (this.config.onSignOut) {
|
|
727
|
+
try {
|
|
728
|
+
await this.config.onSignOut();
|
|
729
|
+
} catch (error) {
|
|
730
|
+
if (error instanceof Error) {
|
|
731
|
+
console.error("[InsforgeManager] Error clearing cookie:", error.message);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
}
|
|
724
735
|
if (this.refreshIntervalRef) {
|
|
725
736
|
clearInterval(this.refreshIntervalRef);
|
|
726
737
|
this.refreshIntervalRef = null;
|
|
727
738
|
}
|
|
739
|
+
this.user = null;
|
|
740
|
+
if (this.config.onAuthChange) {
|
|
741
|
+
this.config.onAuthChange(null);
|
|
742
|
+
}
|
|
743
|
+
this.notifyListeners();
|
|
728
744
|
}
|
|
729
745
|
async updateUser(data) {
|
|
730
746
|
if (!this.user) {
|
|
@@ -757,24 +773,7 @@ var InsforgeManager = class _InsforgeManager {
|
|
|
757
773
|
return { error: error?.message || "Failed to update user" };
|
|
758
774
|
}
|
|
759
775
|
async reloadAuth() {
|
|
760
|
-
|
|
761
|
-
const sessionResult = this.sdk.auth.getCurrentSession();
|
|
762
|
-
const token2 = sessionResult.data?.session?.accessToken;
|
|
763
|
-
if (token2) {
|
|
764
|
-
await this.loadFullUserProfile();
|
|
765
|
-
return { success: true };
|
|
766
|
-
} else {
|
|
767
|
-
this.user = null;
|
|
768
|
-
this.isLoaded = true;
|
|
769
|
-
this.notifyListeners();
|
|
770
|
-
return { success: false, error: "no_session" };
|
|
771
|
-
}
|
|
772
|
-
} catch (error) {
|
|
773
|
-
return {
|
|
774
|
-
success: false,
|
|
775
|
-
error: error instanceof Error ? error.message : "Failed to reload auth"
|
|
776
|
-
};
|
|
777
|
-
}
|
|
776
|
+
return await this.loadAuthState();
|
|
778
777
|
}
|
|
779
778
|
async sendVerificationEmail(email) {
|
|
780
779
|
const sdkResult = await this.sdk.auth.sendVerificationEmail({ email });
|
|
@@ -847,6 +846,9 @@ var InsforgeManager = class _InsforgeManager {
|
|
|
847
846
|
this.notifyListeners();
|
|
848
847
|
}
|
|
849
848
|
// Set initial state from server (for SSR hydration)
|
|
849
|
+
// This is ONLY basic user info from cookies, not full profile
|
|
850
|
+
// Following Clerk's pattern: initialState prevents hydration mismatch
|
|
851
|
+
// but full user data is still loaded via initialize()
|
|
850
852
|
setInitialState(initialState) {
|
|
851
853
|
if (this.user !== void 0) {
|
|
852
854
|
return;
|
|
@@ -873,10 +875,6 @@ var InsforgeManager = class _InsforgeManager {
|
|
|
873
875
|
}
|
|
874
876
|
// Cleanup
|
|
875
877
|
cleanup() {
|
|
876
|
-
if (this.authSubscription) {
|
|
877
|
-
this.authSubscription.unsubscribe();
|
|
878
|
-
this.authSubscription = null;
|
|
879
|
-
}
|
|
880
878
|
if (this.refreshIntervalRef) {
|
|
881
879
|
clearInterval(this.refreshIntervalRef);
|
|
882
880
|
this.refreshIntervalRef = null;
|