@insforge/sdk 1.0.1-refresh.5 → 1.0.1-refresh.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 +56 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +56 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -513,13 +513,19 @@ var Auth = class {
|
|
|
513
513
|
* @internal
|
|
514
514
|
*/
|
|
515
515
|
_switchToSecureStorage() {
|
|
516
|
-
|
|
516
|
+
console.log("[InsForge:Auth] _switchToSecureStorage() called, current strategy:", this.tokenManager.getStrategyId());
|
|
517
|
+
if (this.tokenManager.getStrategyId() === "secure") {
|
|
518
|
+
console.log("[InsForge:Auth] _switchToSecureStorage() - already in secure mode, skipping");
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
517
521
|
const currentSession = this.tokenManager.getSession();
|
|
518
522
|
this.tokenManager.setStrategy(new SecureSessionStorage());
|
|
519
523
|
if (typeof localStorage !== "undefined") {
|
|
524
|
+
console.log("[InsForge:Auth] _switchToSecureStorage() - clearing localStorage");
|
|
520
525
|
localStorage.removeItem(TOKEN_KEY);
|
|
521
526
|
localStorage.removeItem(USER_KEY);
|
|
522
527
|
}
|
|
528
|
+
console.log("[InsForge:Auth] _switchToSecureStorage() - setting isAuthenticated cookie");
|
|
523
529
|
this.setAuthenticatedCookie();
|
|
524
530
|
if (currentSession) {
|
|
525
531
|
this.tokenManager.saveSession(currentSession);
|
|
@@ -545,8 +551,11 @@ var Auth = class {
|
|
|
545
551
|
* @internal
|
|
546
552
|
*/
|
|
547
553
|
_detectStorageFromResponse(sessionMode) {
|
|
554
|
+
console.log("[InsForge:Auth] _detectStorageFromResponse() - sessionMode:", sessionMode);
|
|
548
555
|
if (sessionMode === "secure") {
|
|
549
556
|
this._switchToSecureStorage();
|
|
557
|
+
} else {
|
|
558
|
+
this._switchToLocalStorage();
|
|
550
559
|
}
|
|
551
560
|
}
|
|
552
561
|
/**
|
|
@@ -711,18 +720,25 @@ var Auth = class {
|
|
|
711
720
|
* In modern mode, also calls backend to clear the refresh token cookie
|
|
712
721
|
*/
|
|
713
722
|
async signOut() {
|
|
723
|
+
console.log("[InsForge:Auth] signOut() called");
|
|
724
|
+
console.log("[InsForge:Auth] signOut() stack trace:", new Error().stack);
|
|
714
725
|
try {
|
|
715
726
|
if (this.tokenManager.getStrategyId() === "secure") {
|
|
727
|
+
console.log("[InsForge:Auth] signOut() - calling backend /api/auth/logout");
|
|
716
728
|
try {
|
|
717
729
|
await this.http.post("/api/auth/logout");
|
|
718
|
-
|
|
730
|
+
console.log("[InsForge:Auth] signOut() - backend logout successful");
|
|
731
|
+
} catch (e) {
|
|
732
|
+
console.log("[InsForge:Auth] signOut() - backend logout failed (ignored):", e);
|
|
719
733
|
}
|
|
720
734
|
}
|
|
721
735
|
this.tokenManager.clearSession();
|
|
722
736
|
this.http.setAuthToken(null);
|
|
723
737
|
this.clearAuthenticatedCookie();
|
|
738
|
+
console.log("[InsForge:Auth] signOut() - completed");
|
|
724
739
|
return { error: null };
|
|
725
740
|
} catch (error) {
|
|
741
|
+
console.error("[InsForge:Auth] signOut() - error:", error);
|
|
726
742
|
return {
|
|
727
743
|
error: new InsForgeError(
|
|
728
744
|
"Failed to sign out",
|
|
@@ -739,10 +755,12 @@ var Auth = class {
|
|
|
739
755
|
* @returns New access token or throws an error
|
|
740
756
|
*/
|
|
741
757
|
async refreshToken() {
|
|
758
|
+
console.log("[InsForge:Auth] refreshToken() called");
|
|
742
759
|
try {
|
|
743
760
|
const response = await this.http.post(
|
|
744
761
|
"/api/auth/refresh"
|
|
745
762
|
);
|
|
763
|
+
console.log("[InsForge:Auth] refreshToken() - response received, hasAccessToken:", !!response.accessToken);
|
|
746
764
|
if (response.accessToken) {
|
|
747
765
|
this._detectStorageFromResponse(response.sessionMode);
|
|
748
766
|
this.tokenManager.setAccessToken(response.accessToken);
|
|
@@ -750,6 +768,7 @@ var Auth = class {
|
|
|
750
768
|
if (response.user) {
|
|
751
769
|
this.tokenManager.setUser(response.user);
|
|
752
770
|
}
|
|
771
|
+
console.log("[InsForge:Auth] refreshToken() - success");
|
|
753
772
|
return response.accessToken;
|
|
754
773
|
}
|
|
755
774
|
throw new InsForgeError(
|
|
@@ -758,8 +777,16 @@ var Auth = class {
|
|
|
758
777
|
"REFRESH_FAILED"
|
|
759
778
|
);
|
|
760
779
|
} catch (error) {
|
|
780
|
+
console.error("[InsForge:Auth] refreshToken() - error:", error);
|
|
761
781
|
if (error instanceof InsForgeError) {
|
|
782
|
+
if (error.statusCode === 404) {
|
|
783
|
+
console.log("[InsForge:Auth] refreshToken() - 404 detected, backend does not support refresh endpoint");
|
|
784
|
+
console.log("[InsForge:Auth] refreshToken() - switching to LocalSessionStorage for backward compatibility");
|
|
785
|
+
this._switchToLocalStorage();
|
|
786
|
+
this.clearAuthenticatedCookie();
|
|
787
|
+
}
|
|
762
788
|
if (error.statusCode === 401 || error.statusCode === 403) {
|
|
789
|
+
console.log("[InsForge:Auth] refreshToken() - clearing session due to 401/403");
|
|
763
790
|
this.tokenManager.clearSession();
|
|
764
791
|
this.http.setAuthToken(null);
|
|
765
792
|
this.clearAuthenticatedCookie();
|
|
@@ -818,12 +845,17 @@ var Auth = class {
|
|
|
818
845
|
* to refresh the session if no access token is available (e.g., after page reload).
|
|
819
846
|
*/
|
|
820
847
|
async getCurrentUser() {
|
|
848
|
+
console.log("[InsForge:Auth] getCurrentUser() called");
|
|
821
849
|
try {
|
|
822
850
|
let accessToken = this.tokenManager.getAccessToken();
|
|
823
|
-
|
|
851
|
+
const shouldRefresh = this.tokenManager.shouldAttemptRefresh();
|
|
852
|
+
console.log("[InsForge:Auth] getCurrentUser() - hasAccessToken:", !!accessToken, "shouldAttemptRefresh:", shouldRefresh);
|
|
853
|
+
if (!accessToken && shouldRefresh) {
|
|
854
|
+
console.log("[InsForge:Auth] getCurrentUser() - attempting refresh");
|
|
824
855
|
try {
|
|
825
856
|
accessToken = await this.refreshToken();
|
|
826
857
|
} catch (error) {
|
|
858
|
+
console.log("[InsForge:Auth] getCurrentUser() - refresh failed:", error);
|
|
827
859
|
if (error instanceof InsForgeError && (error.statusCode === 401 || error.statusCode === 403)) {
|
|
828
860
|
return { data: null, error };
|
|
829
861
|
}
|
|
@@ -831,14 +863,17 @@ var Auth = class {
|
|
|
831
863
|
}
|
|
832
864
|
}
|
|
833
865
|
if (!accessToken) {
|
|
866
|
+
console.log("[InsForge:Auth] getCurrentUser() - no access token, returning null");
|
|
834
867
|
return { data: null, error: null };
|
|
835
868
|
}
|
|
836
869
|
this.http.setAuthToken(accessToken);
|
|
870
|
+
console.log("[InsForge:Auth] getCurrentUser() - fetching user from API");
|
|
837
871
|
const authResponse = await this.http.get("/api/auth/sessions/current");
|
|
838
872
|
const { data: profile, error: profileError } = await this.database.from("users").select("*").eq("id", authResponse.user.id).single();
|
|
839
873
|
if (profileError && profileError.code !== "PGRST116") {
|
|
840
874
|
return { data: null, error: profileError };
|
|
841
875
|
}
|
|
876
|
+
console.log("[InsForge:Auth] getCurrentUser() - success");
|
|
842
877
|
return {
|
|
843
878
|
data: {
|
|
844
879
|
user: authResponse.user,
|
|
@@ -847,8 +882,12 @@ var Auth = class {
|
|
|
847
882
|
error: null
|
|
848
883
|
};
|
|
849
884
|
} catch (error) {
|
|
885
|
+
console.error("[InsForge:Auth] getCurrentUser() - catch error:", error);
|
|
850
886
|
if (error instanceof InsForgeError && error.statusCode === 401) {
|
|
851
|
-
|
|
887
|
+
console.log("[InsForge:Auth] getCurrentUser() - 401 error, clearing local session only (NOT calling signOut)");
|
|
888
|
+
this.tokenManager.clearSession();
|
|
889
|
+
this.http.setAuthToken(null);
|
|
890
|
+
this.clearAuthenticatedCookie();
|
|
852
891
|
return { data: null, error: null };
|
|
853
892
|
}
|
|
854
893
|
if (error instanceof InsForgeError) {
|
|
@@ -1656,9 +1695,14 @@ function hasAuthenticatedCookie() {
|
|
|
1656
1695
|
}
|
|
1657
1696
|
var InsForgeClient = class {
|
|
1658
1697
|
constructor(config = {}) {
|
|
1698
|
+
console.log("[InsForge:Client] Initializing SDK");
|
|
1659
1699
|
this.http = new HttpClient(config);
|
|
1660
1700
|
this.tokenManager = new TokenManager(config.storage);
|
|
1661
|
-
|
|
1701
|
+
const hasAuthCookie = hasAuthenticatedCookie();
|
|
1702
|
+
console.log("[InsForge:Client] hasAuthenticatedCookie:", hasAuthCookie);
|
|
1703
|
+
console.log("[InsForge:Client] document.cookie:", typeof document !== "undefined" ? document.cookie : "N/A (SSR)");
|
|
1704
|
+
if (hasAuthCookie) {
|
|
1705
|
+
console.log("[InsForge:Client] Switching to SecureSessionStorage");
|
|
1662
1706
|
this.tokenManager.setStrategy(new SecureSessionStorage());
|
|
1663
1707
|
}
|
|
1664
1708
|
if (config.edgeFunctionToken) {
|
|
@@ -1670,25 +1714,31 @@ var InsForgeClient = class {
|
|
|
1670
1714
|
});
|
|
1671
1715
|
}
|
|
1672
1716
|
this.http.setRefreshCallback(async () => {
|
|
1717
|
+
console.log("[InsForge:Client] HTTP 401 refresh callback triggered");
|
|
1673
1718
|
try {
|
|
1674
1719
|
return await this.auth.refreshToken();
|
|
1675
|
-
} catch {
|
|
1720
|
+
} catch (e) {
|
|
1721
|
+
console.log("[InsForge:Client] Refresh callback failed:", e);
|
|
1676
1722
|
if (this.tokenManager.getStrategyId() === "secure") {
|
|
1723
|
+
console.log("[InsForge:Client] Falling back to LocalSessionStorage");
|
|
1677
1724
|
this.auth._switchToLocalStorage();
|
|
1678
1725
|
}
|
|
1679
1726
|
return null;
|
|
1680
1727
|
}
|
|
1681
1728
|
});
|
|
1682
1729
|
const existingSession = this.tokenManager.getSession();
|
|
1730
|
+
console.log("[InsForge:Client] existingSession:", !!existingSession, "strategyId:", this.tokenManager.getStrategyId());
|
|
1683
1731
|
if (existingSession?.accessToken) {
|
|
1684
1732
|
this.http.setAuthToken(existingSession.accessToken);
|
|
1685
1733
|
} else if (this.tokenManager.getStrategyId() === "secure") {
|
|
1734
|
+
console.log("[InsForge:Client] Secure mode, no session in memory - will refresh on first API call");
|
|
1686
1735
|
}
|
|
1687
1736
|
this.auth = new Auth(this.http, this.tokenManager);
|
|
1688
1737
|
this.database = new Database(this.http, this.tokenManager);
|
|
1689
1738
|
this.storage = new Storage(this.http);
|
|
1690
1739
|
this.ai = new AI(this.http);
|
|
1691
1740
|
this.functions = new Functions(this.http);
|
|
1741
|
+
console.log("[InsForge:Client] SDK initialized");
|
|
1692
1742
|
}
|
|
1693
1743
|
/**
|
|
1694
1744
|
* Get the underlying HTTP client for custom requests
|