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