@djangocfg/api 2.1.567 → 2.1.570

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/auth.cjs CHANGED
@@ -33,6 +33,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
33
33
  var auth_exports = {};
34
34
  __export(auth_exports, {
35
35
  AUTH_CONSTANTS: () => AUTH_CONSTANTS,
36
+ AUTH_ERROR_KEYS: () => AUTH_ERROR_KEYS,
36
37
  AccountsProvider: () => AccountsProvider,
37
38
  Analytics: () => Analytics,
38
39
  AnalyticsCategory: () => AnalyticsCategory,
@@ -51,9 +52,11 @@ __export(auth_exports, {
51
52
  hasValidCache: () => hasValidCache,
52
53
  isAllowedAuthPath: () => isAllowedAuthPath,
53
54
  isForbidden: () => isForbidden,
55
+ logAuthFailure: () => logAuthFailure,
54
56
  logger: () => logger,
55
57
  normalizePath: () => normalizePath,
56
58
  peekSavedRedirect: () => peekSavedRedirect,
59
+ resolveAuthError: () => resolveAuthError,
57
60
  resolveGuardIsAuthenticated: () => resolveGuardIsAuthenticated,
58
61
  resolveGuardIsLoading: () => resolveGuardIsLoading,
59
62
  setAnalyticsSink: () => setAnalyticsSink,
@@ -2568,6 +2571,67 @@ var Analytics = {
2568
2571
  }
2569
2572
  };
2570
2573
 
2574
+ // src/auth/utils/errors.ts
2575
+ var prop = /* @__PURE__ */ __name((value, key) => typeof value === "object" && value !== null ? value[key] : void 0, "prop");
2576
+ var TRANSPORT_FALLBACK = /^HTTP \d{3}:\s*/;
2577
+ var AUTH_ERROR_KEYS = {
2578
+ invalidCredentials: "ui.errors.unauthorized",
2579
+ rateLimited: "api.errors.rateLimited",
2580
+ serverUnavailable: "api.errors.serverUnavailable",
2581
+ networkError: "api.errors.networkError",
2582
+ generic: "ui.errors.generic"
2583
+ };
2584
+ var FALLBACK_COPY = {
2585
+ [AUTH_ERROR_KEYS.invalidCredentials]: "Please sign in to continue",
2586
+ [AUTH_ERROR_KEYS.rateLimited]: "Too many requests. Please wait.",
2587
+ [AUTH_ERROR_KEYS.serverUnavailable]: "Server is unavailable",
2588
+ [AUTH_ERROR_KEYS.networkError]: "Network error",
2589
+ [AUTH_ERROR_KEYS.generic]: "Something went wrong"
2590
+ };
2591
+ var keyForStatus = /* @__PURE__ */ __name((status) => {
2592
+ if (status === void 0) return AUTH_ERROR_KEYS.generic;
2593
+ if (status === 401 || status === 403) return AUTH_ERROR_KEYS.invalidCredentials;
2594
+ if (status === 404) return AUTH_ERROR_KEYS.serverUnavailable;
2595
+ if (status === 429) return AUTH_ERROR_KEYS.rateLimited;
2596
+ if (status >= 500) return AUTH_ERROR_KEYS.serverUnavailable;
2597
+ return AUTH_ERROR_KEYS.generic;
2598
+ }, "keyForStatus");
2599
+ var resolveAuthError = /* @__PURE__ */ __name((error, fallback) => {
2600
+ if (typeof error === "string" && error && !TRANSPORT_FALLBACK.test(error)) {
2601
+ return { message: error };
2602
+ }
2603
+ const candidates = [
2604
+ prop(error, "message"),
2605
+ prop(error, "detail"),
2606
+ prop(prop(error, "response"), "error"),
2607
+ prop(prop(error, "response"), "detail"),
2608
+ prop(prop(error, "response"), "message"),
2609
+ prop(prop(prop(error, "response"), "data"), "detail")
2610
+ ];
2611
+ for (const candidate of candidates) {
2612
+ if (typeof candidate === "string" && candidate && !TRANSPORT_FALLBACK.test(candidate)) {
2613
+ return { message: candidate };
2614
+ }
2615
+ }
2616
+ const rawStatus = prop(error, "statusCode") ?? prop(error, "status");
2617
+ const status = typeof rawStatus === "number" ? rawStatus : void 0;
2618
+ if (status === void 0 && fallback) {
2619
+ return { message: fallback };
2620
+ }
2621
+ const key = keyForStatus(status);
2622
+ return { message: FALLBACK_COPY[key], key };
2623
+ }, "resolveAuthError");
2624
+ var formatAuthError = /* @__PURE__ */ __name((error, fallback) => resolveAuthError(error, fallback).message, "formatAuthError");
2625
+ var logAuthFailure = /* @__PURE__ */ __name((log, message, error) => {
2626
+ const code = prop(error, "statusCode") ?? prop(error, "status");
2627
+ const isClientError = typeof code === "number" && code >= 400 && code < 500;
2628
+ if (isClientError) {
2629
+ log.warn(message, error);
2630
+ return;
2631
+ }
2632
+ log.error(message, error);
2633
+ }, "logAuthFailure");
2634
+
2571
2635
  // src/auth/hooks/useSessionStorage.ts
2572
2636
  var import_react7 = require("react");
2573
2637
 
@@ -2788,18 +2852,6 @@ var useAuthRedirectManager = /* @__PURE__ */ __name((options = {}) => {
2788
2852
 
2789
2853
  // src/auth/hooks/twoFactor.shared.ts
2790
2854
  var TOTP_CODE_LENGTH = 6;
2791
- function extractTwoFactorError(err, fallback) {
2792
- if (err instanceof APIError) {
2793
- const body = err.response ?? null;
2794
- if (typeof body?.error === "string") return body.error;
2795
- if (typeof body?.detail === "string") return body.detail;
2796
- if (typeof body?.message === "string") return body.message;
2797
- return err.errorMessage ?? err.message;
2798
- }
2799
- if (err instanceof Error) return err.message;
2800
- return fallback;
2801
- }
2802
- __name(extractTwoFactorError, "extractTwoFactorError");
2803
2855
  function extractAttemptsRemaining(err) {
2804
2856
  if (err instanceof APIError) {
2805
2857
  const body = err.response ?? null;
@@ -2875,8 +2927,8 @@ var useTwoFactorVerify = /* @__PURE__ */ __name((options = {}) => {
2875
2927
  handleSuccess(response);
2876
2928
  return true;
2877
2929
  } catch (err) {
2878
- authLogger.error("2FA TOTP verification error:", err);
2879
- const errorMessage = extractTwoFactorError(err, "Invalid verification code");
2930
+ logAuthFailure(authLogger, "2FA TOTP verification failed:", err);
2931
+ const errorMessage = formatAuthError(err, "Invalid verification code");
2880
2932
  const attempts = extractAttemptsRemaining(err);
2881
2933
  if (attempts !== null) setAttemptsRemaining(attempts);
2882
2934
  setError(errorMessage);
@@ -2921,8 +2973,8 @@ var useTwoFactorVerify = /* @__PURE__ */ __name((options = {}) => {
2921
2973
  handleSuccess(response);
2922
2974
  return true;
2923
2975
  } catch (err) {
2924
- authLogger.error("2FA backup code verification error:", err);
2925
- const errorMessage = extractTwoFactorError(err, "Invalid backup code");
2976
+ logAuthFailure(authLogger, "2FA backup code verification failed:", err);
2977
+ const errorMessage = formatAuthError(err, "Invalid backup code");
2926
2978
  const attempts = extractAttemptsRemaining(err);
2927
2979
  if (attempts !== null) setAttemptsRemaining(attempts);
2928
2980
  setError(errorMessage);
@@ -3345,7 +3397,7 @@ var useGithubAuth = /* @__PURE__ */ __name((options = {}) => {
3345
3397
  }
3346
3398
  window.location.href = response.authorization_url;
3347
3399
  } catch (err) {
3348
- const errorMessage = err instanceof Error ? err.message : "Failed to start GitHub authentication";
3400
+ const errorMessage = formatAuthError(err, "Failed to start GitHub authentication");
3349
3401
  authLogger.error("GitHub OAuth start error:", err);
3350
3402
  setError(errorMessage);
3351
3403
  onError?.(errorMessage);
@@ -3408,7 +3460,7 @@ var useGithubAuth = /* @__PURE__ */ __name((options = {}) => {
3408
3460
  }
3409
3461
  return;
3410
3462
  } catch (err) {
3411
- const errorMessage = err instanceof Error ? err.message : "GitHub authentication failed";
3463
+ const errorMessage = formatAuthError(err, "GitHub authentication failed");
3412
3464
  authLogger.error("GitHub OAuth callback error:", err);
3413
3465
  setError(errorMessage);
3414
3466
  onError?.(errorMessage);
@@ -3470,8 +3522,8 @@ var useTwoFactorSetup = /* @__PURE__ */ __name((options = {}) => {
3470
3522
  authLogger.info("2FA setup initiated, expires in:", data.expiresIn, "seconds");
3471
3523
  return data;
3472
3524
  } catch (err) {
3473
- const errorMessage = extractTwoFactorError(err, "Failed to start 2FA setup");
3474
- authLogger.error("2FA setup error:", err);
3525
+ const errorMessage = formatAuthError(err, "Failed to start 2FA setup");
3526
+ logAuthFailure(authLogger, "2FA setup failed:", err);
3475
3527
  setError(errorMessage);
3476
3528
  setSetupStep("idle");
3477
3529
  onError?.(errorMessage);
@@ -3511,8 +3563,8 @@ var useTwoFactorSetup = /* @__PURE__ */ __name((options = {}) => {
3511
3563
  onComplete?.(codes);
3512
3564
  return codes;
3513
3565
  } catch (err) {
3514
- const errorMessage = extractTwoFactorError(err, "Invalid code. Please try again.");
3515
- authLogger.error("2FA setup confirmation error:", err);
3566
+ const errorMessage = formatAuthError(err, "Invalid code. Please try again.");
3567
+ logAuthFailure(authLogger, "2FA setup confirmation failed:", err);
3516
3568
  setError(errorMessage);
3517
3569
  setSetupStep("scanning");
3518
3570
  onError?.(errorMessage);
@@ -3563,8 +3615,8 @@ var useTwoFactorStatus = /* @__PURE__ */ __name(() => {
3563
3615
  setHas2FAEnabled(response.has_2fa_enabled);
3564
3616
  authLogger.info("2FA status:", response.has_2fa_enabled ? "enabled" : "disabled");
3565
3617
  } catch (err) {
3566
- const errorMessage = extractTwoFactorError(err, "Failed to fetch 2FA status");
3567
- authLogger.error("Failed to fetch 2FA status:", err);
3618
+ const errorMessage = formatAuthError(err, "Failed to fetch 2FA status");
3619
+ logAuthFailure(authLogger, "Failed to fetch 2FA status:", err);
3568
3620
  setError(errorMessage);
3569
3621
  } finally {
3570
3622
  setIsLoading(false);
@@ -3585,8 +3637,8 @@ var useTwoFactorStatus = /* @__PURE__ */ __name(() => {
3585
3637
  authLogger.info("2FA disabled successfully");
3586
3638
  return true;
3587
3639
  } catch (err) {
3588
- const errorMessage = extractTwoFactorError(err, "Invalid verification code");
3589
- authLogger.error("Failed to disable 2FA:", err);
3640
+ const errorMessage = formatAuthError(err, "Invalid verification code");
3641
+ logAuthFailure(authLogger, "Failed to disable 2FA:", err);
3590
3642
  setError(errorMessage);
3591
3643
  return false;
3592
3644
  } finally {
@@ -4764,10 +4816,6 @@ var resolveAuthRoutes = /* @__PURE__ */ __name((routes) => ({
4764
4816
  }), "resolveAuthRoutes");
4765
4817
  var EMAIL_STORAGE_KEY = "auth_email";
4766
4818
  var AuthContext = (0, import_react16.createContext)(void 0);
4767
- var apiErrorMessage = /* @__PURE__ */ __name((error) => {
4768
- const body = error.response;
4769
- return body?.error || body?.detail || body?.message || error.errorMessage;
4770
- }, "apiErrorMessage");
4771
4819
  var AuthProviderInternal = /* @__PURE__ */ __name(({ children, config }) => {
4772
4820
  const accounts = useAccountsContext();
4773
4821
  const session = useSession();
@@ -4895,18 +4943,18 @@ var AuthProviderInternal = /* @__PURE__ */ __name(({ children, config }) => {
4895
4943
  webmail: result.webmail ?? null
4896
4944
  };
4897
4945
  } catch (error) {
4898
- authLogger.error("Request OTP error:", error);
4946
+ logAuthFailure(authLogger, "Request OTP failed:", error);
4899
4947
  if (error instanceof APIError) {
4900
4948
  const body = error.response;
4901
4949
  const retryAfter = body?.retry_after ?? body?.retryAfter;
4902
4950
  return {
4903
4951
  success: false,
4904
4952
  statusCode: error.statusCode,
4905
- message: apiErrorMessage(error),
4953
+ message: formatAuthError(error),
4906
4954
  retryAfter: typeof retryAfter === "number" ? retryAfter : void 0
4907
4955
  };
4908
4956
  }
4909
- return { success: false, message: "Failed to send OTP" };
4957
+ return { success: false, message: formatAuthError(error, "Failed to send OTP") };
4910
4958
  }
4911
4959
  },
4912
4960
  []
@@ -4959,15 +5007,15 @@ var AuthProviderInternal = /* @__PURE__ */ __name(({ children, config }) => {
4959
5007
  should_prompt_2fa: result.should_prompt_2fa
4960
5008
  };
4961
5009
  } catch (error) {
4962
- authLogger.error("Verify OTP error:", error);
5010
+ logAuthFailure(authLogger, "Verify OTP failed:", error);
4963
5011
  Analytics.event("auth_otp_verify_fail" /* AUTH_OTP_VERIFY_FAIL */, {
4964
5012
  category: "auth" /* AUTH */,
4965
5013
  label: "email"
4966
5014
  });
4967
5015
  if (error instanceof APIError) {
4968
- return { success: false, message: apiErrorMessage(error) };
5016
+ return { success: false, message: formatAuthError(error) };
4969
5017
  }
4970
- return { success: false, message: "Failed to verify OTP" };
5018
+ return { success: false, message: formatAuthError(error, "Failed to verify OTP") };
4971
5019
  }
4972
5020
  },
4973
5021
  [setStoredEmail, redirectManager, router]
@@ -5018,7 +5066,7 @@ var AuthProviderInternal = /* @__PURE__ */ __name(({ children, config }) => {
5018
5066
  should_prompt_2fa: result.should_prompt_2fa
5019
5067
  };
5020
5068
  } catch (error) {
5021
- authLogger.error("Password login error:", error);
5069
+ logAuthFailure(authLogger, "Password login failed:", error);
5022
5070
  Analytics.event("auth_password_login_fail" /* AUTH_PASSWORD_LOGIN_FAIL */, {
5023
5071
  category: "auth" /* AUTH */,
5024
5072
  label: "password"
@@ -5028,12 +5076,12 @@ var AuthProviderInternal = /* @__PURE__ */ __name(({ children, config }) => {
5028
5076
  return {
5029
5077
  success: false,
5030
5078
  statusCode: error.statusCode,
5031
- message: apiErrorMessage(error),
5079
+ message: formatAuthError(error),
5032
5080
  errorCode: body?.error_code,
5033
5081
  retryAfter: typeof body?.retry_after === "number" ? body.retry_after : void 0
5034
5082
  };
5035
5083
  }
5036
- return { success: false, message: "Failed to sign in" };
5084
+ return { success: false, message: formatAuthError(error, "Failed to sign in") };
5037
5085
  }
5038
5086
  },
5039
5087
  [setStoredEmail, redirectManager, router]
@@ -5203,25 +5251,6 @@ var validateEmail = /* @__PURE__ */ __name((email) => {
5203
5251
  return emailRegex.test(email);
5204
5252
  }, "validateEmail");
5205
5253
 
5206
- // src/auth/utils/errors.ts
5207
- var prop = /* @__PURE__ */ __name((value, key) => typeof value === "object" && value !== null ? value[key] : void 0, "prop");
5208
- var formatAuthError = /* @__PURE__ */ __name((error) => {
5209
- if (typeof error === "string") {
5210
- return error;
5211
- }
5212
- const candidates = [
5213
- prop(error, "message"),
5214
- prop(error, "detail"),
5215
- prop(prop(prop(error, "response"), "data"), "detail")
5216
- ];
5217
- for (const candidate of candidates) {
5218
- if (typeof candidate === "string" && candidate) {
5219
- return candidate;
5220
- }
5221
- }
5222
- return "An unexpected error occurred";
5223
- }, "formatAuthError");
5224
-
5225
5254
  // src/auth/utils/guard.ts
5226
5255
  function isForbidden(s) {
5227
5256
  return Boolean(