@nocios/crudify-ui 1.2.22 → 1.2.28
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/index.d.mts +16 -20
- package/dist/index.d.ts +16 -20
- package/dist/index.js +96 -112
- package/dist/index.mjs +96 -112
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -535,24 +535,19 @@ interface UseCrudifyAuthReturn {
|
|
|
535
535
|
declare const useCrudifyAuth: () => UseCrudifyAuthReturn;
|
|
536
536
|
|
|
537
537
|
/**
|
|
538
|
-
*
|
|
538
|
+
* Complete user data structure
|
|
539
539
|
*/
|
|
540
|
-
interface
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
displayData: Record<string, any>;
|
|
540
|
+
interface CrudifyUserData {
|
|
541
|
+
session: JWTPayload$1 | null;
|
|
542
|
+
data: UserProfile | null;
|
|
544
543
|
}
|
|
545
544
|
/**
|
|
546
545
|
* Return type for useCrudifyUser hook
|
|
547
546
|
*/
|
|
548
547
|
interface UseCrudifyUserReturn {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
userProfile: UserProfile | null;
|
|
553
|
-
profileLoading: boolean;
|
|
554
|
-
profileError: string | null;
|
|
555
|
-
extendedData: ExtendedUserData;
|
|
548
|
+
user: CrudifyUserData;
|
|
549
|
+
loading: boolean;
|
|
550
|
+
error: string | null;
|
|
556
551
|
refreshProfile: () => Promise<void>;
|
|
557
552
|
clearProfile: () => void;
|
|
558
553
|
}
|
|
@@ -588,19 +583,20 @@ interface UseCrudifyUserOptions {
|
|
|
588
583
|
* ```tsx
|
|
589
584
|
* function UserProfilePage() {
|
|
590
585
|
* const {
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
* extendedData,
|
|
586
|
+
* user,
|
|
587
|
+
* loading,
|
|
588
|
+
* error,
|
|
595
589
|
* refreshProfile
|
|
596
590
|
* } = useCrudifyUser({ autoFetch: true });
|
|
597
591
|
*
|
|
598
|
-
* if (
|
|
592
|
+
* if (loading) return <LoadingSpinner />;
|
|
593
|
+
* if (error) return <div>Error: {error}</div>;
|
|
599
594
|
*
|
|
600
595
|
* return (
|
|
601
596
|
* <div>
|
|
602
|
-
* <h1>Welcome {
|
|
603
|
-
* <p>
|
|
597
|
+
* <h1>Welcome {user.data?.fullName || user.session?.email}</h1>
|
|
598
|
+
* <p>Session data: {JSON.stringify(user.session, null, 2)}</p>
|
|
599
|
+
* <p>Profile data: {JSON.stringify(user.data, null, 2)}</p>
|
|
604
600
|
* <button onClick={refreshProfile}>Refresh</button>
|
|
605
601
|
* </div>
|
|
606
602
|
* );
|
|
@@ -921,4 +917,4 @@ declare function parseJavaScriptError(error: unknown): ParsedError;
|
|
|
921
917
|
*/
|
|
922
918
|
declare function handleCrudifyError(error: unknown): ParsedError[];
|
|
923
919
|
|
|
924
|
-
export { type ApiError, type BoxScreenType, type CrudifyApiResponse, type CrudifyConfig, type CrudifyDataContextState, CrudifyDataProvider, type CrudifyDataProviderProps, CrudifyLogin, type CrudifyLoginConfig, type CrudifyLoginProps, type CrudifyLoginTranslations, type CrudifyTransactionResponse, ERROR_CODES, ERROR_SEVERITY_MAP, type ErrorCode, type ErrorSeverity, type
|
|
920
|
+
export { type ApiError, type BoxScreenType, type CrudifyApiResponse, type CrudifyConfig, type CrudifyDataContextState, CrudifyDataProvider, type CrudifyDataProviderProps, CrudifyLogin, type CrudifyLoginConfig, type CrudifyLoginProps, type CrudifyLoginTranslations, type CrudifyTransactionResponse, type CrudifyUserData, ERROR_CODES, ERROR_SEVERITY_MAP, type ErrorCode, type ErrorSeverity, type ForgotPasswordRequest, type JWTPayload$1 as JWTPayload, type JwtPayload, type LoginRequest, type LoginResponse, type ParsedError, type ResetPasswordRequest, type ResolvedConfig, type TransactionResponseData, type UseCrudifyAuthReturn, type UseCrudifyConfigReturn, type UseCrudifyDataReturn, type UseCrudifyInstanceReturn, type UseCrudifyUserOptions, type UseCrudifyUserReturn, type UserLoginData, type UserProfile, UserProfileDisplay, type ValidateCodeRequest, type ValidationError, configurationManager, crudifyInitializer, decodeJwtSafely, getCookie, getCrudifyInstanceAsync, getCrudifyInstanceSync, getCurrentUserEmail, getErrorMessage, handleCrudifyError, isTokenExpired, parseApiError, parseJavaScriptError, parseTransactionError, secureLocalStorage, secureSessionStorage, tokenManager, useCrudifyAuth, useCrudifyConfig, useCrudifyData, useCrudifyDataContext, useCrudifyInstance, useCrudifyLogin, useCrudifyUser, useUserProfile };
|
package/dist/index.d.ts
CHANGED
|
@@ -535,24 +535,19 @@ interface UseCrudifyAuthReturn {
|
|
|
535
535
|
declare const useCrudifyAuth: () => UseCrudifyAuthReturn;
|
|
536
536
|
|
|
537
537
|
/**
|
|
538
|
-
*
|
|
538
|
+
* Complete user data structure
|
|
539
539
|
*/
|
|
540
|
-
interface
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
displayData: Record<string, any>;
|
|
540
|
+
interface CrudifyUserData {
|
|
541
|
+
session: JWTPayload$1 | null;
|
|
542
|
+
data: UserProfile | null;
|
|
544
543
|
}
|
|
545
544
|
/**
|
|
546
545
|
* Return type for useCrudifyUser hook
|
|
547
546
|
*/
|
|
548
547
|
interface UseCrudifyUserReturn {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
userProfile: UserProfile | null;
|
|
553
|
-
profileLoading: boolean;
|
|
554
|
-
profileError: string | null;
|
|
555
|
-
extendedData: ExtendedUserData;
|
|
548
|
+
user: CrudifyUserData;
|
|
549
|
+
loading: boolean;
|
|
550
|
+
error: string | null;
|
|
556
551
|
refreshProfile: () => Promise<void>;
|
|
557
552
|
clearProfile: () => void;
|
|
558
553
|
}
|
|
@@ -588,19 +583,20 @@ interface UseCrudifyUserOptions {
|
|
|
588
583
|
* ```tsx
|
|
589
584
|
* function UserProfilePage() {
|
|
590
585
|
* const {
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
* extendedData,
|
|
586
|
+
* user,
|
|
587
|
+
* loading,
|
|
588
|
+
* error,
|
|
595
589
|
* refreshProfile
|
|
596
590
|
* } = useCrudifyUser({ autoFetch: true });
|
|
597
591
|
*
|
|
598
|
-
* if (
|
|
592
|
+
* if (loading) return <LoadingSpinner />;
|
|
593
|
+
* if (error) return <div>Error: {error}</div>;
|
|
599
594
|
*
|
|
600
595
|
* return (
|
|
601
596
|
* <div>
|
|
602
|
-
* <h1>Welcome {
|
|
603
|
-
* <p>
|
|
597
|
+
* <h1>Welcome {user.data?.fullName || user.session?.email}</h1>
|
|
598
|
+
* <p>Session data: {JSON.stringify(user.session, null, 2)}</p>
|
|
599
|
+
* <p>Profile data: {JSON.stringify(user.data, null, 2)}</p>
|
|
604
600
|
* <button onClick={refreshProfile}>Refresh</button>
|
|
605
601
|
* </div>
|
|
606
602
|
* );
|
|
@@ -921,4 +917,4 @@ declare function parseJavaScriptError(error: unknown): ParsedError;
|
|
|
921
917
|
*/
|
|
922
918
|
declare function handleCrudifyError(error: unknown): ParsedError[];
|
|
923
919
|
|
|
924
|
-
export { type ApiError, type BoxScreenType, type CrudifyApiResponse, type CrudifyConfig, type CrudifyDataContextState, CrudifyDataProvider, type CrudifyDataProviderProps, CrudifyLogin, type CrudifyLoginConfig, type CrudifyLoginProps, type CrudifyLoginTranslations, type CrudifyTransactionResponse, ERROR_CODES, ERROR_SEVERITY_MAP, type ErrorCode, type ErrorSeverity, type
|
|
920
|
+
export { type ApiError, type BoxScreenType, type CrudifyApiResponse, type CrudifyConfig, type CrudifyDataContextState, CrudifyDataProvider, type CrudifyDataProviderProps, CrudifyLogin, type CrudifyLoginConfig, type CrudifyLoginProps, type CrudifyLoginTranslations, type CrudifyTransactionResponse, type CrudifyUserData, ERROR_CODES, ERROR_SEVERITY_MAP, type ErrorCode, type ErrorSeverity, type ForgotPasswordRequest, type JWTPayload$1 as JWTPayload, type JwtPayload, type LoginRequest, type LoginResponse, type ParsedError, type ResetPasswordRequest, type ResolvedConfig, type TransactionResponseData, type UseCrudifyAuthReturn, type UseCrudifyConfigReturn, type UseCrudifyDataReturn, type UseCrudifyInstanceReturn, type UseCrudifyUserOptions, type UseCrudifyUserReturn, type UserLoginData, type UserProfile, UserProfileDisplay, type ValidateCodeRequest, type ValidationError, configurationManager, crudifyInitializer, decodeJwtSafely, getCookie, getCrudifyInstanceAsync, getCrudifyInstanceSync, getCurrentUserEmail, getErrorMessage, handleCrudifyError, isTokenExpired, parseApiError, parseJavaScriptError, parseTransactionError, secureLocalStorage, secureSessionStorage, tokenManager, useCrudifyAuth, useCrudifyConfig, useCrudifyData, useCrudifyDataContext, useCrudifyInstance, useCrudifyLogin, useCrudifyUser, useUserProfile };
|
package/dist/index.js
CHANGED
|
@@ -3272,57 +3272,37 @@ init_CrudifyDataProvider();
|
|
|
3272
3272
|
var useCrudifyUser = (options = {}) => {
|
|
3273
3273
|
const { autoFetch = true, retryOnError = false, maxRetries = 3 } = options;
|
|
3274
3274
|
const { isAuthenticated, isInitialized, user: jwtUser, token } = useCrudifyDataContext();
|
|
3275
|
-
const [
|
|
3276
|
-
const [
|
|
3277
|
-
const [
|
|
3278
|
-
const [extendedData, setExtendedData] = (0, import_react13.useState)({
|
|
3279
|
-
fullProfile: null,
|
|
3280
|
-
totalFields: 0,
|
|
3281
|
-
displayData: {}
|
|
3282
|
-
});
|
|
3275
|
+
const [userData, setUserData] = (0, import_react13.useState)(null);
|
|
3276
|
+
const [loading, setLoading] = (0, import_react13.useState)(false);
|
|
3277
|
+
const [error, setError] = (0, import_react13.useState)(null);
|
|
3283
3278
|
const abortControllerRef = (0, import_react13.useRef)(null);
|
|
3284
3279
|
const mountedRef = (0, import_react13.useRef)(true);
|
|
3285
3280
|
const requestIdRef = (0, import_react13.useRef)(0);
|
|
3286
3281
|
const retryCountRef = (0, import_react13.useRef)(0);
|
|
3287
|
-
const
|
|
3288
|
-
if (!jwtUser)
|
|
3289
|
-
|
|
3290
|
-
userEmail: null,
|
|
3291
|
-
userId: null,
|
|
3292
|
-
userIdentifier: null
|
|
3293
|
-
};
|
|
3294
|
-
}
|
|
3295
|
-
return {
|
|
3296
|
-
userEmail: jwtUser.email || jwtUser["cognito:username"] || null,
|
|
3297
|
-
userId: jwtUser.sub || null,
|
|
3298
|
-
userIdentifier: jwtUser["cognito:username"] || jwtUser.email || jwtUser.sub || null
|
|
3299
|
-
};
|
|
3282
|
+
const getUserEmailFromJWT = (0, import_react13.useCallback)(() => {
|
|
3283
|
+
if (!jwtUser) return null;
|
|
3284
|
+
return jwtUser.email || jwtUser["cognito:username"] || null;
|
|
3300
3285
|
}, [jwtUser]);
|
|
3301
3286
|
const clearProfile = (0, import_react13.useCallback)(() => {
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
setExtendedData({
|
|
3306
|
-
fullProfile: null,
|
|
3307
|
-
totalFields: 0,
|
|
3308
|
-
displayData: {}
|
|
3309
|
-
});
|
|
3287
|
+
setUserData(null);
|
|
3288
|
+
setError(null);
|
|
3289
|
+
setLoading(false);
|
|
3310
3290
|
retryCountRef.current = 0;
|
|
3311
3291
|
}, []);
|
|
3312
3292
|
const refreshProfile = (0, import_react13.useCallback)(async () => {
|
|
3313
|
-
const
|
|
3314
|
-
console.log("\u{1F464} useCrudifyUser - Refreshing profile for:",
|
|
3315
|
-
if (!
|
|
3293
|
+
const userEmail = getUserEmailFromJWT();
|
|
3294
|
+
console.log("\u{1F464} useCrudifyUser - Refreshing profile for:", userEmail);
|
|
3295
|
+
if (!userEmail) {
|
|
3316
3296
|
if (mountedRef.current) {
|
|
3317
|
-
|
|
3318
|
-
|
|
3297
|
+
setError("No user email available from JWT token");
|
|
3298
|
+
setLoading(false);
|
|
3319
3299
|
}
|
|
3320
3300
|
return;
|
|
3321
3301
|
}
|
|
3322
3302
|
if (!isInitialized) {
|
|
3323
3303
|
if (mountedRef.current) {
|
|
3324
|
-
|
|
3325
|
-
|
|
3304
|
+
setError("Crudify not initialized");
|
|
3305
|
+
setLoading(false);
|
|
3326
3306
|
}
|
|
3327
3307
|
return;
|
|
3328
3308
|
}
|
|
@@ -3334,77 +3314,91 @@ var useCrudifyUser = (options = {}) => {
|
|
|
3334
3314
|
const currentRequestId = ++requestIdRef.current;
|
|
3335
3315
|
try {
|
|
3336
3316
|
if (mountedRef.current) {
|
|
3337
|
-
|
|
3338
|
-
|
|
3317
|
+
setLoading(true);
|
|
3318
|
+
setError(null);
|
|
3339
3319
|
}
|
|
3340
3320
|
console.log("\u{1F464} useCrudifyUser - Fetching profile data from database");
|
|
3341
3321
|
const response = await import_crudify_browser5.default.readItems("users", {
|
|
3342
|
-
filter: { email:
|
|
3322
|
+
filter: { email: userEmail },
|
|
3343
3323
|
pagination: { limit: 1 }
|
|
3344
3324
|
});
|
|
3345
3325
|
console.log("\u{1F464} useCrudifyUser - Database response:", response);
|
|
3326
|
+
console.log("\u{1F464} useCrudifyUser - response.data:", response.data);
|
|
3327
|
+
console.log("\u{1F464} useCrudifyUser - response.data type:", typeof response.data);
|
|
3346
3328
|
if (currentRequestId === requestIdRef.current && mountedRef.current && !abortController.signal.aborted) {
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
"role",
|
|
3376
|
-
"permissions",
|
|
3377
|
-
"isActive",
|
|
3378
|
-
"lastLogin",
|
|
3379
|
-
"createdAt",
|
|
3380
|
-
"updatedAt"
|
|
3381
|
-
].includes(key)
|
|
3382
|
-
).reduce((acc, key) => ({ ...acc, [key]: userData[key] }), {})
|
|
3329
|
+
let userData2 = null;
|
|
3330
|
+
if (response.success) {
|
|
3331
|
+
console.log("\u{1F464} useCrudifyUser - Processing successful response:", {
|
|
3332
|
+
dataType: typeof response.data,
|
|
3333
|
+
isArray: Array.isArray(response.data),
|
|
3334
|
+
hasResponse: !!response.data?.response,
|
|
3335
|
+
hasResponseData: !!response.data?.response?.data,
|
|
3336
|
+
responseDataType: typeof response.data?.response?.data
|
|
3337
|
+
});
|
|
3338
|
+
if (Array.isArray(response.data) && response.data.length > 0) {
|
|
3339
|
+
console.log("\u{1F464} useCrudifyUser - Found direct array format");
|
|
3340
|
+
userData2 = response.data[0];
|
|
3341
|
+
} else if (response.data?.response?.data) {
|
|
3342
|
+
console.log("\u{1F464} useCrudifyUser - Found nested response.data format");
|
|
3343
|
+
try {
|
|
3344
|
+
const rawData = response.data.response.data;
|
|
3345
|
+
console.log("\u{1F464} useCrudifyUser - Raw nested data:", rawData);
|
|
3346
|
+
console.log("\u{1F464} useCrudifyUser - Raw data type:", typeof rawData);
|
|
3347
|
+
const parsedData = typeof rawData === "string" ? JSON.parse(rawData) : rawData;
|
|
3348
|
+
console.log("\u{1F464} useCrudifyUser - Parsed nested data:", parsedData);
|
|
3349
|
+
if (parsedData && parsedData.items && Array.isArray(parsedData.items) && parsedData.items.length > 0) {
|
|
3350
|
+
userData2 = parsedData.items[0];
|
|
3351
|
+
console.log("\u{1F464} useCrudifyUser - Extracted user from nested items:", userData2);
|
|
3352
|
+
} else {
|
|
3353
|
+
console.log("\u{1F464} useCrudifyUser - No items found in parsed data or items array is empty");
|
|
3354
|
+
}
|
|
3355
|
+
} catch (parseError) {
|
|
3356
|
+
console.error("\u{1F464} useCrudifyUser - Error parsing nested response data:", parseError);
|
|
3383
3357
|
}
|
|
3384
|
-
}
|
|
3385
|
-
|
|
3386
|
-
|
|
3358
|
+
} else if (response.data && typeof response.data === "object") {
|
|
3359
|
+
console.log("\u{1F464} useCrudifyUser - Found object format, checking for items");
|
|
3360
|
+
if (response.data.items && Array.isArray(response.data.items) && response.data.items.length > 0) {
|
|
3361
|
+
console.log("\u{1F464} useCrudifyUser - Found items in object format");
|
|
3362
|
+
userData2 = response.data.items[0];
|
|
3363
|
+
} else {
|
|
3364
|
+
console.log("\u{1F464} useCrudifyUser - No items found in object format");
|
|
3365
|
+
}
|
|
3366
|
+
} else if (response.data?.data?.response?.data) {
|
|
3367
|
+
console.log("\u{1F464} useCrudifyUser - Found double-nested data.data.response.data format");
|
|
3368
|
+
try {
|
|
3369
|
+
const rawData = response.data.data.response.data;
|
|
3370
|
+
console.log("\u{1F464} useCrudifyUser - Raw double-nested data:", rawData);
|
|
3371
|
+
const parsedData = typeof rawData === "string" ? JSON.parse(rawData) : rawData;
|
|
3372
|
+
console.log("\u{1F464} useCrudifyUser - Parsed double-nested data:", parsedData);
|
|
3373
|
+
if (parsedData && parsedData.items && Array.isArray(parsedData.items) && parsedData.items.length > 0) {
|
|
3374
|
+
userData2 = parsedData.items[0];
|
|
3375
|
+
console.log("\u{1F464} useCrudifyUser - Extracted user from double-nested items:", userData2);
|
|
3376
|
+
}
|
|
3377
|
+
} catch (parseError) {
|
|
3378
|
+
console.error("\u{1F464} useCrudifyUser - Error parsing double-nested response data:", parseError);
|
|
3379
|
+
}
|
|
3380
|
+
}
|
|
3381
|
+
}
|
|
3382
|
+
if (userData2) {
|
|
3383
|
+
console.log("\u{1F464} useCrudifyUser - User data found:", userData2);
|
|
3384
|
+
setUserData(userData2);
|
|
3385
|
+
setError(null);
|
|
3387
3386
|
retryCountRef.current = 0;
|
|
3388
|
-
console.log("\u{1F464} useCrudifyUser - Profile loaded successfully:",
|
|
3387
|
+
console.log("\u{1F464} useCrudifyUser - Profile loaded successfully:", userData2);
|
|
3389
3388
|
} else {
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
fullProfile: null,
|
|
3394
|
-
totalFields: 0,
|
|
3395
|
-
displayData: {}
|
|
3396
|
-
});
|
|
3397
|
-
console.warn("\u{1F464} useCrudifyUser - User not found for email:", userEmail2);
|
|
3389
|
+
setError("User profile not found in database");
|
|
3390
|
+
setUserData(null);
|
|
3391
|
+
console.warn("\u{1F464} useCrudifyUser - User not found for email:", userEmail);
|
|
3398
3392
|
}
|
|
3399
3393
|
}
|
|
3400
3394
|
} catch (err) {
|
|
3401
3395
|
if (currentRequestId === requestIdRef.current && mountedRef.current) {
|
|
3402
|
-
const
|
|
3403
|
-
console.error("\u{1F464} useCrudifyUser - Error fetching profile:",
|
|
3404
|
-
if (
|
|
3396
|
+
const error2 = err;
|
|
3397
|
+
console.error("\u{1F464} useCrudifyUser - Error fetching profile:", error2);
|
|
3398
|
+
if (error2.name === "AbortError") {
|
|
3405
3399
|
return;
|
|
3406
3400
|
}
|
|
3407
|
-
const shouldRetry = retryOnError && retryCountRef.current < maxRetries && (
|
|
3401
|
+
const shouldRetry = retryOnError && retryCountRef.current < maxRetries && (error2.message?.includes("Network Error") || error2.message?.includes("Failed to fetch"));
|
|
3408
3402
|
if (shouldRetry) {
|
|
3409
3403
|
retryCountRef.current++;
|
|
3410
3404
|
console.log(`\u{1F464} useCrudifyUser - Retrying profile fetch (${retryCountRef.current}/${maxRetries})`);
|
|
@@ -3414,24 +3408,19 @@ var useCrudifyUser = (options = {}) => {
|
|
|
3414
3408
|
}
|
|
3415
3409
|
}, 1e3 * retryCountRef.current);
|
|
3416
3410
|
} else {
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
setExtendedData({
|
|
3420
|
-
fullProfile: null,
|
|
3421
|
-
totalFields: 0,
|
|
3422
|
-
displayData: {}
|
|
3423
|
-
});
|
|
3411
|
+
setError("Failed to load user profile from database");
|
|
3412
|
+
setUserData(null);
|
|
3424
3413
|
}
|
|
3425
3414
|
}
|
|
3426
3415
|
} finally {
|
|
3427
3416
|
if (currentRequestId === requestIdRef.current && mountedRef.current) {
|
|
3428
|
-
|
|
3417
|
+
setLoading(false);
|
|
3429
3418
|
}
|
|
3430
3419
|
if (abortControllerRef.current === abortController) {
|
|
3431
3420
|
abortControllerRef.current = null;
|
|
3432
3421
|
}
|
|
3433
3422
|
}
|
|
3434
|
-
}, [isInitialized,
|
|
3423
|
+
}, [isInitialized, getUserEmailFromJWT, retryOnError, maxRetries]);
|
|
3435
3424
|
(0, import_react13.useEffect)(() => {
|
|
3436
3425
|
if (autoFetch && isAuthenticated && isInitialized) {
|
|
3437
3426
|
refreshProfile();
|
|
@@ -3449,19 +3438,14 @@ var useCrudifyUser = (options = {}) => {
|
|
|
3449
3438
|
}
|
|
3450
3439
|
};
|
|
3451
3440
|
}, []);
|
|
3452
|
-
const
|
|
3441
|
+
const user = {
|
|
3442
|
+
session: jwtUser,
|
|
3443
|
+
data: userData
|
|
3444
|
+
};
|
|
3453
3445
|
return {
|
|
3454
|
-
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
userIdentifier,
|
|
3458
|
-
// Perfil completo desde BD
|
|
3459
|
-
userProfile,
|
|
3460
|
-
profileLoading,
|
|
3461
|
-
profileError,
|
|
3462
|
-
// Datos extendidos formateados
|
|
3463
|
-
extendedData,
|
|
3464
|
-
// Acciones
|
|
3446
|
+
user,
|
|
3447
|
+
loading,
|
|
3448
|
+
error,
|
|
3465
3449
|
refreshProfile,
|
|
3466
3450
|
clearProfile
|
|
3467
3451
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -2024,57 +2024,37 @@ import crudify3 from "@nocios/crudify-browser";
|
|
|
2024
2024
|
var useCrudifyUser = (options = {}) => {
|
|
2025
2025
|
const { autoFetch = true, retryOnError = false, maxRetries = 3 } = options;
|
|
2026
2026
|
const { isAuthenticated, isInitialized, user: jwtUser, token } = useCrudifyDataContext();
|
|
2027
|
-
const [
|
|
2028
|
-
const [
|
|
2029
|
-
const [
|
|
2030
|
-
const [extendedData, setExtendedData] = useState8({
|
|
2031
|
-
fullProfile: null,
|
|
2032
|
-
totalFields: 0,
|
|
2033
|
-
displayData: {}
|
|
2034
|
-
});
|
|
2027
|
+
const [userData, setUserData] = useState8(null);
|
|
2028
|
+
const [loading, setLoading] = useState8(false);
|
|
2029
|
+
const [error, setError] = useState8(null);
|
|
2035
2030
|
const abortControllerRef = useRef3(null);
|
|
2036
2031
|
const mountedRef = useRef3(true);
|
|
2037
2032
|
const requestIdRef = useRef3(0);
|
|
2038
2033
|
const retryCountRef = useRef3(0);
|
|
2039
|
-
const
|
|
2040
|
-
if (!jwtUser)
|
|
2041
|
-
|
|
2042
|
-
userEmail: null,
|
|
2043
|
-
userId: null,
|
|
2044
|
-
userIdentifier: null
|
|
2045
|
-
};
|
|
2046
|
-
}
|
|
2047
|
-
return {
|
|
2048
|
-
userEmail: jwtUser.email || jwtUser["cognito:username"] || null,
|
|
2049
|
-
userId: jwtUser.sub || null,
|
|
2050
|
-
userIdentifier: jwtUser["cognito:username"] || jwtUser.email || jwtUser.sub || null
|
|
2051
|
-
};
|
|
2034
|
+
const getUserEmailFromJWT = useCallback2(() => {
|
|
2035
|
+
if (!jwtUser) return null;
|
|
2036
|
+
return jwtUser.email || jwtUser["cognito:username"] || null;
|
|
2052
2037
|
}, [jwtUser]);
|
|
2053
2038
|
const clearProfile = useCallback2(() => {
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
setExtendedData({
|
|
2058
|
-
fullProfile: null,
|
|
2059
|
-
totalFields: 0,
|
|
2060
|
-
displayData: {}
|
|
2061
|
-
});
|
|
2039
|
+
setUserData(null);
|
|
2040
|
+
setError(null);
|
|
2041
|
+
setLoading(false);
|
|
2062
2042
|
retryCountRef.current = 0;
|
|
2063
2043
|
}, []);
|
|
2064
2044
|
const refreshProfile = useCallback2(async () => {
|
|
2065
|
-
const
|
|
2066
|
-
console.log("\u{1F464} useCrudifyUser - Refreshing profile for:",
|
|
2067
|
-
if (!
|
|
2045
|
+
const userEmail = getUserEmailFromJWT();
|
|
2046
|
+
console.log("\u{1F464} useCrudifyUser - Refreshing profile for:", userEmail);
|
|
2047
|
+
if (!userEmail) {
|
|
2068
2048
|
if (mountedRef.current) {
|
|
2069
|
-
|
|
2070
|
-
|
|
2049
|
+
setError("No user email available from JWT token");
|
|
2050
|
+
setLoading(false);
|
|
2071
2051
|
}
|
|
2072
2052
|
return;
|
|
2073
2053
|
}
|
|
2074
2054
|
if (!isInitialized) {
|
|
2075
2055
|
if (mountedRef.current) {
|
|
2076
|
-
|
|
2077
|
-
|
|
2056
|
+
setError("Crudify not initialized");
|
|
2057
|
+
setLoading(false);
|
|
2078
2058
|
}
|
|
2079
2059
|
return;
|
|
2080
2060
|
}
|
|
@@ -2086,77 +2066,91 @@ var useCrudifyUser = (options = {}) => {
|
|
|
2086
2066
|
const currentRequestId = ++requestIdRef.current;
|
|
2087
2067
|
try {
|
|
2088
2068
|
if (mountedRef.current) {
|
|
2089
|
-
|
|
2090
|
-
|
|
2069
|
+
setLoading(true);
|
|
2070
|
+
setError(null);
|
|
2091
2071
|
}
|
|
2092
2072
|
console.log("\u{1F464} useCrudifyUser - Fetching profile data from database");
|
|
2093
2073
|
const response = await crudify3.readItems("users", {
|
|
2094
|
-
filter: { email:
|
|
2074
|
+
filter: { email: userEmail },
|
|
2095
2075
|
pagination: { limit: 1 }
|
|
2096
2076
|
});
|
|
2097
2077
|
console.log("\u{1F464} useCrudifyUser - Database response:", response);
|
|
2078
|
+
console.log("\u{1F464} useCrudifyUser - response.data:", response.data);
|
|
2079
|
+
console.log("\u{1F464} useCrudifyUser - response.data type:", typeof response.data);
|
|
2098
2080
|
if (currentRequestId === requestIdRef.current && mountedRef.current && !abortController.signal.aborted) {
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
"role",
|
|
2128
|
-
"permissions",
|
|
2129
|
-
"isActive",
|
|
2130
|
-
"lastLogin",
|
|
2131
|
-
"createdAt",
|
|
2132
|
-
"updatedAt"
|
|
2133
|
-
].includes(key)
|
|
2134
|
-
).reduce((acc, key) => ({ ...acc, [key]: userData[key] }), {})
|
|
2081
|
+
let userData2 = null;
|
|
2082
|
+
if (response.success) {
|
|
2083
|
+
console.log("\u{1F464} useCrudifyUser - Processing successful response:", {
|
|
2084
|
+
dataType: typeof response.data,
|
|
2085
|
+
isArray: Array.isArray(response.data),
|
|
2086
|
+
hasResponse: !!response.data?.response,
|
|
2087
|
+
hasResponseData: !!response.data?.response?.data,
|
|
2088
|
+
responseDataType: typeof response.data?.response?.data
|
|
2089
|
+
});
|
|
2090
|
+
if (Array.isArray(response.data) && response.data.length > 0) {
|
|
2091
|
+
console.log("\u{1F464} useCrudifyUser - Found direct array format");
|
|
2092
|
+
userData2 = response.data[0];
|
|
2093
|
+
} else if (response.data?.response?.data) {
|
|
2094
|
+
console.log("\u{1F464} useCrudifyUser - Found nested response.data format");
|
|
2095
|
+
try {
|
|
2096
|
+
const rawData = response.data.response.data;
|
|
2097
|
+
console.log("\u{1F464} useCrudifyUser - Raw nested data:", rawData);
|
|
2098
|
+
console.log("\u{1F464} useCrudifyUser - Raw data type:", typeof rawData);
|
|
2099
|
+
const parsedData = typeof rawData === "string" ? JSON.parse(rawData) : rawData;
|
|
2100
|
+
console.log("\u{1F464} useCrudifyUser - Parsed nested data:", parsedData);
|
|
2101
|
+
if (parsedData && parsedData.items && Array.isArray(parsedData.items) && parsedData.items.length > 0) {
|
|
2102
|
+
userData2 = parsedData.items[0];
|
|
2103
|
+
console.log("\u{1F464} useCrudifyUser - Extracted user from nested items:", userData2);
|
|
2104
|
+
} else {
|
|
2105
|
+
console.log("\u{1F464} useCrudifyUser - No items found in parsed data or items array is empty");
|
|
2106
|
+
}
|
|
2107
|
+
} catch (parseError) {
|
|
2108
|
+
console.error("\u{1F464} useCrudifyUser - Error parsing nested response data:", parseError);
|
|
2135
2109
|
}
|
|
2136
|
-
}
|
|
2137
|
-
|
|
2138
|
-
|
|
2110
|
+
} else if (response.data && typeof response.data === "object") {
|
|
2111
|
+
console.log("\u{1F464} useCrudifyUser - Found object format, checking for items");
|
|
2112
|
+
if (response.data.items && Array.isArray(response.data.items) && response.data.items.length > 0) {
|
|
2113
|
+
console.log("\u{1F464} useCrudifyUser - Found items in object format");
|
|
2114
|
+
userData2 = response.data.items[0];
|
|
2115
|
+
} else {
|
|
2116
|
+
console.log("\u{1F464} useCrudifyUser - No items found in object format");
|
|
2117
|
+
}
|
|
2118
|
+
} else if (response.data?.data?.response?.data) {
|
|
2119
|
+
console.log("\u{1F464} useCrudifyUser - Found double-nested data.data.response.data format");
|
|
2120
|
+
try {
|
|
2121
|
+
const rawData = response.data.data.response.data;
|
|
2122
|
+
console.log("\u{1F464} useCrudifyUser - Raw double-nested data:", rawData);
|
|
2123
|
+
const parsedData = typeof rawData === "string" ? JSON.parse(rawData) : rawData;
|
|
2124
|
+
console.log("\u{1F464} useCrudifyUser - Parsed double-nested data:", parsedData);
|
|
2125
|
+
if (parsedData && parsedData.items && Array.isArray(parsedData.items) && parsedData.items.length > 0) {
|
|
2126
|
+
userData2 = parsedData.items[0];
|
|
2127
|
+
console.log("\u{1F464} useCrudifyUser - Extracted user from double-nested items:", userData2);
|
|
2128
|
+
}
|
|
2129
|
+
} catch (parseError) {
|
|
2130
|
+
console.error("\u{1F464} useCrudifyUser - Error parsing double-nested response data:", parseError);
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
2134
|
+
if (userData2) {
|
|
2135
|
+
console.log("\u{1F464} useCrudifyUser - User data found:", userData2);
|
|
2136
|
+
setUserData(userData2);
|
|
2137
|
+
setError(null);
|
|
2139
2138
|
retryCountRef.current = 0;
|
|
2140
|
-
console.log("\u{1F464} useCrudifyUser - Profile loaded successfully:",
|
|
2139
|
+
console.log("\u{1F464} useCrudifyUser - Profile loaded successfully:", userData2);
|
|
2141
2140
|
} else {
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
fullProfile: null,
|
|
2146
|
-
totalFields: 0,
|
|
2147
|
-
displayData: {}
|
|
2148
|
-
});
|
|
2149
|
-
console.warn("\u{1F464} useCrudifyUser - User not found for email:", userEmail2);
|
|
2141
|
+
setError("User profile not found in database");
|
|
2142
|
+
setUserData(null);
|
|
2143
|
+
console.warn("\u{1F464} useCrudifyUser - User not found for email:", userEmail);
|
|
2150
2144
|
}
|
|
2151
2145
|
}
|
|
2152
2146
|
} catch (err) {
|
|
2153
2147
|
if (currentRequestId === requestIdRef.current && mountedRef.current) {
|
|
2154
|
-
const
|
|
2155
|
-
console.error("\u{1F464} useCrudifyUser - Error fetching profile:",
|
|
2156
|
-
if (
|
|
2148
|
+
const error2 = err;
|
|
2149
|
+
console.error("\u{1F464} useCrudifyUser - Error fetching profile:", error2);
|
|
2150
|
+
if (error2.name === "AbortError") {
|
|
2157
2151
|
return;
|
|
2158
2152
|
}
|
|
2159
|
-
const shouldRetry = retryOnError && retryCountRef.current < maxRetries && (
|
|
2153
|
+
const shouldRetry = retryOnError && retryCountRef.current < maxRetries && (error2.message?.includes("Network Error") || error2.message?.includes("Failed to fetch"));
|
|
2160
2154
|
if (shouldRetry) {
|
|
2161
2155
|
retryCountRef.current++;
|
|
2162
2156
|
console.log(`\u{1F464} useCrudifyUser - Retrying profile fetch (${retryCountRef.current}/${maxRetries})`);
|
|
@@ -2166,24 +2160,19 @@ var useCrudifyUser = (options = {}) => {
|
|
|
2166
2160
|
}
|
|
2167
2161
|
}, 1e3 * retryCountRef.current);
|
|
2168
2162
|
} else {
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
setExtendedData({
|
|
2172
|
-
fullProfile: null,
|
|
2173
|
-
totalFields: 0,
|
|
2174
|
-
displayData: {}
|
|
2175
|
-
});
|
|
2163
|
+
setError("Failed to load user profile from database");
|
|
2164
|
+
setUserData(null);
|
|
2176
2165
|
}
|
|
2177
2166
|
}
|
|
2178
2167
|
} finally {
|
|
2179
2168
|
if (currentRequestId === requestIdRef.current && mountedRef.current) {
|
|
2180
|
-
|
|
2169
|
+
setLoading(false);
|
|
2181
2170
|
}
|
|
2182
2171
|
if (abortControllerRef.current === abortController) {
|
|
2183
2172
|
abortControllerRef.current = null;
|
|
2184
2173
|
}
|
|
2185
2174
|
}
|
|
2186
|
-
}, [isInitialized,
|
|
2175
|
+
}, [isInitialized, getUserEmailFromJWT, retryOnError, maxRetries]);
|
|
2187
2176
|
useEffect8(() => {
|
|
2188
2177
|
if (autoFetch && isAuthenticated && isInitialized) {
|
|
2189
2178
|
refreshProfile();
|
|
@@ -2201,19 +2190,14 @@ var useCrudifyUser = (options = {}) => {
|
|
|
2201
2190
|
}
|
|
2202
2191
|
};
|
|
2203
2192
|
}, []);
|
|
2204
|
-
const
|
|
2193
|
+
const user = {
|
|
2194
|
+
session: jwtUser,
|
|
2195
|
+
data: userData
|
|
2196
|
+
};
|
|
2205
2197
|
return {
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
userIdentifier,
|
|
2210
|
-
// Perfil completo desde BD
|
|
2211
|
-
userProfile,
|
|
2212
|
-
profileLoading,
|
|
2213
|
-
profileError,
|
|
2214
|
-
// Datos extendidos formateados
|
|
2215
|
-
extendedData,
|
|
2216
|
-
// Acciones
|
|
2198
|
+
user,
|
|
2199
|
+
loading,
|
|
2200
|
+
error,
|
|
2217
2201
|
refreshProfile,
|
|
2218
2202
|
clearProfile
|
|
2219
2203
|
};
|