@djangocfg/api 2.1.459 → 2.1.462

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
@@ -74,6 +74,7 @@ __export(auth_exports, {
74
74
  useTwoFactor: () => useTwoFactor,
75
75
  useTwoFactorSetup: () => useTwoFactorSetup,
76
76
  useTwoFactorStatus: () => useTwoFactorStatus,
77
+ useTwoFactorVerify: () => useTwoFactorVerify,
77
78
  validateEmail: () => validateEmail,
78
79
  validateIdentifier: () => validateIdentifier
79
80
  });
@@ -1149,7 +1150,7 @@ var useAutoAuth = /* @__PURE__ */ __name((options = {}) => {
1149
1150
  };
1150
1151
  }, "useAutoAuth");
1151
1152
 
1152
- // src/auth/hooks/useTwoFactor.ts
1153
+ // src/auth/hooks/useTwoFactorVerify.ts
1153
1154
  var import_react8 = require("react");
1154
1155
 
1155
1156
  // src/_api/generated/core/bodySerializer.gen.ts
@@ -2677,8 +2678,35 @@ var useAuthRedirectManager = /* @__PURE__ */ __name((options = {}) => {
2677
2678
  };
2678
2679
  }, "useAuthRedirectManager");
2679
2680
 
2680
- // src/auth/hooks/useTwoFactor.ts
2681
- var useTwoFactor = /* @__PURE__ */ __name((options = {}) => {
2681
+ // src/auth/hooks/twoFactor.shared.ts
2682
+ var TOTP_CODE_LENGTH = 6;
2683
+ function extractTwoFactorError(err, fallback) {
2684
+ if (err instanceof APIError) {
2685
+ const body = err.response ?? null;
2686
+ if (typeof body?.error === "string") return body.error;
2687
+ if (typeof body?.detail === "string") return body.detail;
2688
+ if (typeof body?.message === "string") return body.message;
2689
+ return err.errorMessage ?? err.message;
2690
+ }
2691
+ if (err instanceof Error) return err.message;
2692
+ return fallback;
2693
+ }
2694
+ __name(extractTwoFactorError, "extractTwoFactorError");
2695
+ function extractAttemptsRemaining(err) {
2696
+ if (err instanceof APIError) {
2697
+ const body = err.response ?? null;
2698
+ if (typeof body?.attempts_remaining === "number") return body.attempts_remaining;
2699
+ }
2700
+ return null;
2701
+ }
2702
+ __name(extractAttemptsRemaining, "extractAttemptsRemaining");
2703
+ function isValidTotpCode(code) {
2704
+ return !!code && code.length === TOTP_CODE_LENGTH;
2705
+ }
2706
+ __name(isValidTotpCode, "isValidTotpCode");
2707
+
2708
+ // src/auth/hooks/useTwoFactorVerify.ts
2709
+ var useTwoFactorVerify = /* @__PURE__ */ __name((options = {}) => {
2682
2710
  const { onSuccess, onError, redirectUrl, skipRedirect = false } = options;
2683
2711
  const router = useCfgRouter();
2684
2712
  const [isLoading, setIsLoading] = (0, import_react8.useState)(false);
@@ -2718,7 +2746,7 @@ var useTwoFactor = /* @__PURE__ */ __name((options = {}) => {
2718
2746
  onError?.(msg);
2719
2747
  return false;
2720
2748
  }
2721
- if (!code || code.length !== 6) {
2749
+ if (!isValidTotpCode(code)) {
2722
2750
  const msg = "Please enter a 6-digit code";
2723
2751
  setError(msg);
2724
2752
  onError?.(msg);
@@ -2740,10 +2768,9 @@ var useTwoFactor = /* @__PURE__ */ __name((options = {}) => {
2740
2768
  return true;
2741
2769
  } catch (err) {
2742
2770
  authLogger.error("2FA TOTP verification error:", err);
2743
- const errorMessage = err instanceof APIError ? err.response?.error || err.response?.detail || err.response?.message || err.errorMessage : err instanceof Error ? err.message : "Invalid verification code";
2744
- if (err instanceof APIError && typeof err.response?.attempts_remaining === "number") {
2745
- setAttemptsRemaining(err.response.attempts_remaining);
2746
- }
2771
+ const errorMessage = extractTwoFactorError(err, "Invalid verification code");
2772
+ const attempts = extractAttemptsRemaining(err);
2773
+ if (attempts !== null) setAttemptsRemaining(attempts);
2747
2774
  setError(errorMessage);
2748
2775
  onError?.(errorMessage);
2749
2776
  Analytics.event("auth_otp_verify_fail" /* AUTH_OTP_VERIFY_FAIL */, {
@@ -2787,10 +2814,9 @@ var useTwoFactor = /* @__PURE__ */ __name((options = {}) => {
2787
2814
  return true;
2788
2815
  } catch (err) {
2789
2816
  authLogger.error("2FA backup code verification error:", err);
2790
- const errorMessage = err instanceof APIError ? err.response?.error || err.response?.detail || err.response?.message || err.errorMessage : err instanceof Error ? err.message : "Invalid backup code";
2791
- if (err instanceof APIError && typeof err.response?.attempts_remaining === "number") {
2792
- setAttemptsRemaining(err.response.attempts_remaining);
2793
- }
2817
+ const errorMessage = extractTwoFactorError(err, "Invalid backup code");
2818
+ const attempts = extractAttemptsRemaining(err);
2819
+ if (attempts !== null) setAttemptsRemaining(attempts);
2794
2820
  setError(errorMessage);
2795
2821
  onError?.(errorMessage);
2796
2822
  Analytics.event("auth_otp_verify_fail" /* AUTH_OTP_VERIFY_FAIL */, {
@@ -2812,7 +2838,7 @@ var useTwoFactor = /* @__PURE__ */ __name((options = {}) => {
2812
2838
  verifyBackupCode,
2813
2839
  clearError
2814
2840
  };
2815
- }, "useTwoFactor");
2841
+ }, "useTwoFactorVerify");
2816
2842
 
2817
2843
  // src/auth/hooks/useAuthForm.ts
2818
2844
  var useAuthForm = /* @__PURE__ */ __name((options) => {
@@ -2856,7 +2882,7 @@ var useAuthForm = /* @__PURE__ */ __name((options) => {
2856
2882
  startRateLimitCountdown,
2857
2883
  setWebmail
2858
2884
  } = formState;
2859
- const twoFactor = useTwoFactor({
2885
+ const twoFactor = useTwoFactorVerify({
2860
2886
  onSuccess: /* @__PURE__ */ __name(() => {
2861
2887
  authLogger.info("2FA verification successful, showing success screen");
2862
2888
  setStep("success");
@@ -3247,7 +3273,7 @@ var useTwoFactorSetup = /* @__PURE__ */ __name((options = {}) => {
3247
3273
  authLogger.info("2FA setup initiated, expires in:", data.expiresIn, "seconds");
3248
3274
  return data;
3249
3275
  } catch (err) {
3250
- const errorMessage = err instanceof Error ? err.message : "Failed to start 2FA setup";
3276
+ const errorMessage = extractTwoFactorError(err, "Failed to start 2FA setup");
3251
3277
  authLogger.error("2FA setup error:", err);
3252
3278
  setError(errorMessage);
3253
3279
  setSetupStep("idle");
@@ -3264,7 +3290,7 @@ var useTwoFactorSetup = /* @__PURE__ */ __name((options = {}) => {
3264
3290
  onError?.(msg);
3265
3291
  return null;
3266
3292
  }
3267
- if (!code || code.length !== 6) {
3293
+ if (!isValidTotpCode(code)) {
3268
3294
  const msg = "Please enter a 6-digit code";
3269
3295
  setError(msg);
3270
3296
  onError?.(msg);
@@ -3288,7 +3314,7 @@ var useTwoFactorSetup = /* @__PURE__ */ __name((options = {}) => {
3288
3314
  onComplete?.(codes);
3289
3315
  return codes;
3290
3316
  } catch (err) {
3291
- const errorMessage = err instanceof Error ? err.message : "Invalid code. Please try again.";
3317
+ const errorMessage = extractTwoFactorError(err, "Invalid code. Please try again.");
3292
3318
  authLogger.error("2FA setup confirmation error:", err);
3293
3319
  setError(errorMessage);
3294
3320
  setSetupStep("scanning");
@@ -3314,17 +3340,6 @@ var useTwoFactorSetup = /* @__PURE__ */ __name((options = {}) => {
3314
3340
 
3315
3341
  // src/auth/hooks/useTwoFactorStatus.ts
3316
3342
  var import_react12 = require("react");
3317
- function extractErrorMessage(err, fallback) {
3318
- if (err instanceof APIError) {
3319
- const body = err.response;
3320
- if (typeof body?.error === "string") return body.error;
3321
- if (typeof body?.detail === "string") return body.detail;
3322
- return err.message;
3323
- }
3324
- if (err instanceof Error) return err.message;
3325
- return fallback;
3326
- }
3327
- __name(extractErrorMessage, "extractErrorMessage");
3328
3343
  var useTwoFactorStatus = /* @__PURE__ */ __name(() => {
3329
3344
  const [isLoading, setIsLoading] = (0, import_react12.useState)(false);
3330
3345
  const [error, setError] = (0, import_react12.useState)(null);
@@ -3351,7 +3366,7 @@ var useTwoFactorStatus = /* @__PURE__ */ __name(() => {
3351
3366
  setHas2FAEnabled(response.has_2fa_enabled);
3352
3367
  authLogger.info("2FA status:", response.has_2fa_enabled ? "enabled" : "disabled");
3353
3368
  } catch (err) {
3354
- const errorMessage = extractErrorMessage(err, "Failed to fetch 2FA status");
3369
+ const errorMessage = extractTwoFactorError(err, "Failed to fetch 2FA status");
3355
3370
  authLogger.error("Failed to fetch 2FA status:", err);
3356
3371
  setError(errorMessage);
3357
3372
  } finally {
@@ -3359,7 +3374,7 @@ var useTwoFactorStatus = /* @__PURE__ */ __name(() => {
3359
3374
  }
3360
3375
  }, []);
3361
3376
  const disable2FA = (0, import_react12.useCallback)(async (code) => {
3362
- if (!code || code.length !== 6) {
3377
+ if (!isValidTotpCode(code)) {
3363
3378
  setError("Please enter a 6-digit code");
3364
3379
  return false;
3365
3380
  }
@@ -3373,7 +3388,7 @@ var useTwoFactorStatus = /* @__PURE__ */ __name(() => {
3373
3388
  authLogger.info("2FA disabled successfully");
3374
3389
  return true;
3375
3390
  } catch (err) {
3376
- const errorMessage = extractErrorMessage(err, "Invalid verification code");
3391
+ const errorMessage = extractTwoFactorError(err, "Invalid verification code");
3377
3392
  authLogger.error("Failed to disable 2FA:", err);
3378
3393
  setError(errorMessage);
3379
3394
  return false;
@@ -3392,6 +3407,14 @@ var useTwoFactorStatus = /* @__PURE__ */ __name(() => {
3392
3407
  };
3393
3408
  }, "useTwoFactorStatus");
3394
3409
 
3410
+ // src/auth/hooks/useTwoFactor.ts
3411
+ var useTwoFactor = /* @__PURE__ */ __name((options = {}) => {
3412
+ const verify = useTwoFactorVerify(options.verify);
3413
+ const setup = useTwoFactorSetup(options.setup);
3414
+ const status = useTwoFactorStatus();
3415
+ return { verify, setup, status };
3416
+ }, "useTwoFactor");
3417
+
3395
3418
  // src/auth/hooks/useLocalStorage.ts
3396
3419
  var import_react13 = require("react");
3397
3420
  function useLocalStorage(key, initialValue) {