@djangocfg/api 2.1.428 → 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 +58 -40
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.mjs +71 -53
- package/dist/auth.mjs.map +1 -1
- package/package.json +2 -2
- package/src/auth/hooks/useAuthForm.ts +39 -26
- package/src/auth/hooks/useAutoAuth.ts +35 -26
- package/src/auth/hooks/useQueryParams.ts +20 -6
package/dist/auth.mjs
CHANGED
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
useContext as useContext2,
|
|
28
28
|
useEffect as useEffect9,
|
|
29
29
|
useMemo as useMemo3,
|
|
30
|
-
useRef as
|
|
30
|
+
useRef as useRef7,
|
|
31
31
|
useState as useState12
|
|
32
32
|
} from "react";
|
|
33
33
|
import { SWRConfig } from "swr";
|
|
@@ -125,10 +125,22 @@ function useQueryParams() {
|
|
|
125
125
|
}, "updateQueryParams");
|
|
126
126
|
updateQueryParams();
|
|
127
127
|
window.addEventListener("popstate", updateQueryParams);
|
|
128
|
-
const
|
|
128
|
+
const origPush = window.history.pushState;
|
|
129
|
+
const origReplace = window.history.replaceState;
|
|
130
|
+
window.history.pushState = function(...args) {
|
|
131
|
+
const result = origPush.apply(this, args);
|
|
132
|
+
updateQueryParams();
|
|
133
|
+
return result;
|
|
134
|
+
};
|
|
135
|
+
window.history.replaceState = function(...args) {
|
|
136
|
+
const result = origReplace.apply(this, args);
|
|
137
|
+
updateQueryParams();
|
|
138
|
+
return result;
|
|
139
|
+
};
|
|
129
140
|
return () => {
|
|
130
141
|
window.removeEventListener("popstate", updateQueryParams);
|
|
131
|
-
|
|
142
|
+
window.history.pushState = origPush;
|
|
143
|
+
window.history.replaceState = origReplace;
|
|
132
144
|
};
|
|
133
145
|
}, [pathname]);
|
|
134
146
|
return queryParams;
|
|
@@ -216,7 +228,7 @@ var useAuthValidation = /* @__PURE__ */ __name(() => {
|
|
|
216
228
|
var validateIdentifier = /* @__PURE__ */ __name((id) => EMAIL_REGEX.test(id), "validateIdentifier");
|
|
217
229
|
|
|
218
230
|
// src/auth/hooks/useAuthForm.ts
|
|
219
|
-
import { useCallback as useCallback5, useEffect as useEffect4, useRef as
|
|
231
|
+
import { useCallback as useCallback5, useEffect as useEffect4, useRef as useRef4 } from "react";
|
|
220
232
|
|
|
221
233
|
// src/auth/utils/logger.ts
|
|
222
234
|
import { createConsola } from "consola";
|
|
@@ -270,7 +282,7 @@ var authLogger = logger.withTag("auth");
|
|
|
270
282
|
|
|
271
283
|
// src/auth/hooks/useAutoAuth.ts
|
|
272
284
|
import { usePathname as usePathname2 } from "next/navigation";
|
|
273
|
-
import { useEffect as useEffect3 } from "react";
|
|
285
|
+
import { useEffect as useEffect3, useRef as useRef3 } from "react";
|
|
274
286
|
var normalizePath = /* @__PURE__ */ __name((path) => {
|
|
275
287
|
if (!path) return "";
|
|
276
288
|
const withoutLocale = path.replace(/^\/[a-z]{2}(?=\/|$)/, "");
|
|
@@ -286,25 +298,27 @@ var useAutoAuth = /* @__PURE__ */ __name((options = {}) => {
|
|
|
286
298
|
const isAllowedPath = allowedPaths.some(
|
|
287
299
|
(path) => normalizedPath === path || normalizedPath.startsWith(path + "/")
|
|
288
300
|
);
|
|
289
|
-
const
|
|
301
|
+
const queryOtp = queryParams.get("otp") || "";
|
|
302
|
+
const queryEmail = queryParams.get("email") || void 0;
|
|
303
|
+
const hasOTP = !!queryOtp;
|
|
290
304
|
const isReady = !!pathname && hasOTP && isAllowedPath;
|
|
305
|
+
const otpValid = queryOtp.length === AUTH_CONSTANTS.EMAIL_OTP_LENGTH;
|
|
306
|
+
const onOTPDetectedRef = useRef3(onOTPDetected);
|
|
307
|
+
onOTPDetectedRef.current = onOTPDetected;
|
|
291
308
|
useEffect3(() => {
|
|
292
|
-
if (!isReady) return;
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
router.replace(queryString ? `${realPathname}?${queryString}` : realPathname);
|
|
306
|
-
}
|
|
307
|
-
}, [pathname, queryParams, onOTPDetected, cleanupUrl, router, isReady]);
|
|
309
|
+
if (!isReady || !otpValid) return;
|
|
310
|
+
authLogger.info("OTP detected in URL on auth page:", queryOtp);
|
|
311
|
+
onOTPDetectedRef.current?.(queryOtp, queryEmail);
|
|
312
|
+
}, [isReady, otpValid, queryOtp, queryEmail]);
|
|
313
|
+
useEffect3(() => {
|
|
314
|
+
if (!isReady || !cleanupUrl || !queryOtp) return;
|
|
315
|
+
const cleanQuery = Object.fromEntries(queryParams.entries());
|
|
316
|
+
delete cleanQuery.otp;
|
|
317
|
+
delete cleanQuery.email;
|
|
318
|
+
const queryString = new URLSearchParams(cleanQuery).toString();
|
|
319
|
+
const realPathname = typeof window !== "undefined" ? window.location.pathname : pathname;
|
|
320
|
+
router.replace(queryString ? `${realPathname}?${queryString}` : realPathname);
|
|
321
|
+
}, [isReady, cleanupUrl, queryOtp]);
|
|
308
322
|
return {
|
|
309
323
|
isReady,
|
|
310
324
|
hasOTP,
|
|
@@ -2698,7 +2712,8 @@ var useAuthForm = /* @__PURE__ */ __name((options) => {
|
|
|
2698
2712
|
} = options;
|
|
2699
2713
|
const formState = useAuthFormState();
|
|
2700
2714
|
const validation = useAuthValidation();
|
|
2701
|
-
const isAutoSubmitFromUrlRef =
|
|
2715
|
+
const isAutoSubmitFromUrlRef = useRef4(false);
|
|
2716
|
+
const processedOtpKeyRef = useRef4(null);
|
|
2702
2717
|
const {
|
|
2703
2718
|
requestOTP,
|
|
2704
2719
|
verifyOTP,
|
|
@@ -2909,29 +2924,32 @@ var useAuthForm = /* @__PURE__ */ __name((options) => {
|
|
|
2909
2924
|
setTwoFactorCode("");
|
|
2910
2925
|
clearError();
|
|
2911
2926
|
}, [setUseBackupCode, setTwoFactorCode, clearError]);
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2927
|
+
const handleOTPFromUrl = useCallback5((detectedOtp, detectedEmail) => {
|
|
2928
|
+
const autoIdentifier = detectedEmail || getSavedEmail() || "";
|
|
2929
|
+
if (!autoIdentifier) {
|
|
2930
|
+
authLogger.warn("No identifier found for auto-submit (no email in URL or storage)");
|
|
2931
|
+
return;
|
|
2932
|
+
}
|
|
2933
|
+
const otpKey = `${autoIdentifier}:${detectedOtp}`;
|
|
2934
|
+
if (processedOtpKeyRef.current === otpKey) return;
|
|
2935
|
+
if (isAutoSubmitFromUrlRef.current) return;
|
|
2936
|
+
processedOtpKeyRef.current = otpKey;
|
|
2937
|
+
isAutoSubmitFromUrlRef.current = true;
|
|
2938
|
+
authLogger.info("OTP detected from URL, auto-submitting");
|
|
2939
|
+
setIdentifier(autoIdentifier);
|
|
2940
|
+
setOtp(detectedOtp);
|
|
2941
|
+
setStep("otp");
|
|
2942
|
+
setTimeout(async () => {
|
|
2943
|
+
try {
|
|
2944
|
+
await submitOTP(autoIdentifier, detectedOtp);
|
|
2945
|
+
} finally {
|
|
2921
2946
|
isAutoSubmitFromUrlRef.current = false;
|
|
2922
|
-
return;
|
|
2923
2947
|
}
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
await submitOTP(autoIdentifier, detectedOtp);
|
|
2930
|
-
} finally {
|
|
2931
|
-
isAutoSubmitFromUrlRef.current = false;
|
|
2932
|
-
}
|
|
2933
|
-
}, 100);
|
|
2934
|
-
}, "onOTPDetected"),
|
|
2948
|
+
}, 100);
|
|
2949
|
+
}, [getSavedEmail, setIdentifier, setOtp, setStep, submitOTP]);
|
|
2950
|
+
useAutoAuth({
|
|
2951
|
+
allowedPaths: [authPath],
|
|
2952
|
+
onOTPDetected: handleOTPFromUrl,
|
|
2935
2953
|
cleanupUrl: true
|
|
2936
2954
|
});
|
|
2937
2955
|
return {
|
|
@@ -3775,7 +3793,7 @@ function getCacheMetadata() {
|
|
|
3775
3793
|
__name(getCacheMetadata, "getCacheMetadata");
|
|
3776
3794
|
|
|
3777
3795
|
// src/auth/hooks/useTokenRefresh.ts
|
|
3778
|
-
import { useCallback as useCallback9, useEffect as useEffect7, useRef as
|
|
3796
|
+
import { useCallback as useCallback9, useEffect as useEffect7, useRef as useRef5 } from "react";
|
|
3779
3797
|
var TOKEN_REFRESH_THRESHOLD_MS = 10 * 60 * 1e3;
|
|
3780
3798
|
var CHECK_INTERVAL_MS = 5 * 60 * 1e3;
|
|
3781
3799
|
function getTokenExpiry(token) {
|
|
@@ -3796,7 +3814,7 @@ function isTokenExpiringSoon(token, thresholdMs) {
|
|
|
3796
3814
|
__name(isTokenExpiringSoon, "isTokenExpiringSoon");
|
|
3797
3815
|
function useTokenRefresh(options = {}) {
|
|
3798
3816
|
const { enabled = true, onRefresh, onRefreshError } = options;
|
|
3799
|
-
const isRefreshingRef =
|
|
3817
|
+
const isRefreshingRef = useRef5(false);
|
|
3800
3818
|
const refreshToken = useCallback9(async () => {
|
|
3801
3819
|
if (isRefreshingRef.current) {
|
|
3802
3820
|
authLogger.debug("Token refresh already in progress");
|
|
@@ -3915,7 +3933,7 @@ import {
|
|
|
3915
3933
|
useContext,
|
|
3916
3934
|
useEffect as useEffect8,
|
|
3917
3935
|
useMemo as useMemo2,
|
|
3918
|
-
useRef as
|
|
3936
|
+
useRef as useRef6,
|
|
3919
3937
|
useState as useState11
|
|
3920
3938
|
} from "react";
|
|
3921
3939
|
|
|
@@ -4502,8 +4520,8 @@ function AccountsProvider({ children }) {
|
|
|
4502
4520
|
});
|
|
4503
4521
|
const [isLoadingProfile, setIsLoadingProfile] = useState11(false);
|
|
4504
4522
|
const [profileError, setProfileError] = useState11(null);
|
|
4505
|
-
const profileRef =
|
|
4506
|
-
const isLoadingRef =
|
|
4523
|
+
const profileRef = useRef6(profile);
|
|
4524
|
+
const isLoadingRef = useRef6(false);
|
|
4507
4525
|
useEffect8(() => {
|
|
4508
4526
|
profileRef.current = profile;
|
|
4509
4527
|
}, [profile]);
|
|
@@ -4671,9 +4689,9 @@ var AuthProviderInternal = /* @__PURE__ */ __name(({ children, config }) => {
|
|
|
4671
4689
|
}, "onRefreshError")
|
|
4672
4690
|
});
|
|
4673
4691
|
const user = accounts.profile;
|
|
4674
|
-
const userRef =
|
|
4675
|
-
const configRef =
|
|
4676
|
-
const isLoadingProfileRef =
|
|
4692
|
+
const userRef = useRef7(user);
|
|
4693
|
+
const configRef = useRef7(config);
|
|
4694
|
+
const isLoadingProfileRef = useRef7(false);
|
|
4677
4695
|
useEffect9(() => {
|
|
4678
4696
|
userRef.current = user;
|
|
4679
4697
|
}, [user]);
|
|
@@ -4699,7 +4717,7 @@ var AuthProviderInternal = /* @__PURE__ */ __name(({ children, config }) => {
|
|
|
4699
4717
|
}
|
|
4700
4718
|
return false;
|
|
4701
4719
|
}, [clearAuthState]);
|
|
4702
|
-
const isAutoLoggingOutRef =
|
|
4720
|
+
const isAutoLoggingOutRef = useRef7(false);
|
|
4703
4721
|
const swrOnError = useCallback12((error) => {
|
|
4704
4722
|
const isAuthError = error?.status === 401 || error?.statusCode === 401 || error?.code === "token_not_valid" || error?.code === "authentication_failed";
|
|
4705
4723
|
if (isAuthError && !isAutoLoggingOutRef.current) {
|