@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.mjs CHANGED
@@ -513,13 +513,19 @@ var Auth = class {
513
513
  * @internal
514
514
  */
515
515
  _switchToSecureStorage() {
516
- if (this.tokenManager.getStrategyId() === "secure") return;
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,6 +551,7 @@ 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();
550
557
  }
@@ -711,18 +718,25 @@ var Auth = class {
711
718
  * In modern mode, also calls backend to clear the refresh token cookie
712
719
  */
713
720
  async signOut() {
721
+ console.log("[InsForge:Auth] signOut() called");
722
+ console.log("[InsForge:Auth] signOut() stack trace:", new Error().stack);
714
723
  try {
715
724
  if (this.tokenManager.getStrategyId() === "secure") {
725
+ console.log("[InsForge:Auth] signOut() - calling backend /api/auth/logout");
716
726
  try {
717
727
  await this.http.post("/api/auth/logout");
718
- } catch {
728
+ console.log("[InsForge:Auth] signOut() - backend logout successful");
729
+ } catch (e) {
730
+ console.log("[InsForge:Auth] signOut() - backend logout failed (ignored):", e);
719
731
  }
720
732
  }
721
733
  this.tokenManager.clearSession();
722
734
  this.http.setAuthToken(null);
723
735
  this.clearAuthenticatedCookie();
736
+ console.log("[InsForge:Auth] signOut() - completed");
724
737
  return { error: null };
725
738
  } catch (error) {
739
+ console.error("[InsForge:Auth] signOut() - error:", error);
726
740
  return {
727
741
  error: new InsForgeError(
728
742
  "Failed to sign out",
@@ -739,10 +753,12 @@ var Auth = class {
739
753
  * @returns New access token or throws an error
740
754
  */
741
755
  async refreshToken() {
756
+ console.log("[InsForge:Auth] refreshToken() called");
742
757
  try {
743
758
  const response = await this.http.post(
744
759
  "/api/auth/refresh"
745
760
  );
761
+ console.log("[InsForge:Auth] refreshToken() - response received, hasAccessToken:", !!response.accessToken);
746
762
  if (response.accessToken) {
747
763
  this._detectStorageFromResponse(response.sessionMode);
748
764
  this.tokenManager.setAccessToken(response.accessToken);
@@ -750,6 +766,7 @@ var Auth = class {
750
766
  if (response.user) {
751
767
  this.tokenManager.setUser(response.user);
752
768
  }
769
+ console.log("[InsForge:Auth] refreshToken() - success");
753
770
  return response.accessToken;
754
771
  }
755
772
  throw new InsForgeError(
@@ -758,8 +775,10 @@ var Auth = class {
758
775
  "REFRESH_FAILED"
759
776
  );
760
777
  } catch (error) {
778
+ console.error("[InsForge:Auth] refreshToken() - error:", error);
761
779
  if (error instanceof InsForgeError) {
762
780
  if (error.statusCode === 401 || error.statusCode === 403) {
781
+ console.log("[InsForge:Auth] refreshToken() - clearing session due to 401/403");
763
782
  this.tokenManager.clearSession();
764
783
  this.http.setAuthToken(null);
765
784
  this.clearAuthenticatedCookie();
@@ -818,12 +837,17 @@ var Auth = class {
818
837
  * to refresh the session if no access token is available (e.g., after page reload).
819
838
  */
820
839
  async getCurrentUser() {
840
+ console.log("[InsForge:Auth] getCurrentUser() called");
821
841
  try {
822
842
  let accessToken = this.tokenManager.getAccessToken();
823
- if (!accessToken && this.tokenManager.shouldAttemptRefresh()) {
843
+ const shouldRefresh = this.tokenManager.shouldAttemptRefresh();
844
+ console.log("[InsForge:Auth] getCurrentUser() - hasAccessToken:", !!accessToken, "shouldAttemptRefresh:", shouldRefresh);
845
+ if (!accessToken && shouldRefresh) {
846
+ console.log("[InsForge:Auth] getCurrentUser() - attempting refresh");
824
847
  try {
825
848
  accessToken = await this.refreshToken();
826
849
  } catch (error) {
850
+ console.log("[InsForge:Auth] getCurrentUser() - refresh failed:", error);
827
851
  if (error instanceof InsForgeError && (error.statusCode === 401 || error.statusCode === 403)) {
828
852
  return { data: null, error };
829
853
  }
@@ -831,14 +855,17 @@ var Auth = class {
831
855
  }
832
856
  }
833
857
  if (!accessToken) {
858
+ console.log("[InsForge:Auth] getCurrentUser() - no access token, returning null");
834
859
  return { data: null, error: null };
835
860
  }
836
861
  this.http.setAuthToken(accessToken);
862
+ console.log("[InsForge:Auth] getCurrentUser() - fetching user from API");
837
863
  const authResponse = await this.http.get("/api/auth/sessions/current");
838
864
  const { data: profile, error: profileError } = await this.database.from("users").select("*").eq("id", authResponse.user.id).single();
839
865
  if (profileError && profileError.code !== "PGRST116") {
840
866
  return { data: null, error: profileError };
841
867
  }
868
+ console.log("[InsForge:Auth] getCurrentUser() - success");
842
869
  return {
843
870
  data: {
844
871
  user: authResponse.user,
@@ -847,8 +874,12 @@ var Auth = class {
847
874
  error: null
848
875
  };
849
876
  } catch (error) {
877
+ console.error("[InsForge:Auth] getCurrentUser() - catch error:", error);
850
878
  if (error instanceof InsForgeError && error.statusCode === 401) {
851
- await this.signOut();
879
+ console.log("[InsForge:Auth] getCurrentUser() - 401 error, clearing local session only (NOT calling signOut)");
880
+ this.tokenManager.clearSession();
881
+ this.http.setAuthToken(null);
882
+ this.clearAuthenticatedCookie();
852
883
  return { data: null, error: null };
853
884
  }
854
885
  if (error instanceof InsForgeError) {
@@ -1656,9 +1687,14 @@ function hasAuthenticatedCookie() {
1656
1687
  }
1657
1688
  var InsForgeClient = class {
1658
1689
  constructor(config = {}) {
1690
+ console.log("[InsForge:Client] Initializing SDK");
1659
1691
  this.http = new HttpClient(config);
1660
1692
  this.tokenManager = new TokenManager(config.storage);
1661
- if (hasAuthenticatedCookie()) {
1693
+ const hasAuthCookie = hasAuthenticatedCookie();
1694
+ console.log("[InsForge:Client] hasAuthenticatedCookie:", hasAuthCookie);
1695
+ console.log("[InsForge:Client] document.cookie:", typeof document !== "undefined" ? document.cookie : "N/A (SSR)");
1696
+ if (hasAuthCookie) {
1697
+ console.log("[InsForge:Client] Switching to SecureSessionStorage");
1662
1698
  this.tokenManager.setStrategy(new SecureSessionStorage());
1663
1699
  }
1664
1700
  if (config.edgeFunctionToken) {
@@ -1670,25 +1706,31 @@ var InsForgeClient = class {
1670
1706
  });
1671
1707
  }
1672
1708
  this.http.setRefreshCallback(async () => {
1709
+ console.log("[InsForge:Client] HTTP 401 refresh callback triggered");
1673
1710
  try {
1674
1711
  return await this.auth.refreshToken();
1675
- } catch {
1712
+ } catch (e) {
1713
+ console.log("[InsForge:Client] Refresh callback failed:", e);
1676
1714
  if (this.tokenManager.getStrategyId() === "secure") {
1715
+ console.log("[InsForge:Client] Falling back to LocalSessionStorage");
1677
1716
  this.auth._switchToLocalStorage();
1678
1717
  }
1679
1718
  return null;
1680
1719
  }
1681
1720
  });
1682
1721
  const existingSession = this.tokenManager.getSession();
1722
+ console.log("[InsForge:Client] existingSession:", !!existingSession, "strategyId:", this.tokenManager.getStrategyId());
1683
1723
  if (existingSession?.accessToken) {
1684
1724
  this.http.setAuthToken(existingSession.accessToken);
1685
1725
  } else if (this.tokenManager.getStrategyId() === "secure") {
1726
+ console.log("[InsForge:Client] Secure mode, no session in memory - will refresh on first API call");
1686
1727
  }
1687
1728
  this.auth = new Auth(this.http, this.tokenManager);
1688
1729
  this.database = new Database(this.http, this.tokenManager);
1689
1730
  this.storage = new Storage(this.http);
1690
1731
  this.ai = new AI(this.http);
1691
1732
  this.functions = new Functions(this.http);
1733
+ console.log("[InsForge:Client] SDK initialized");
1692
1734
  }
1693
1735
  /**
1694
1736
  * Get the underlying HTTP client for custom requests