@djangocfg/api 2.1.429 → 2.1.430

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
@@ -187,10 +187,22 @@ function useQueryParams() {
187
187
  }, "updateQueryParams");
188
188
  updateQueryParams();
189
189
  window.addEventListener("popstate", updateQueryParams);
190
- const intervalId = setInterval(updateQueryParams, 100);
190
+ const origPush = window.history.pushState;
191
+ const origReplace = window.history.replaceState;
192
+ window.history.pushState = function(...args) {
193
+ const result = origPush.apply(this, args);
194
+ updateQueryParams();
195
+ return result;
196
+ };
197
+ window.history.replaceState = function(...args) {
198
+ const result = origReplace.apply(this, args);
199
+ updateQueryParams();
200
+ return result;
201
+ };
191
202
  return () => {
192
203
  window.removeEventListener("popstate", updateQueryParams);
193
- clearInterval(intervalId);
204
+ window.history.pushState = origPush;
205
+ window.history.replaceState = origReplace;
194
206
  };
195
207
  }, [pathname]);
196
208
  return queryParams;
@@ -348,25 +360,27 @@ var useAutoAuth = /* @__PURE__ */ __name((options = {}) => {
348
360
  const isAllowedPath = allowedPaths.some(
349
361
  (path) => normalizedPath === path || normalizedPath.startsWith(path + "/")
350
362
  );
351
- const hasOTP = !!queryParams.get("otp");
363
+ const queryOtp = queryParams.get("otp") || "";
364
+ const queryEmail = queryParams.get("email") || void 0;
365
+ const hasOTP = !!queryOtp;
352
366
  const isReady = !!pathname && hasOTP && isAllowedPath;
367
+ const otpValid = queryOtp.length === AUTH_CONSTANTS.EMAIL_OTP_LENGTH;
368
+ const onOTPDetectedRef = (0, import_react5.useRef)(onOTPDetected);
369
+ onOTPDetectedRef.current = onOTPDetected;
353
370
  (0, import_react5.useEffect)(() => {
354
- if (!isReady) return;
355
- const queryOtp = queryParams.get("otp");
356
- const queryEmail = queryParams.get("email") || void 0;
357
- if (queryOtp && typeof queryOtp === "string" && queryOtp.length === AUTH_CONSTANTS.EMAIL_OTP_LENGTH) {
358
- authLogger.info("OTP detected in URL on auth page:", queryOtp);
359
- onOTPDetected?.(queryOtp, queryEmail);
360
- }
361
- if (cleanupUrl && queryOtp) {
362
- const cleanQuery = Object.fromEntries(queryParams.entries());
363
- delete cleanQuery.otp;
364
- delete cleanQuery.email;
365
- const queryString = new URLSearchParams(cleanQuery).toString();
366
- const realPathname = typeof window !== "undefined" ? window.location.pathname : pathname;
367
- router.replace(queryString ? `${realPathname}?${queryString}` : realPathname);
368
- }
369
- }, [pathname, queryParams, onOTPDetected, cleanupUrl, router, isReady]);
371
+ if (!isReady || !otpValid) return;
372
+ authLogger.info("OTP detected in URL on auth page:", queryOtp);
373
+ onOTPDetectedRef.current?.(queryOtp, queryEmail);
374
+ }, [isReady, otpValid, queryOtp, queryEmail]);
375
+ (0, import_react5.useEffect)(() => {
376
+ if (!isReady || !cleanupUrl || !queryOtp) return;
377
+ const cleanQuery = Object.fromEntries(queryParams.entries());
378
+ delete cleanQuery.otp;
379
+ delete cleanQuery.email;
380
+ const queryString = new URLSearchParams(cleanQuery).toString();
381
+ const realPathname = typeof window !== "undefined" ? window.location.pathname : pathname;
382
+ router.replace(queryString ? `${realPathname}?${queryString}` : realPathname);
383
+ }, [isReady, cleanupUrl, queryOtp]);
370
384
  return {
371
385
  isReady,
372
386
  hasOTP,
@@ -2761,6 +2775,7 @@ var useAuthForm = /* @__PURE__ */ __name((options) => {
2761
2775
  const formState = useAuthFormState();
2762
2776
  const validation = useAuthValidation();
2763
2777
  const isAutoSubmitFromUrlRef = (0, import_react7.useRef)(false);
2778
+ const processedOtpKeyRef = (0, import_react7.useRef)(null);
2764
2779
  const {
2765
2780
  requestOTP,
2766
2781
  verifyOTP,
@@ -2971,29 +2986,32 @@ var useAuthForm = /* @__PURE__ */ __name((options) => {
2971
2986
  setTwoFactorCode("");
2972
2987
  clearError();
2973
2988
  }, [setUseBackupCode, setTwoFactorCode, clearError]);
2974
- useAutoAuth({
2975
- allowedPaths: [authPath],
2976
- onOTPDetected: /* @__PURE__ */ __name((detectedOtp, detectedEmail) => {
2977
- if (isAutoSubmitFromUrlRef.current || isLoading) return;
2978
- isAutoSubmitFromUrlRef.current = true;
2979
- authLogger.info("OTP detected from URL, auto-submitting");
2980
- const autoIdentifier = detectedEmail || getSavedEmail() || "";
2981
- if (!autoIdentifier) {
2982
- authLogger.warn("No identifier found for auto-submit (no email in URL or storage)");
2989
+ const handleOTPFromUrl = (0, import_react7.useCallback)((detectedOtp, detectedEmail) => {
2990
+ const autoIdentifier = detectedEmail || getSavedEmail() || "";
2991
+ if (!autoIdentifier) {
2992
+ authLogger.warn("No identifier found for auto-submit (no email in URL or storage)");
2993
+ return;
2994
+ }
2995
+ const otpKey = `${autoIdentifier}:${detectedOtp}`;
2996
+ if (processedOtpKeyRef.current === otpKey) return;
2997
+ if (isAutoSubmitFromUrlRef.current) return;
2998
+ processedOtpKeyRef.current = otpKey;
2999
+ isAutoSubmitFromUrlRef.current = true;
3000
+ authLogger.info("OTP detected from URL, auto-submitting");
3001
+ setIdentifier(autoIdentifier);
3002
+ setOtp(detectedOtp);
3003
+ setStep("otp");
3004
+ setTimeout(async () => {
3005
+ try {
3006
+ await submitOTP(autoIdentifier, detectedOtp);
3007
+ } finally {
2983
3008
  isAutoSubmitFromUrlRef.current = false;
2984
- return;
2985
3009
  }
2986
- setIdentifier(autoIdentifier);
2987
- setOtp(detectedOtp);
2988
- setStep("otp");
2989
- setTimeout(async () => {
2990
- try {
2991
- await submitOTP(autoIdentifier, detectedOtp);
2992
- } finally {
2993
- isAutoSubmitFromUrlRef.current = false;
2994
- }
2995
- }, 100);
2996
- }, "onOTPDetected"),
3010
+ }, 100);
3011
+ }, [getSavedEmail, setIdentifier, setOtp, setStep, submitOTP]);
3012
+ useAutoAuth({
3013
+ allowedPaths: [authPath],
3014
+ onOTPDetected: handleOTPFromUrl,
2997
3015
  cleanupUrl: true
2998
3016
  });
2999
3017
  return {