@insforge/sdk 1.0.1-refresh.1 → 1.0.1-refresh.2

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
@@ -140,9 +140,11 @@ var HttpClient = class {
140
140
  this.isRefreshing = true;
141
141
  try {
142
142
  const newToken = await this.refreshCallback();
143
- this.refreshQueue.forEach(({ resolve }) => {
143
+ this.refreshQueue.forEach(({ resolve, reject }) => {
144
144
  if (newToken) {
145
145
  resolve(newToken);
146
+ } else {
147
+ reject(new Error("Token refresh failed"));
146
148
  }
147
149
  });
148
150
  this.refreshQueue = [];
@@ -533,6 +535,28 @@ var Auth = class {
533
535
  this.initPromise = null;
534
536
  this.database = new Database(http, tokenManager);
535
537
  }
538
+ /**
539
+ * Check if an error represents an authentication failure
540
+ * Used to determine appropriate HTTP status code (401 vs 500)
541
+ */
542
+ isAuthenticationError(error) {
543
+ if (error instanceof Error) {
544
+ const message = error.message.toLowerCase();
545
+ const authKeywords = [
546
+ "unauthorized",
547
+ "invalid token",
548
+ "expired token",
549
+ "token expired",
550
+ "invalid refresh token",
551
+ "refresh token",
552
+ "authentication",
553
+ "not authenticated",
554
+ "session expired"
555
+ ];
556
+ return authKeywords.some((keyword) => message.includes(keyword));
557
+ }
558
+ return false;
559
+ }
536
560
  /**
537
561
  * Set the initialization promise that auth operations should wait for
538
562
  * This ensures TokenManager mode is set before any auth operations
@@ -751,14 +775,22 @@ var Auth = class {
751
775
  "REFRESH_FAILED"
752
776
  );
753
777
  } catch (error) {
754
- this.tokenManager.clearSession();
755
- this.http.setAuthToken(null);
756
778
  if (error instanceof InsForgeError) {
779
+ if (error.statusCode === 401 || error.statusCode === 403) {
780
+ this.tokenManager.clearSession();
781
+ this.http.setAuthToken(null);
782
+ }
757
783
  throw error;
758
784
  }
785
+ const errorMessage = error instanceof Error ? error.message : "Token refresh failed";
786
+ const isAuthError = this.isAuthenticationError(error);
787
+ if (isAuthError) {
788
+ this.tokenManager.clearSession();
789
+ this.http.setAuthToken(null);
790
+ }
759
791
  throw new InsForgeError(
760
- error instanceof Error ? error.message : "Token refresh failed",
761
- 401,
792
+ errorMessage,
793
+ isAuthError ? 401 : 500,
762
794
  "REFRESH_FAILED"
763
795
  );
764
796
  }