@nocios/crudify-ui 1.3.0 → 1.3.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/README.md +155 -1108
- package/README_DEPTH.md +1046 -0
- package/dist/index.d.mts +181 -1
- package/dist/index.d.ts +181 -1
- package/dist/index.js +393 -20
- package/dist/index.mjs +388 -19
- package/package.json +1 -1
- package/MIGRATION-GUIDE.md +0 -312
- package/MIGRATION.md +0 -201
- package/MIGRATION_EXAMPLE.md +0 -538
- package/TECHNICAL_SPECIFICATION.md +0 -344
- package/example-app.tsx +0 -197
- package/mejoras_npm_lib.md +0 -790
- package/tsup.config.ts +0 -9
package/dist/index.js
CHANGED
|
@@ -1298,7 +1298,7 @@ __export(index_exports, {
|
|
|
1298
1298
|
TokenStorage: () => TokenStorage,
|
|
1299
1299
|
UserProfileDisplay: () => UserProfileDisplay_default,
|
|
1300
1300
|
configurationManager: () => configurationManager,
|
|
1301
|
-
crudify: () =>
|
|
1301
|
+
crudify: () => import_crudify_browser11.default,
|
|
1302
1302
|
crudifyInitializer: () => crudifyInitializer,
|
|
1303
1303
|
decodeJwtSafely: () => decodeJwtSafely,
|
|
1304
1304
|
getCookie: () => getCookie,
|
|
@@ -1314,6 +1314,7 @@ __export(index_exports, {
|
|
|
1314
1314
|
secureLocalStorage: () => secureLocalStorage,
|
|
1315
1315
|
secureSessionStorage: () => secureSessionStorage,
|
|
1316
1316
|
tokenManager: () => tokenManager,
|
|
1317
|
+
useAuth: () => useAuth,
|
|
1317
1318
|
useCrudifyAuth: () => useCrudifyAuth,
|
|
1318
1319
|
useCrudifyConfig: () => useCrudifyConfig,
|
|
1319
1320
|
useCrudifyData: () => useCrudifyData,
|
|
@@ -1321,12 +1322,14 @@ __export(index_exports, {
|
|
|
1321
1322
|
useCrudifyInstance: () => useCrudifyInstance,
|
|
1322
1323
|
useCrudifyLogin: () => useCrudifyLogin,
|
|
1323
1324
|
useCrudifyUser: () => useCrudifyUser,
|
|
1325
|
+
useData: () => useData,
|
|
1324
1326
|
useSession: () => useSession,
|
|
1325
1327
|
useSessionContext: () => useSessionContext,
|
|
1328
|
+
useUserData: () => useUserData,
|
|
1326
1329
|
useUserProfile: () => useUserProfile
|
|
1327
1330
|
});
|
|
1328
1331
|
module.exports = __toCommonJS(index_exports);
|
|
1329
|
-
var
|
|
1332
|
+
var import_crudify_browser11 = __toESM(require("@nocios/crudify-browser"));
|
|
1330
1333
|
__reExport(index_exports, require("@nocios/crudify-browser"), module.exports);
|
|
1331
1334
|
|
|
1332
1335
|
// src/components/CrudifyLogin/index.tsx
|
|
@@ -2018,7 +2021,7 @@ var useCrudifyAuth = () => {
|
|
|
2018
2021
|
// src/components/CrudifyLogin/Forms/LoginForm.tsx
|
|
2019
2022
|
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
2020
2023
|
var LoginForm = ({ onScreenChange, onExternalNavigate, onLoginSuccess, onError, redirectUrl = "/" }) => {
|
|
2021
|
-
const { crudify:
|
|
2024
|
+
const { crudify: crudify11 } = useCrudify();
|
|
2022
2025
|
const { state, updateFormData, setFieldError, clearErrors, setLoading } = useLoginState();
|
|
2023
2026
|
const { setToken } = useCrudifyAuth();
|
|
2024
2027
|
const { t } = useTranslation();
|
|
@@ -2070,10 +2073,10 @@ var LoginForm = ({ onScreenChange, onExternalNavigate, onLoginSuccess, onError,
|
|
|
2070
2073
|
clearErrors();
|
|
2071
2074
|
setLoading(true);
|
|
2072
2075
|
try {
|
|
2073
|
-
if (!
|
|
2076
|
+
if (!crudify11) {
|
|
2074
2077
|
throw new Error("Crudify not initialized");
|
|
2075
2078
|
}
|
|
2076
|
-
const response = await
|
|
2079
|
+
const response = await crudify11.login(state.formData.username, state.formData.password);
|
|
2077
2080
|
setLoading(false);
|
|
2078
2081
|
if (response.success) {
|
|
2079
2082
|
console.log("\u{1F510} LoginForm - Login successful, setting tokens");
|
|
@@ -2236,7 +2239,7 @@ var import_react7 = require("react");
|
|
|
2236
2239
|
var import_material2 = require("@mui/material");
|
|
2237
2240
|
var import_jsx_runtime6 = require("react/jsx-runtime");
|
|
2238
2241
|
var ForgotPasswordForm = ({ onScreenChange, onError }) => {
|
|
2239
|
-
const { crudify:
|
|
2242
|
+
const { crudify: crudify11 } = useCrudify();
|
|
2240
2243
|
const [email, setEmail] = (0, import_react7.useState)("");
|
|
2241
2244
|
const [loading, setLoading] = (0, import_react7.useState)(false);
|
|
2242
2245
|
const [errors, setErrors] = (0, import_react7.useState)([]);
|
|
@@ -2265,7 +2268,7 @@ var ForgotPasswordForm = ({ onScreenChange, onError }) => {
|
|
|
2265
2268
|
return emailRegex.test(email2);
|
|
2266
2269
|
};
|
|
2267
2270
|
const handleSubmit = async () => {
|
|
2268
|
-
if (loading || !
|
|
2271
|
+
if (loading || !crudify11) return;
|
|
2269
2272
|
setErrors([]);
|
|
2270
2273
|
setHelperTextEmail(null);
|
|
2271
2274
|
if (!email) {
|
|
@@ -2279,7 +2282,7 @@ var ForgotPasswordForm = ({ onScreenChange, onError }) => {
|
|
|
2279
2282
|
setLoading(true);
|
|
2280
2283
|
try {
|
|
2281
2284
|
const data = [{ operation: "requestPasswordReset", data: { email } }];
|
|
2282
|
-
const response = await
|
|
2285
|
+
const response = await crudify11.transaction(data);
|
|
2283
2286
|
if (response.success) {
|
|
2284
2287
|
if (response.data && response.data.existingCodeValid) {
|
|
2285
2288
|
setCodeAlreadyExists(true);
|
|
@@ -2382,7 +2385,7 @@ var import_react8 = require("react");
|
|
|
2382
2385
|
var import_material3 = require("@mui/material");
|
|
2383
2386
|
var import_jsx_runtime7 = require("react/jsx-runtime");
|
|
2384
2387
|
var ResetPasswordForm = ({ onScreenChange, onError, searchParams, onResetSuccess }) => {
|
|
2385
|
-
const { crudify:
|
|
2388
|
+
const { crudify: crudify11 } = useCrudify();
|
|
2386
2389
|
const [newPassword, setNewPassword] = (0, import_react8.useState)("");
|
|
2387
2390
|
const [confirmPassword, setConfirmPassword] = (0, import_react8.useState)("");
|
|
2388
2391
|
const [loading, setLoading] = (0, import_react8.useState)(false);
|
|
@@ -2462,9 +2465,9 @@ var ResetPasswordForm = ({ onScreenChange, onError, searchParams, onResetSuccess
|
|
|
2462
2465
|
setErrors([t("resetPassword.invalidCode")]);
|
|
2463
2466
|
setValidatingCode(false);
|
|
2464
2467
|
setTimeout(() => onScreenChange?.("forgotPassword"), 3e3);
|
|
2465
|
-
}, [searchParams,
|
|
2468
|
+
}, [searchParams, crudify11, t, onScreenChange]);
|
|
2466
2469
|
(0, import_react8.useEffect)(() => {
|
|
2467
|
-
if (
|
|
2470
|
+
if (crudify11 && pendingValidation && !isValidating) {
|
|
2468
2471
|
setIsValidating(true);
|
|
2469
2472
|
const validateCode = async (emailToValidate, codeToValidate) => {
|
|
2470
2473
|
try {
|
|
@@ -2474,7 +2477,7 @@ var ResetPasswordForm = ({ onScreenChange, onError, searchParams, onResetSuccess
|
|
|
2474
2477
|
data: { email: emailToValidate, codePassword: codeToValidate }
|
|
2475
2478
|
}
|
|
2476
2479
|
];
|
|
2477
|
-
const response = await
|
|
2480
|
+
const response = await crudify11.transaction(data);
|
|
2478
2481
|
if (response.data && Array.isArray(response.data)) {
|
|
2479
2482
|
const validationResult = response.data[0];
|
|
2480
2483
|
if (validationResult && validationResult.response && validationResult.response.status === "OK") {
|
|
@@ -2503,7 +2506,7 @@ var ResetPasswordForm = ({ onScreenChange, onError, searchParams, onResetSuccess
|
|
|
2503
2506
|
};
|
|
2504
2507
|
validateCode(pendingValidation.email, pendingValidation.code);
|
|
2505
2508
|
}
|
|
2506
|
-
}, [
|
|
2509
|
+
}, [crudify11, pendingValidation, t, onScreenChange]);
|
|
2507
2510
|
const validatePassword = (password) => {
|
|
2508
2511
|
if (password.length < 8) {
|
|
2509
2512
|
return t("resetPassword.passwordTooShort");
|
|
@@ -2511,7 +2514,7 @@ var ResetPasswordForm = ({ onScreenChange, onError, searchParams, onResetSuccess
|
|
|
2511
2514
|
return null;
|
|
2512
2515
|
};
|
|
2513
2516
|
const handleSubmit = async () => {
|
|
2514
|
-
if (loading || !
|
|
2517
|
+
if (loading || !crudify11) return;
|
|
2515
2518
|
setErrors([]);
|
|
2516
2519
|
setHelperTextNewPassword(null);
|
|
2517
2520
|
setHelperTextConfirmPassword(null);
|
|
@@ -2542,7 +2545,7 @@ var ResetPasswordForm = ({ onScreenChange, onError, searchParams, onResetSuccess
|
|
|
2542
2545
|
data: { email, codePassword: code, newPassword }
|
|
2543
2546
|
}
|
|
2544
2547
|
];
|
|
2545
|
-
const response = await
|
|
2548
|
+
const response = await crudify11.transaction(data);
|
|
2546
2549
|
if (response.success) {
|
|
2547
2550
|
setErrors([]);
|
|
2548
2551
|
setTimeout(() => {
|
|
@@ -2653,7 +2656,7 @@ var import_react9 = require("react");
|
|
|
2653
2656
|
var import_material4 = require("@mui/material");
|
|
2654
2657
|
var import_jsx_runtime8 = require("react/jsx-runtime");
|
|
2655
2658
|
var CheckCodeForm = ({ onScreenChange, onError, searchParams }) => {
|
|
2656
|
-
const { crudify:
|
|
2659
|
+
const { crudify: crudify11 } = useCrudify();
|
|
2657
2660
|
const [code, setCode] = (0, import_react9.useState)("");
|
|
2658
2661
|
const [loading, setLoading] = (0, import_react9.useState)(false);
|
|
2659
2662
|
const [errors, setErrors] = (0, import_react9.useState)([]);
|
|
@@ -2692,7 +2695,7 @@ var CheckCodeForm = ({ onScreenChange, onError, searchParams }) => {
|
|
|
2692
2695
|
}
|
|
2693
2696
|
}, [searchParams, onScreenChange]);
|
|
2694
2697
|
const handleSubmit = async () => {
|
|
2695
|
-
if (loading || !
|
|
2698
|
+
if (loading || !crudify11) return;
|
|
2696
2699
|
setErrors([]);
|
|
2697
2700
|
setHelperTextCode(null);
|
|
2698
2701
|
if (!code) {
|
|
@@ -2711,7 +2714,7 @@ var CheckCodeForm = ({ onScreenChange, onError, searchParams }) => {
|
|
|
2711
2714
|
data: { email, codePassword: code }
|
|
2712
2715
|
}
|
|
2713
2716
|
];
|
|
2714
|
-
const response = await
|
|
2717
|
+
const response = await crudify11.transaction(data);
|
|
2715
2718
|
if (response.success) {
|
|
2716
2719
|
onScreenChange?.("resetPassword", { email, code, fromCodeVerification: "true" });
|
|
2717
2720
|
} else {
|
|
@@ -4458,11 +4461,40 @@ function useSession(options = {}) {
|
|
|
4458
4461
|
|
|
4459
4462
|
// src/providers/SessionProvider.tsx
|
|
4460
4463
|
var import_react17 = require("react");
|
|
4464
|
+
init_jwtUtils();
|
|
4461
4465
|
var import_jsx_runtime12 = require("react/jsx-runtime");
|
|
4462
4466
|
var SessionContext = (0, import_react17.createContext)(void 0);
|
|
4463
4467
|
function SessionProvider({ children, options = {} }) {
|
|
4464
|
-
const
|
|
4465
|
-
|
|
4468
|
+
const sessionHook = useSession(options);
|
|
4469
|
+
const sessionData = (0, import_react17.useMemo)(() => {
|
|
4470
|
+
if (!sessionHook.tokens?.accessToken || !sessionHook.isAuthenticated) {
|
|
4471
|
+
return null;
|
|
4472
|
+
}
|
|
4473
|
+
try {
|
|
4474
|
+
const decoded = decodeJwtSafely(sessionHook.tokens.accessToken);
|
|
4475
|
+
if (decoded && decoded.sub && decoded.email && decoded.subscriber) {
|
|
4476
|
+
const result = {
|
|
4477
|
+
_id: decoded.sub,
|
|
4478
|
+
email: decoded.email,
|
|
4479
|
+
subscriberKey: decoded.subscriber
|
|
4480
|
+
};
|
|
4481
|
+
Object.keys(decoded).forEach((key) => {
|
|
4482
|
+
if (!["sub", "email", "subscriber"].includes(key)) {
|
|
4483
|
+
result[key] = decoded[key];
|
|
4484
|
+
}
|
|
4485
|
+
});
|
|
4486
|
+
return result;
|
|
4487
|
+
}
|
|
4488
|
+
} catch (error) {
|
|
4489
|
+
console.error("Error decoding JWT token for sessionData:", error);
|
|
4490
|
+
}
|
|
4491
|
+
return null;
|
|
4492
|
+
}, [sessionHook.tokens?.accessToken, sessionHook.isAuthenticated]);
|
|
4493
|
+
const contextValue = {
|
|
4494
|
+
...sessionHook,
|
|
4495
|
+
sessionData
|
|
4496
|
+
};
|
|
4497
|
+
return /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(SessionContext.Provider, { value: contextValue, children });
|
|
4466
4498
|
}
|
|
4467
4499
|
function useSessionContext() {
|
|
4468
4500
|
const context = (0, import_react17.useContext)(SessionContext);
|
|
@@ -4704,6 +4736,344 @@ function SessionStatus() {
|
|
|
4704
4736
|
] })
|
|
4705
4737
|
] });
|
|
4706
4738
|
}
|
|
4739
|
+
|
|
4740
|
+
// src/hooks/useUserData.ts
|
|
4741
|
+
var import_react19 = require("react");
|
|
4742
|
+
var import_crudify_browser9 = __toESM(require("@nocios/crudify-browser"));
|
|
4743
|
+
var useUserData = (options = {}) => {
|
|
4744
|
+
const { autoFetch = true, retryOnError = false, maxRetries = 3 } = options;
|
|
4745
|
+
const { isAuthenticated, isInitialized, sessionData, tokens } = useSessionContext();
|
|
4746
|
+
const [userData, setUserData] = (0, import_react19.useState)(null);
|
|
4747
|
+
const [loading, setLoading] = (0, import_react19.useState)(false);
|
|
4748
|
+
const [error, setError] = (0, import_react19.useState)(null);
|
|
4749
|
+
const abortControllerRef = (0, import_react19.useRef)(null);
|
|
4750
|
+
const mountedRef = (0, import_react19.useRef)(true);
|
|
4751
|
+
const requestIdRef = (0, import_react19.useRef)(0);
|
|
4752
|
+
const retryCountRef = (0, import_react19.useRef)(0);
|
|
4753
|
+
const getUserEmail = (0, import_react19.useCallback)(() => {
|
|
4754
|
+
if (!sessionData) return null;
|
|
4755
|
+
return sessionData.email || sessionData["cognito:username"] || null;
|
|
4756
|
+
}, [sessionData]);
|
|
4757
|
+
const clearProfile = (0, import_react19.useCallback)(() => {
|
|
4758
|
+
setUserData(null);
|
|
4759
|
+
setError(null);
|
|
4760
|
+
setLoading(false);
|
|
4761
|
+
retryCountRef.current = 0;
|
|
4762
|
+
}, []);
|
|
4763
|
+
const refreshProfile = (0, import_react19.useCallback)(async () => {
|
|
4764
|
+
const userEmail = getUserEmail();
|
|
4765
|
+
console.log("\u{1F464} useUserData - Refreshing profile for:", userEmail);
|
|
4766
|
+
if (!userEmail) {
|
|
4767
|
+
if (mountedRef.current) {
|
|
4768
|
+
setError("No user email available from session data");
|
|
4769
|
+
setLoading(false);
|
|
4770
|
+
}
|
|
4771
|
+
return;
|
|
4772
|
+
}
|
|
4773
|
+
if (!isInitialized) {
|
|
4774
|
+
if (mountedRef.current) {
|
|
4775
|
+
setError("Session not initialized");
|
|
4776
|
+
setLoading(false);
|
|
4777
|
+
}
|
|
4778
|
+
return;
|
|
4779
|
+
}
|
|
4780
|
+
if (abortControllerRef.current) {
|
|
4781
|
+
abortControllerRef.current.abort();
|
|
4782
|
+
}
|
|
4783
|
+
const abortController = new AbortController();
|
|
4784
|
+
abortControllerRef.current = abortController;
|
|
4785
|
+
const currentRequestId = ++requestIdRef.current;
|
|
4786
|
+
try {
|
|
4787
|
+
if (mountedRef.current) {
|
|
4788
|
+
setLoading(true);
|
|
4789
|
+
setError(null);
|
|
4790
|
+
}
|
|
4791
|
+
console.log("\u{1F464} useUserData - Fetching profile data from database");
|
|
4792
|
+
const response = await import_crudify_browser9.default.readItems("users", {
|
|
4793
|
+
filter: { email: userEmail },
|
|
4794
|
+
pagination: { limit: 1 }
|
|
4795
|
+
});
|
|
4796
|
+
console.log("\u{1F464} useUserData - Database response:", response);
|
|
4797
|
+
console.log("\u{1F464} useUserData - response.data:", response.data);
|
|
4798
|
+
console.log("\u{1F464} useUserData - response.data type:", typeof response.data);
|
|
4799
|
+
if (currentRequestId === requestIdRef.current && mountedRef.current && !abortController.signal.aborted) {
|
|
4800
|
+
let userData2 = null;
|
|
4801
|
+
if (response.success) {
|
|
4802
|
+
console.log("\u{1F464} useUserData - Processing successful response:", {
|
|
4803
|
+
dataType: typeof response.data,
|
|
4804
|
+
isArray: Array.isArray(response.data),
|
|
4805
|
+
hasResponse: !!response.data?.response,
|
|
4806
|
+
hasResponseData: !!response.data?.response?.data,
|
|
4807
|
+
responseDataType: typeof response.data?.response?.data
|
|
4808
|
+
});
|
|
4809
|
+
if (Array.isArray(response.data) && response.data.length > 0) {
|
|
4810
|
+
console.log("\u{1F464} useUserData - Found direct array format");
|
|
4811
|
+
userData2 = response.data[0];
|
|
4812
|
+
} else if (response.data?.response?.data) {
|
|
4813
|
+
console.log("\u{1F464} useUserData - Found nested response.data format");
|
|
4814
|
+
try {
|
|
4815
|
+
const rawData = response.data.response.data;
|
|
4816
|
+
console.log("\u{1F464} useUserData - Raw nested data:", rawData);
|
|
4817
|
+
console.log("\u{1F464} useUserData - Raw data type:", typeof rawData);
|
|
4818
|
+
const parsedData = typeof rawData === "string" ? JSON.parse(rawData) : rawData;
|
|
4819
|
+
console.log("\u{1F464} useUserData - Parsed nested data:", parsedData);
|
|
4820
|
+
if (parsedData && parsedData.items && Array.isArray(parsedData.items) && parsedData.items.length > 0) {
|
|
4821
|
+
userData2 = parsedData.items[0];
|
|
4822
|
+
console.log("\u{1F464} useUserData - Extracted user from nested items:", userData2);
|
|
4823
|
+
} else {
|
|
4824
|
+
console.log("\u{1F464} useUserData - No items found in parsed data or items array is empty");
|
|
4825
|
+
}
|
|
4826
|
+
} catch (parseError) {
|
|
4827
|
+
console.error("\u{1F464} useUserData - Error parsing nested response data:", parseError);
|
|
4828
|
+
}
|
|
4829
|
+
} else if (response.data && typeof response.data === "object") {
|
|
4830
|
+
console.log("\u{1F464} useUserData - Found object format, checking for items");
|
|
4831
|
+
if (response.data.items && Array.isArray(response.data.items) && response.data.items.length > 0) {
|
|
4832
|
+
console.log("\u{1F464} useUserData - Found items in object format");
|
|
4833
|
+
userData2 = response.data.items[0];
|
|
4834
|
+
} else {
|
|
4835
|
+
console.log("\u{1F464} useUserData - No items found in object format");
|
|
4836
|
+
}
|
|
4837
|
+
} else if (response.data?.data?.response?.data) {
|
|
4838
|
+
console.log("\u{1F464} useUserData - Found double-nested data.data.response.data format");
|
|
4839
|
+
try {
|
|
4840
|
+
const rawData = response.data.data.response.data;
|
|
4841
|
+
console.log("\u{1F464} useUserData - Raw double-nested data:", rawData);
|
|
4842
|
+
const parsedData = typeof rawData === "string" ? JSON.parse(rawData) : rawData;
|
|
4843
|
+
console.log("\u{1F464} useUserData - Parsed double-nested data:", parsedData);
|
|
4844
|
+
if (parsedData && parsedData.items && Array.isArray(parsedData.items) && parsedData.items.length > 0) {
|
|
4845
|
+
userData2 = parsedData.items[0];
|
|
4846
|
+
console.log("\u{1F464} useUserData - Extracted user from double-nested items:", userData2);
|
|
4847
|
+
}
|
|
4848
|
+
} catch (parseError) {
|
|
4849
|
+
console.error("\u{1F464} useUserData - Error parsing double-nested response data:", parseError);
|
|
4850
|
+
}
|
|
4851
|
+
}
|
|
4852
|
+
}
|
|
4853
|
+
if (userData2) {
|
|
4854
|
+
console.log("\u{1F464} useUserData - User data found:", userData2);
|
|
4855
|
+
setUserData(userData2);
|
|
4856
|
+
setError(null);
|
|
4857
|
+
retryCountRef.current = 0;
|
|
4858
|
+
console.log("\u{1F464} useUserData - Profile loaded successfully:", userData2);
|
|
4859
|
+
} else {
|
|
4860
|
+
setError("User profile not found in database");
|
|
4861
|
+
setUserData(null);
|
|
4862
|
+
console.warn("\u{1F464} useUserData - User not found for email:", userEmail);
|
|
4863
|
+
}
|
|
4864
|
+
}
|
|
4865
|
+
} catch (err) {
|
|
4866
|
+
if (currentRequestId === requestIdRef.current && mountedRef.current) {
|
|
4867
|
+
const error2 = err;
|
|
4868
|
+
console.error("\u{1F464} useUserData - Error fetching profile:", error2);
|
|
4869
|
+
if (error2.name === "AbortError") {
|
|
4870
|
+
return;
|
|
4871
|
+
}
|
|
4872
|
+
const shouldRetry = retryOnError && retryCountRef.current < maxRetries && (error2.message?.includes("Network Error") || error2.message?.includes("Failed to fetch"));
|
|
4873
|
+
if (shouldRetry) {
|
|
4874
|
+
retryCountRef.current++;
|
|
4875
|
+
console.log(`\u{1F464} useUserData - Retrying profile fetch (${retryCountRef.current}/${maxRetries})`);
|
|
4876
|
+
setTimeout(() => {
|
|
4877
|
+
if (mountedRef.current) {
|
|
4878
|
+
refreshProfile();
|
|
4879
|
+
}
|
|
4880
|
+
}, 1e3 * retryCountRef.current);
|
|
4881
|
+
} else {
|
|
4882
|
+
setError("Failed to load user profile from database");
|
|
4883
|
+
setUserData(null);
|
|
4884
|
+
}
|
|
4885
|
+
}
|
|
4886
|
+
} finally {
|
|
4887
|
+
if (currentRequestId === requestIdRef.current && mountedRef.current) {
|
|
4888
|
+
setLoading(false);
|
|
4889
|
+
}
|
|
4890
|
+
if (abortControllerRef.current === abortController) {
|
|
4891
|
+
abortControllerRef.current = null;
|
|
4892
|
+
}
|
|
4893
|
+
}
|
|
4894
|
+
}, [isInitialized, getUserEmail, retryOnError, maxRetries]);
|
|
4895
|
+
(0, import_react19.useEffect)(() => {
|
|
4896
|
+
if (autoFetch && isAuthenticated && isInitialized) {
|
|
4897
|
+
refreshProfile();
|
|
4898
|
+
} else if (!isAuthenticated) {
|
|
4899
|
+
clearProfile();
|
|
4900
|
+
}
|
|
4901
|
+
}, [autoFetch, isAuthenticated, isInitialized, refreshProfile, clearProfile]);
|
|
4902
|
+
(0, import_react19.useEffect)(() => {
|
|
4903
|
+
mountedRef.current = true;
|
|
4904
|
+
return () => {
|
|
4905
|
+
mountedRef.current = false;
|
|
4906
|
+
if (abortControllerRef.current) {
|
|
4907
|
+
abortControllerRef.current.abort();
|
|
4908
|
+
abortControllerRef.current = null;
|
|
4909
|
+
}
|
|
4910
|
+
};
|
|
4911
|
+
}, []);
|
|
4912
|
+
const user = {
|
|
4913
|
+
session: sessionData,
|
|
4914
|
+
// Usar sessionData del nuevo sistema
|
|
4915
|
+
data: userData
|
|
4916
|
+
// Mantener userData del database igual que legacy
|
|
4917
|
+
};
|
|
4918
|
+
return {
|
|
4919
|
+
user,
|
|
4920
|
+
loading,
|
|
4921
|
+
error,
|
|
4922
|
+
refreshProfile,
|
|
4923
|
+
clearProfile
|
|
4924
|
+
};
|
|
4925
|
+
};
|
|
4926
|
+
|
|
4927
|
+
// src/hooks/useAuth.ts
|
|
4928
|
+
var import_react20 = require("react");
|
|
4929
|
+
var useAuth = () => {
|
|
4930
|
+
const {
|
|
4931
|
+
isAuthenticated,
|
|
4932
|
+
isLoading,
|
|
4933
|
+
isInitialized,
|
|
4934
|
+
tokens,
|
|
4935
|
+
error,
|
|
4936
|
+
sessionData,
|
|
4937
|
+
login,
|
|
4938
|
+
logout,
|
|
4939
|
+
refreshTokens,
|
|
4940
|
+
clearError,
|
|
4941
|
+
getTokenInfo,
|
|
4942
|
+
isExpiringSoon,
|
|
4943
|
+
expiresIn,
|
|
4944
|
+
refreshExpiresIn
|
|
4945
|
+
} = useSessionContext();
|
|
4946
|
+
const setToken = (0, import_react20.useCallback)((token) => {
|
|
4947
|
+
if (token) {
|
|
4948
|
+
console.warn("useAuth.setToken() is deprecated. Use login() method instead for better security.");
|
|
4949
|
+
} else {
|
|
4950
|
+
logout();
|
|
4951
|
+
}
|
|
4952
|
+
}, [logout]);
|
|
4953
|
+
const tokenExpiration = tokens?.expiresAt ? new Date(tokens.expiresAt) : null;
|
|
4954
|
+
return {
|
|
4955
|
+
// Estado básico (compatible con legacy)
|
|
4956
|
+
isAuthenticated,
|
|
4957
|
+
loading: isLoading,
|
|
4958
|
+
// En el nuevo sistema se llama isLoading
|
|
4959
|
+
error,
|
|
4960
|
+
// Datos del token (compatible con legacy + mejorado)
|
|
4961
|
+
token: tokens?.accessToken || null,
|
|
4962
|
+
user: sessionData,
|
|
4963
|
+
// sessionData del nuevo sistema (más rico que legacy)
|
|
4964
|
+
tokenExpiration,
|
|
4965
|
+
// Acciones (compatible con legacy + mejorado)
|
|
4966
|
+
setToken,
|
|
4967
|
+
logout,
|
|
4968
|
+
refreshToken: refreshTokens,
|
|
4969
|
+
// Funcionalidad real en el nuevo sistema
|
|
4970
|
+
// Nuevas funcionalidades del sistema de refresh tokens
|
|
4971
|
+
login,
|
|
4972
|
+
isExpiringSoon,
|
|
4973
|
+
expiresIn,
|
|
4974
|
+
refreshExpiresIn,
|
|
4975
|
+
getTokenInfo,
|
|
4976
|
+
clearError
|
|
4977
|
+
};
|
|
4978
|
+
};
|
|
4979
|
+
|
|
4980
|
+
// src/hooks/useData.ts
|
|
4981
|
+
var import_react21 = require("react");
|
|
4982
|
+
var import_crudify_browser10 = __toESM(require("@nocios/crudify-browser"));
|
|
4983
|
+
var useData = () => {
|
|
4984
|
+
const {
|
|
4985
|
+
isInitialized,
|
|
4986
|
+
isLoading,
|
|
4987
|
+
error,
|
|
4988
|
+
isAuthenticated,
|
|
4989
|
+
login: sessionLogin
|
|
4990
|
+
} = useSessionContext();
|
|
4991
|
+
const isReady = (0, import_react21.useCallback)(() => {
|
|
4992
|
+
return isInitialized && !isLoading && !error;
|
|
4993
|
+
}, [isInitialized, isLoading, error]);
|
|
4994
|
+
const waitForReady = (0, import_react21.useCallback)(async () => {
|
|
4995
|
+
return new Promise((resolve, reject) => {
|
|
4996
|
+
const checkReady = () => {
|
|
4997
|
+
if (isReady()) {
|
|
4998
|
+
resolve();
|
|
4999
|
+
} else if (error) {
|
|
5000
|
+
reject(new Error(error));
|
|
5001
|
+
} else {
|
|
5002
|
+
setTimeout(checkReady, 100);
|
|
5003
|
+
}
|
|
5004
|
+
};
|
|
5005
|
+
checkReady();
|
|
5006
|
+
});
|
|
5007
|
+
}, [isReady, error]);
|
|
5008
|
+
const ensureReady = (0, import_react21.useCallback)(async () => {
|
|
5009
|
+
if (!isReady()) {
|
|
5010
|
+
throw new Error("System not ready. Check isInitialized, isLoading, and error states.");
|
|
5011
|
+
}
|
|
5012
|
+
}, [isReady]);
|
|
5013
|
+
const readItems = (0, import_react21.useCallback)(async (moduleKey, filter, options) => {
|
|
5014
|
+
await ensureReady();
|
|
5015
|
+
return await import_crudify_browser10.default.readItems(moduleKey, filter || {}, options);
|
|
5016
|
+
}, [ensureReady]);
|
|
5017
|
+
const readItem = (0, import_react21.useCallback)(async (moduleKey, filter, options) => {
|
|
5018
|
+
await ensureReady();
|
|
5019
|
+
return await import_crudify_browser10.default.readItem(moduleKey, filter, options);
|
|
5020
|
+
}, [ensureReady]);
|
|
5021
|
+
const createItem = (0, import_react21.useCallback)(async (moduleKey, data, options) => {
|
|
5022
|
+
await ensureReady();
|
|
5023
|
+
return await import_crudify_browser10.default.createItem(moduleKey, data, options);
|
|
5024
|
+
}, [ensureReady]);
|
|
5025
|
+
const updateItem = (0, import_react21.useCallback)(async (moduleKey, data, options) => {
|
|
5026
|
+
await ensureReady();
|
|
5027
|
+
return await import_crudify_browser10.default.updateItem(moduleKey, data, options);
|
|
5028
|
+
}, [ensureReady]);
|
|
5029
|
+
const deleteItem = (0, import_react21.useCallback)(async (moduleKey, id, options) => {
|
|
5030
|
+
await ensureReady();
|
|
5031
|
+
return await import_crudify_browser10.default.deleteItem(moduleKey, id, options);
|
|
5032
|
+
}, [ensureReady]);
|
|
5033
|
+
const transaction = (0, import_react21.useCallback)(async (operations, options) => {
|
|
5034
|
+
await ensureReady();
|
|
5035
|
+
return await import_crudify_browser10.default.transaction(operations, options);
|
|
5036
|
+
}, [ensureReady]);
|
|
5037
|
+
const login = (0, import_react21.useCallback)(async (email, password) => {
|
|
5038
|
+
try {
|
|
5039
|
+
const result = await sessionLogin(email, password);
|
|
5040
|
+
if (result.success) {
|
|
5041
|
+
return {
|
|
5042
|
+
success: true,
|
|
5043
|
+
data: result.tokens
|
|
5044
|
+
};
|
|
5045
|
+
} else {
|
|
5046
|
+
return {
|
|
5047
|
+
success: false,
|
|
5048
|
+
errors: result.error || "Login failed"
|
|
5049
|
+
};
|
|
5050
|
+
}
|
|
5051
|
+
} catch (error2) {
|
|
5052
|
+
return {
|
|
5053
|
+
success: false,
|
|
5054
|
+
errors: error2 instanceof Error ? error2.message : "Login failed"
|
|
5055
|
+
};
|
|
5056
|
+
}
|
|
5057
|
+
}, [sessionLogin]);
|
|
5058
|
+
return {
|
|
5059
|
+
// CRUD operations
|
|
5060
|
+
readItems,
|
|
5061
|
+
readItem,
|
|
5062
|
+
createItem,
|
|
5063
|
+
updateItem,
|
|
5064
|
+
deleteItem,
|
|
5065
|
+
transaction,
|
|
5066
|
+
login,
|
|
5067
|
+
// Estado
|
|
5068
|
+
isInitialized,
|
|
5069
|
+
isInitializing: isLoading,
|
|
5070
|
+
// El nuevo sistema usa isLoading
|
|
5071
|
+
initializationError: error,
|
|
5072
|
+
// Utilidades
|
|
5073
|
+
isReady,
|
|
5074
|
+
waitForReady
|
|
5075
|
+
};
|
|
5076
|
+
};
|
|
4707
5077
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4708
5078
|
0 && (module.exports = {
|
|
4709
5079
|
CrudifyDataProvider,
|
|
@@ -4735,6 +5105,7 @@ function SessionStatus() {
|
|
|
4735
5105
|
secureLocalStorage,
|
|
4736
5106
|
secureSessionStorage,
|
|
4737
5107
|
tokenManager,
|
|
5108
|
+
useAuth,
|
|
4738
5109
|
useCrudifyAuth,
|
|
4739
5110
|
useCrudifyConfig,
|
|
4740
5111
|
useCrudifyData,
|
|
@@ -4742,8 +5113,10 @@ function SessionStatus() {
|
|
|
4742
5113
|
useCrudifyInstance,
|
|
4743
5114
|
useCrudifyLogin,
|
|
4744
5115
|
useCrudifyUser,
|
|
5116
|
+
useData,
|
|
4745
5117
|
useSession,
|
|
4746
5118
|
useSessionContext,
|
|
5119
|
+
useUserData,
|
|
4747
5120
|
useUserProfile,
|
|
4748
5121
|
...require("@nocios/crudify-browser")
|
|
4749
5122
|
});
|