@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.js
CHANGED
|
@@ -552,13 +552,19 @@ var Auth = class {
|
|
|
552
552
|
* @internal
|
|
553
553
|
*/
|
|
554
554
|
_switchToSecureStorage() {
|
|
555
|
-
|
|
555
|
+
console.log("[InsForge:Auth] _switchToSecureStorage() called, current strategy:", this.tokenManager.getStrategyId());
|
|
556
|
+
if (this.tokenManager.getStrategyId() === "secure") {
|
|
557
|
+
console.log("[InsForge:Auth] _switchToSecureStorage() - already in secure mode, skipping");
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
556
560
|
const currentSession = this.tokenManager.getSession();
|
|
557
561
|
this.tokenManager.setStrategy(new SecureSessionStorage());
|
|
558
562
|
if (typeof localStorage !== "undefined") {
|
|
563
|
+
console.log("[InsForge:Auth] _switchToSecureStorage() - clearing localStorage");
|
|
559
564
|
localStorage.removeItem(TOKEN_KEY);
|
|
560
565
|
localStorage.removeItem(USER_KEY);
|
|
561
566
|
}
|
|
567
|
+
console.log("[InsForge:Auth] _switchToSecureStorage() - setting isAuthenticated cookie");
|
|
562
568
|
this.setAuthenticatedCookie();
|
|
563
569
|
if (currentSession) {
|
|
564
570
|
this.tokenManager.saveSession(currentSession);
|
|
@@ -584,8 +590,11 @@ var Auth = class {
|
|
|
584
590
|
* @internal
|
|
585
591
|
*/
|
|
586
592
|
_detectStorageFromResponse(sessionMode) {
|
|
593
|
+
console.log("[InsForge:Auth] _detectStorageFromResponse() - sessionMode:", sessionMode);
|
|
587
594
|
if (sessionMode === "secure") {
|
|
588
595
|
this._switchToSecureStorage();
|
|
596
|
+
} else {
|
|
597
|
+
this._switchToLocalStorage();
|
|
589
598
|
}
|
|
590
599
|
}
|
|
591
600
|
/**
|
|
@@ -750,18 +759,25 @@ var Auth = class {
|
|
|
750
759
|
* In modern mode, also calls backend to clear the refresh token cookie
|
|
751
760
|
*/
|
|
752
761
|
async signOut() {
|
|
762
|
+
console.log("[InsForge:Auth] signOut() called");
|
|
763
|
+
console.log("[InsForge:Auth] signOut() stack trace:", new Error().stack);
|
|
753
764
|
try {
|
|
754
765
|
if (this.tokenManager.getStrategyId() === "secure") {
|
|
766
|
+
console.log("[InsForge:Auth] signOut() - calling backend /api/auth/logout");
|
|
755
767
|
try {
|
|
756
768
|
await this.http.post("/api/auth/logout");
|
|
757
|
-
|
|
769
|
+
console.log("[InsForge:Auth] signOut() - backend logout successful");
|
|
770
|
+
} catch (e) {
|
|
771
|
+
console.log("[InsForge:Auth] signOut() - backend logout failed (ignored):", e);
|
|
758
772
|
}
|
|
759
773
|
}
|
|
760
774
|
this.tokenManager.clearSession();
|
|
761
775
|
this.http.setAuthToken(null);
|
|
762
776
|
this.clearAuthenticatedCookie();
|
|
777
|
+
console.log("[InsForge:Auth] signOut() - completed");
|
|
763
778
|
return { error: null };
|
|
764
779
|
} catch (error) {
|
|
780
|
+
console.error("[InsForge:Auth] signOut() - error:", error);
|
|
765
781
|
return {
|
|
766
782
|
error: new InsForgeError(
|
|
767
783
|
"Failed to sign out",
|
|
@@ -778,10 +794,12 @@ var Auth = class {
|
|
|
778
794
|
* @returns New access token or throws an error
|
|
779
795
|
*/
|
|
780
796
|
async refreshToken() {
|
|
797
|
+
console.log("[InsForge:Auth] refreshToken() called");
|
|
781
798
|
try {
|
|
782
799
|
const response = await this.http.post(
|
|
783
800
|
"/api/auth/refresh"
|
|
784
801
|
);
|
|
802
|
+
console.log("[InsForge:Auth] refreshToken() - response received, hasAccessToken:", !!response.accessToken);
|
|
785
803
|
if (response.accessToken) {
|
|
786
804
|
this._detectStorageFromResponse(response.sessionMode);
|
|
787
805
|
this.tokenManager.setAccessToken(response.accessToken);
|
|
@@ -789,6 +807,7 @@ var Auth = class {
|
|
|
789
807
|
if (response.user) {
|
|
790
808
|
this.tokenManager.setUser(response.user);
|
|
791
809
|
}
|
|
810
|
+
console.log("[InsForge:Auth] refreshToken() - success");
|
|
792
811
|
return response.accessToken;
|
|
793
812
|
}
|
|
794
813
|
throw new InsForgeError(
|
|
@@ -797,8 +816,16 @@ var Auth = class {
|
|
|
797
816
|
"REFRESH_FAILED"
|
|
798
817
|
);
|
|
799
818
|
} catch (error) {
|
|
819
|
+
console.error("[InsForge:Auth] refreshToken() - error:", error);
|
|
800
820
|
if (error instanceof InsForgeError) {
|
|
821
|
+
if (error.statusCode === 404) {
|
|
822
|
+
console.log("[InsForge:Auth] refreshToken() - 404 detected, backend does not support refresh endpoint");
|
|
823
|
+
console.log("[InsForge:Auth] refreshToken() - switching to LocalSessionStorage for backward compatibility");
|
|
824
|
+
this._switchToLocalStorage();
|
|
825
|
+
this.clearAuthenticatedCookie();
|
|
826
|
+
}
|
|
801
827
|
if (error.statusCode === 401 || error.statusCode === 403) {
|
|
828
|
+
console.log("[InsForge:Auth] refreshToken() - clearing session due to 401/403");
|
|
802
829
|
this.tokenManager.clearSession();
|
|
803
830
|
this.http.setAuthToken(null);
|
|
804
831
|
this.clearAuthenticatedCookie();
|
|
@@ -857,12 +884,17 @@ var Auth = class {
|
|
|
857
884
|
* to refresh the session if no access token is available (e.g., after page reload).
|
|
858
885
|
*/
|
|
859
886
|
async getCurrentUser() {
|
|
887
|
+
console.log("[InsForge:Auth] getCurrentUser() called");
|
|
860
888
|
try {
|
|
861
889
|
let accessToken = this.tokenManager.getAccessToken();
|
|
862
|
-
|
|
890
|
+
const shouldRefresh = this.tokenManager.shouldAttemptRefresh();
|
|
891
|
+
console.log("[InsForge:Auth] getCurrentUser() - hasAccessToken:", !!accessToken, "shouldAttemptRefresh:", shouldRefresh);
|
|
892
|
+
if (!accessToken && shouldRefresh) {
|
|
893
|
+
console.log("[InsForge:Auth] getCurrentUser() - attempting refresh");
|
|
863
894
|
try {
|
|
864
895
|
accessToken = await this.refreshToken();
|
|
865
896
|
} catch (error) {
|
|
897
|
+
console.log("[InsForge:Auth] getCurrentUser() - refresh failed:", error);
|
|
866
898
|
if (error instanceof InsForgeError && (error.statusCode === 401 || error.statusCode === 403)) {
|
|
867
899
|
return { data: null, error };
|
|
868
900
|
}
|
|
@@ -870,14 +902,17 @@ var Auth = class {
|
|
|
870
902
|
}
|
|
871
903
|
}
|
|
872
904
|
if (!accessToken) {
|
|
905
|
+
console.log("[InsForge:Auth] getCurrentUser() - no access token, returning null");
|
|
873
906
|
return { data: null, error: null };
|
|
874
907
|
}
|
|
875
908
|
this.http.setAuthToken(accessToken);
|
|
909
|
+
console.log("[InsForge:Auth] getCurrentUser() - fetching user from API");
|
|
876
910
|
const authResponse = await this.http.get("/api/auth/sessions/current");
|
|
877
911
|
const { data: profile, error: profileError } = await this.database.from("users").select("*").eq("id", authResponse.user.id).single();
|
|
878
912
|
if (profileError && profileError.code !== "PGRST116") {
|
|
879
913
|
return { data: null, error: profileError };
|
|
880
914
|
}
|
|
915
|
+
console.log("[InsForge:Auth] getCurrentUser() - success");
|
|
881
916
|
return {
|
|
882
917
|
data: {
|
|
883
918
|
user: authResponse.user,
|
|
@@ -886,8 +921,12 @@ var Auth = class {
|
|
|
886
921
|
error: null
|
|
887
922
|
};
|
|
888
923
|
} catch (error) {
|
|
924
|
+
console.error("[InsForge:Auth] getCurrentUser() - catch error:", error);
|
|
889
925
|
if (error instanceof InsForgeError && error.statusCode === 401) {
|
|
890
|
-
|
|
926
|
+
console.log("[InsForge:Auth] getCurrentUser() - 401 error, clearing local session only (NOT calling signOut)");
|
|
927
|
+
this.tokenManager.clearSession();
|
|
928
|
+
this.http.setAuthToken(null);
|
|
929
|
+
this.clearAuthenticatedCookie();
|
|
891
930
|
return { data: null, error: null };
|
|
892
931
|
}
|
|
893
932
|
if (error instanceof InsForgeError) {
|
|
@@ -1695,9 +1734,14 @@ function hasAuthenticatedCookie() {
|
|
|
1695
1734
|
}
|
|
1696
1735
|
var InsForgeClient = class {
|
|
1697
1736
|
constructor(config = {}) {
|
|
1737
|
+
console.log("[InsForge:Client] Initializing SDK");
|
|
1698
1738
|
this.http = new HttpClient(config);
|
|
1699
1739
|
this.tokenManager = new TokenManager(config.storage);
|
|
1700
|
-
|
|
1740
|
+
const hasAuthCookie = hasAuthenticatedCookie();
|
|
1741
|
+
console.log("[InsForge:Client] hasAuthenticatedCookie:", hasAuthCookie);
|
|
1742
|
+
console.log("[InsForge:Client] document.cookie:", typeof document !== "undefined" ? document.cookie : "N/A (SSR)");
|
|
1743
|
+
if (hasAuthCookie) {
|
|
1744
|
+
console.log("[InsForge:Client] Switching to SecureSessionStorage");
|
|
1701
1745
|
this.tokenManager.setStrategy(new SecureSessionStorage());
|
|
1702
1746
|
}
|
|
1703
1747
|
if (config.edgeFunctionToken) {
|
|
@@ -1709,25 +1753,31 @@ var InsForgeClient = class {
|
|
|
1709
1753
|
});
|
|
1710
1754
|
}
|
|
1711
1755
|
this.http.setRefreshCallback(async () => {
|
|
1756
|
+
console.log("[InsForge:Client] HTTP 401 refresh callback triggered");
|
|
1712
1757
|
try {
|
|
1713
1758
|
return await this.auth.refreshToken();
|
|
1714
|
-
} catch {
|
|
1759
|
+
} catch (e) {
|
|
1760
|
+
console.log("[InsForge:Client] Refresh callback failed:", e);
|
|
1715
1761
|
if (this.tokenManager.getStrategyId() === "secure") {
|
|
1762
|
+
console.log("[InsForge:Client] Falling back to LocalSessionStorage");
|
|
1716
1763
|
this.auth._switchToLocalStorage();
|
|
1717
1764
|
}
|
|
1718
1765
|
return null;
|
|
1719
1766
|
}
|
|
1720
1767
|
});
|
|
1721
1768
|
const existingSession = this.tokenManager.getSession();
|
|
1769
|
+
console.log("[InsForge:Client] existingSession:", !!existingSession, "strategyId:", this.tokenManager.getStrategyId());
|
|
1722
1770
|
if (existingSession?.accessToken) {
|
|
1723
1771
|
this.http.setAuthToken(existingSession.accessToken);
|
|
1724
1772
|
} else if (this.tokenManager.getStrategyId() === "secure") {
|
|
1773
|
+
console.log("[InsForge:Client] Secure mode, no session in memory - will refresh on first API call");
|
|
1725
1774
|
}
|
|
1726
1775
|
this.auth = new Auth(this.http, this.tokenManager);
|
|
1727
1776
|
this.database = new Database(this.http, this.tokenManager);
|
|
1728
1777
|
this.storage = new Storage(this.http);
|
|
1729
1778
|
this.ai = new AI(this.http);
|
|
1730
1779
|
this.functions = new Functions(this.http);
|
|
1780
|
+
console.log("[InsForge:Client] SDK initialized");
|
|
1731
1781
|
}
|
|
1732
1782
|
/**
|
|
1733
1783
|
* Get the underlying HTTP client for custom requests
|