@nocios/crudify-ui 1.2.23 → 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 +49 -122
- package/dist/index.mjs +49 -122
- 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,19 +3314,19 @@ 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);
|
|
3346
3326
|
console.log("\u{1F464} useCrudifyUser - response.data:", response.data);
|
|
3347
3327
|
console.log("\u{1F464} useCrudifyUser - response.data type:", typeof response.data);
|
|
3348
3328
|
if (currentRequestId === requestIdRef.current && mountedRef.current && !abortController.signal.aborted) {
|
|
3349
|
-
let
|
|
3329
|
+
let userData2 = null;
|
|
3350
3330
|
if (response.success) {
|
|
3351
3331
|
console.log("\u{1F464} useCrudifyUser - Processing successful response:", {
|
|
3352
3332
|
dataType: typeof response.data,
|
|
@@ -3357,7 +3337,7 @@ var useCrudifyUser = (options = {}) => {
|
|
|
3357
3337
|
});
|
|
3358
3338
|
if (Array.isArray(response.data) && response.data.length > 0) {
|
|
3359
3339
|
console.log("\u{1F464} useCrudifyUser - Found direct array format");
|
|
3360
|
-
|
|
3340
|
+
userData2 = response.data[0];
|
|
3361
3341
|
} else if (response.data?.response?.data) {
|
|
3362
3342
|
console.log("\u{1F464} useCrudifyUser - Found nested response.data format");
|
|
3363
3343
|
try {
|
|
@@ -3367,8 +3347,8 @@ var useCrudifyUser = (options = {}) => {
|
|
|
3367
3347
|
const parsedData = typeof rawData === "string" ? JSON.parse(rawData) : rawData;
|
|
3368
3348
|
console.log("\u{1F464} useCrudifyUser - Parsed nested data:", parsedData);
|
|
3369
3349
|
if (parsedData && parsedData.items && Array.isArray(parsedData.items) && parsedData.items.length > 0) {
|
|
3370
|
-
|
|
3371
|
-
console.log("\u{1F464} useCrudifyUser - Extracted user from nested items:",
|
|
3350
|
+
userData2 = parsedData.items[0];
|
|
3351
|
+
console.log("\u{1F464} useCrudifyUser - Extracted user from nested items:", userData2);
|
|
3372
3352
|
} else {
|
|
3373
3353
|
console.log("\u{1F464} useCrudifyUser - No items found in parsed data or items array is empty");
|
|
3374
3354
|
}
|
|
@@ -3379,7 +3359,7 @@ var useCrudifyUser = (options = {}) => {
|
|
|
3379
3359
|
console.log("\u{1F464} useCrudifyUser - Found object format, checking for items");
|
|
3380
3360
|
if (response.data.items && Array.isArray(response.data.items) && response.data.items.length > 0) {
|
|
3381
3361
|
console.log("\u{1F464} useCrudifyUser - Found items in object format");
|
|
3382
|
-
|
|
3362
|
+
userData2 = response.data.items[0];
|
|
3383
3363
|
} else {
|
|
3384
3364
|
console.log("\u{1F464} useCrudifyUser - No items found in object format");
|
|
3385
3365
|
}
|
|
@@ -3391,77 +3371,34 @@ var useCrudifyUser = (options = {}) => {
|
|
|
3391
3371
|
const parsedData = typeof rawData === "string" ? JSON.parse(rawData) : rawData;
|
|
3392
3372
|
console.log("\u{1F464} useCrudifyUser - Parsed double-nested data:", parsedData);
|
|
3393
3373
|
if (parsedData && parsedData.items && Array.isArray(parsedData.items) && parsedData.items.length > 0) {
|
|
3394
|
-
|
|
3395
|
-
console.log("\u{1F464} useCrudifyUser - Extracted user from double-nested items:",
|
|
3374
|
+
userData2 = parsedData.items[0];
|
|
3375
|
+
console.log("\u{1F464} useCrudifyUser - Extracted user from double-nested items:", userData2);
|
|
3396
3376
|
}
|
|
3397
3377
|
} catch (parseError) {
|
|
3398
3378
|
console.error("\u{1F464} useCrudifyUser - Error parsing double-nested response data:", parseError);
|
|
3399
3379
|
}
|
|
3400
3380
|
}
|
|
3401
3381
|
}
|
|
3402
|
-
if (
|
|
3403
|
-
console.log("\u{1F464} useCrudifyUser - User data found:",
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
fullProfile: userData,
|
|
3407
|
-
totalFields: Object.keys(userData).length,
|
|
3408
|
-
displayData: {
|
|
3409
|
-
id: userData.id || userData._id,
|
|
3410
|
-
email: userData.email,
|
|
3411
|
-
username: userData.username,
|
|
3412
|
-
firstName: userData.firstName || userData.name,
|
|
3413
|
-
lastName: userData.lastName,
|
|
3414
|
-
fullName: userData.fullName || `${userData.firstName || userData.name || ""} ${userData.lastName || ""}`.trim() || null,
|
|
3415
|
-
role: userData.role,
|
|
3416
|
-
permissions: userData.permissions || [],
|
|
3417
|
-
isActive: userData.isActive,
|
|
3418
|
-
lastLogin: userData.lastLogin,
|
|
3419
|
-
createdAt: userData.createdAt,
|
|
3420
|
-
updatedAt: userData.updatedAt,
|
|
3421
|
-
// Include any custom fields
|
|
3422
|
-
...Object.keys(userData).filter(
|
|
3423
|
-
(key) => ![
|
|
3424
|
-
"id",
|
|
3425
|
-
"_id",
|
|
3426
|
-
"email",
|
|
3427
|
-
"username",
|
|
3428
|
-
"firstName",
|
|
3429
|
-
"name",
|
|
3430
|
-
"lastName",
|
|
3431
|
-
"fullName",
|
|
3432
|
-
"role",
|
|
3433
|
-
"permissions",
|
|
3434
|
-
"isActive",
|
|
3435
|
-
"lastLogin",
|
|
3436
|
-
"createdAt",
|
|
3437
|
-
"updatedAt"
|
|
3438
|
-
].includes(key)
|
|
3439
|
-
).reduce((acc, key) => ({ ...acc, [key]: userData[key] }), {})
|
|
3440
|
-
}
|
|
3441
|
-
};
|
|
3442
|
-
setExtendedData(extended);
|
|
3443
|
-
setProfileError(null);
|
|
3382
|
+
if (userData2) {
|
|
3383
|
+
console.log("\u{1F464} useCrudifyUser - User data found:", userData2);
|
|
3384
|
+
setUserData(userData2);
|
|
3385
|
+
setError(null);
|
|
3444
3386
|
retryCountRef.current = 0;
|
|
3445
|
-
console.log("\u{1F464} useCrudifyUser - Profile loaded successfully:",
|
|
3387
|
+
console.log("\u{1F464} useCrudifyUser - Profile loaded successfully:", userData2);
|
|
3446
3388
|
} else {
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
fullProfile: null,
|
|
3451
|
-
totalFields: 0,
|
|
3452
|
-
displayData: {}
|
|
3453
|
-
});
|
|
3454
|
-
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);
|
|
3455
3392
|
}
|
|
3456
3393
|
}
|
|
3457
3394
|
} catch (err) {
|
|
3458
3395
|
if (currentRequestId === requestIdRef.current && mountedRef.current) {
|
|
3459
|
-
const
|
|
3460
|
-
console.error("\u{1F464} useCrudifyUser - Error fetching profile:",
|
|
3461
|
-
if (
|
|
3396
|
+
const error2 = err;
|
|
3397
|
+
console.error("\u{1F464} useCrudifyUser - Error fetching profile:", error2);
|
|
3398
|
+
if (error2.name === "AbortError") {
|
|
3462
3399
|
return;
|
|
3463
3400
|
}
|
|
3464
|
-
const shouldRetry = retryOnError && retryCountRef.current < maxRetries && (
|
|
3401
|
+
const shouldRetry = retryOnError && retryCountRef.current < maxRetries && (error2.message?.includes("Network Error") || error2.message?.includes("Failed to fetch"));
|
|
3465
3402
|
if (shouldRetry) {
|
|
3466
3403
|
retryCountRef.current++;
|
|
3467
3404
|
console.log(`\u{1F464} useCrudifyUser - Retrying profile fetch (${retryCountRef.current}/${maxRetries})`);
|
|
@@ -3471,24 +3408,19 @@ var useCrudifyUser = (options = {}) => {
|
|
|
3471
3408
|
}
|
|
3472
3409
|
}, 1e3 * retryCountRef.current);
|
|
3473
3410
|
} else {
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
setExtendedData({
|
|
3477
|
-
fullProfile: null,
|
|
3478
|
-
totalFields: 0,
|
|
3479
|
-
displayData: {}
|
|
3480
|
-
});
|
|
3411
|
+
setError("Failed to load user profile from database");
|
|
3412
|
+
setUserData(null);
|
|
3481
3413
|
}
|
|
3482
3414
|
}
|
|
3483
3415
|
} finally {
|
|
3484
3416
|
if (currentRequestId === requestIdRef.current && mountedRef.current) {
|
|
3485
|
-
|
|
3417
|
+
setLoading(false);
|
|
3486
3418
|
}
|
|
3487
3419
|
if (abortControllerRef.current === abortController) {
|
|
3488
3420
|
abortControllerRef.current = null;
|
|
3489
3421
|
}
|
|
3490
3422
|
}
|
|
3491
|
-
}, [isInitialized,
|
|
3423
|
+
}, [isInitialized, getUserEmailFromJWT, retryOnError, maxRetries]);
|
|
3492
3424
|
(0, import_react13.useEffect)(() => {
|
|
3493
3425
|
if (autoFetch && isAuthenticated && isInitialized) {
|
|
3494
3426
|
refreshProfile();
|
|
@@ -3506,19 +3438,14 @@ var useCrudifyUser = (options = {}) => {
|
|
|
3506
3438
|
}
|
|
3507
3439
|
};
|
|
3508
3440
|
}, []);
|
|
3509
|
-
const
|
|
3441
|
+
const user = {
|
|
3442
|
+
session: jwtUser,
|
|
3443
|
+
data: userData
|
|
3444
|
+
};
|
|
3510
3445
|
return {
|
|
3511
|
-
|
|
3512
|
-
|
|
3513
|
-
|
|
3514
|
-
userIdentifier,
|
|
3515
|
-
// Perfil completo desde BD
|
|
3516
|
-
userProfile,
|
|
3517
|
-
profileLoading,
|
|
3518
|
-
profileError,
|
|
3519
|
-
// Datos extendidos formateados
|
|
3520
|
-
extendedData,
|
|
3521
|
-
// Acciones
|
|
3446
|
+
user,
|
|
3447
|
+
loading,
|
|
3448
|
+
error,
|
|
3522
3449
|
refreshProfile,
|
|
3523
3450
|
clearProfile
|
|
3524
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,19 +2066,19 @@ 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);
|
|
2098
2078
|
console.log("\u{1F464} useCrudifyUser - response.data:", response.data);
|
|
2099
2079
|
console.log("\u{1F464} useCrudifyUser - response.data type:", typeof response.data);
|
|
2100
2080
|
if (currentRequestId === requestIdRef.current && mountedRef.current && !abortController.signal.aborted) {
|
|
2101
|
-
let
|
|
2081
|
+
let userData2 = null;
|
|
2102
2082
|
if (response.success) {
|
|
2103
2083
|
console.log("\u{1F464} useCrudifyUser - Processing successful response:", {
|
|
2104
2084
|
dataType: typeof response.data,
|
|
@@ -2109,7 +2089,7 @@ var useCrudifyUser = (options = {}) => {
|
|
|
2109
2089
|
});
|
|
2110
2090
|
if (Array.isArray(response.data) && response.data.length > 0) {
|
|
2111
2091
|
console.log("\u{1F464} useCrudifyUser - Found direct array format");
|
|
2112
|
-
|
|
2092
|
+
userData2 = response.data[0];
|
|
2113
2093
|
} else if (response.data?.response?.data) {
|
|
2114
2094
|
console.log("\u{1F464} useCrudifyUser - Found nested response.data format");
|
|
2115
2095
|
try {
|
|
@@ -2119,8 +2099,8 @@ var useCrudifyUser = (options = {}) => {
|
|
|
2119
2099
|
const parsedData = typeof rawData === "string" ? JSON.parse(rawData) : rawData;
|
|
2120
2100
|
console.log("\u{1F464} useCrudifyUser - Parsed nested data:", parsedData);
|
|
2121
2101
|
if (parsedData && parsedData.items && Array.isArray(parsedData.items) && parsedData.items.length > 0) {
|
|
2122
|
-
|
|
2123
|
-
console.log("\u{1F464} useCrudifyUser - Extracted user from nested items:",
|
|
2102
|
+
userData2 = parsedData.items[0];
|
|
2103
|
+
console.log("\u{1F464} useCrudifyUser - Extracted user from nested items:", userData2);
|
|
2124
2104
|
} else {
|
|
2125
2105
|
console.log("\u{1F464} useCrudifyUser - No items found in parsed data or items array is empty");
|
|
2126
2106
|
}
|
|
@@ -2131,7 +2111,7 @@ var useCrudifyUser = (options = {}) => {
|
|
|
2131
2111
|
console.log("\u{1F464} useCrudifyUser - Found object format, checking for items");
|
|
2132
2112
|
if (response.data.items && Array.isArray(response.data.items) && response.data.items.length > 0) {
|
|
2133
2113
|
console.log("\u{1F464} useCrudifyUser - Found items in object format");
|
|
2134
|
-
|
|
2114
|
+
userData2 = response.data.items[0];
|
|
2135
2115
|
} else {
|
|
2136
2116
|
console.log("\u{1F464} useCrudifyUser - No items found in object format");
|
|
2137
2117
|
}
|
|
@@ -2143,77 +2123,34 @@ var useCrudifyUser = (options = {}) => {
|
|
|
2143
2123
|
const parsedData = typeof rawData === "string" ? JSON.parse(rawData) : rawData;
|
|
2144
2124
|
console.log("\u{1F464} useCrudifyUser - Parsed double-nested data:", parsedData);
|
|
2145
2125
|
if (parsedData && parsedData.items && Array.isArray(parsedData.items) && parsedData.items.length > 0) {
|
|
2146
|
-
|
|
2147
|
-
console.log("\u{1F464} useCrudifyUser - Extracted user from double-nested items:",
|
|
2126
|
+
userData2 = parsedData.items[0];
|
|
2127
|
+
console.log("\u{1F464} useCrudifyUser - Extracted user from double-nested items:", userData2);
|
|
2148
2128
|
}
|
|
2149
2129
|
} catch (parseError) {
|
|
2150
2130
|
console.error("\u{1F464} useCrudifyUser - Error parsing double-nested response data:", parseError);
|
|
2151
2131
|
}
|
|
2152
2132
|
}
|
|
2153
2133
|
}
|
|
2154
|
-
if (
|
|
2155
|
-
console.log("\u{1F464} useCrudifyUser - User data found:",
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
fullProfile: userData,
|
|
2159
|
-
totalFields: Object.keys(userData).length,
|
|
2160
|
-
displayData: {
|
|
2161
|
-
id: userData.id || userData._id,
|
|
2162
|
-
email: userData.email,
|
|
2163
|
-
username: userData.username,
|
|
2164
|
-
firstName: userData.firstName || userData.name,
|
|
2165
|
-
lastName: userData.lastName,
|
|
2166
|
-
fullName: userData.fullName || `${userData.firstName || userData.name || ""} ${userData.lastName || ""}`.trim() || null,
|
|
2167
|
-
role: userData.role,
|
|
2168
|
-
permissions: userData.permissions || [],
|
|
2169
|
-
isActive: userData.isActive,
|
|
2170
|
-
lastLogin: userData.lastLogin,
|
|
2171
|
-
createdAt: userData.createdAt,
|
|
2172
|
-
updatedAt: userData.updatedAt,
|
|
2173
|
-
// Include any custom fields
|
|
2174
|
-
...Object.keys(userData).filter(
|
|
2175
|
-
(key) => ![
|
|
2176
|
-
"id",
|
|
2177
|
-
"_id",
|
|
2178
|
-
"email",
|
|
2179
|
-
"username",
|
|
2180
|
-
"firstName",
|
|
2181
|
-
"name",
|
|
2182
|
-
"lastName",
|
|
2183
|
-
"fullName",
|
|
2184
|
-
"role",
|
|
2185
|
-
"permissions",
|
|
2186
|
-
"isActive",
|
|
2187
|
-
"lastLogin",
|
|
2188
|
-
"createdAt",
|
|
2189
|
-
"updatedAt"
|
|
2190
|
-
].includes(key)
|
|
2191
|
-
).reduce((acc, key) => ({ ...acc, [key]: userData[key] }), {})
|
|
2192
|
-
}
|
|
2193
|
-
};
|
|
2194
|
-
setExtendedData(extended);
|
|
2195
|
-
setProfileError(null);
|
|
2134
|
+
if (userData2) {
|
|
2135
|
+
console.log("\u{1F464} useCrudifyUser - User data found:", userData2);
|
|
2136
|
+
setUserData(userData2);
|
|
2137
|
+
setError(null);
|
|
2196
2138
|
retryCountRef.current = 0;
|
|
2197
|
-
console.log("\u{1F464} useCrudifyUser - Profile loaded successfully:",
|
|
2139
|
+
console.log("\u{1F464} useCrudifyUser - Profile loaded successfully:", userData2);
|
|
2198
2140
|
} else {
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
fullProfile: null,
|
|
2203
|
-
totalFields: 0,
|
|
2204
|
-
displayData: {}
|
|
2205
|
-
});
|
|
2206
|
-
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);
|
|
2207
2144
|
}
|
|
2208
2145
|
}
|
|
2209
2146
|
} catch (err) {
|
|
2210
2147
|
if (currentRequestId === requestIdRef.current && mountedRef.current) {
|
|
2211
|
-
const
|
|
2212
|
-
console.error("\u{1F464} useCrudifyUser - Error fetching profile:",
|
|
2213
|
-
if (
|
|
2148
|
+
const error2 = err;
|
|
2149
|
+
console.error("\u{1F464} useCrudifyUser - Error fetching profile:", error2);
|
|
2150
|
+
if (error2.name === "AbortError") {
|
|
2214
2151
|
return;
|
|
2215
2152
|
}
|
|
2216
|
-
const shouldRetry = retryOnError && retryCountRef.current < maxRetries && (
|
|
2153
|
+
const shouldRetry = retryOnError && retryCountRef.current < maxRetries && (error2.message?.includes("Network Error") || error2.message?.includes("Failed to fetch"));
|
|
2217
2154
|
if (shouldRetry) {
|
|
2218
2155
|
retryCountRef.current++;
|
|
2219
2156
|
console.log(`\u{1F464} useCrudifyUser - Retrying profile fetch (${retryCountRef.current}/${maxRetries})`);
|
|
@@ -2223,24 +2160,19 @@ var useCrudifyUser = (options = {}) => {
|
|
|
2223
2160
|
}
|
|
2224
2161
|
}, 1e3 * retryCountRef.current);
|
|
2225
2162
|
} else {
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
setExtendedData({
|
|
2229
|
-
fullProfile: null,
|
|
2230
|
-
totalFields: 0,
|
|
2231
|
-
displayData: {}
|
|
2232
|
-
});
|
|
2163
|
+
setError("Failed to load user profile from database");
|
|
2164
|
+
setUserData(null);
|
|
2233
2165
|
}
|
|
2234
2166
|
}
|
|
2235
2167
|
} finally {
|
|
2236
2168
|
if (currentRequestId === requestIdRef.current && mountedRef.current) {
|
|
2237
|
-
|
|
2169
|
+
setLoading(false);
|
|
2238
2170
|
}
|
|
2239
2171
|
if (abortControllerRef.current === abortController) {
|
|
2240
2172
|
abortControllerRef.current = null;
|
|
2241
2173
|
}
|
|
2242
2174
|
}
|
|
2243
|
-
}, [isInitialized,
|
|
2175
|
+
}, [isInitialized, getUserEmailFromJWT, retryOnError, maxRetries]);
|
|
2244
2176
|
useEffect8(() => {
|
|
2245
2177
|
if (autoFetch && isAuthenticated && isInitialized) {
|
|
2246
2178
|
refreshProfile();
|
|
@@ -2258,19 +2190,14 @@ var useCrudifyUser = (options = {}) => {
|
|
|
2258
2190
|
}
|
|
2259
2191
|
};
|
|
2260
2192
|
}, []);
|
|
2261
|
-
const
|
|
2193
|
+
const user = {
|
|
2194
|
+
session: jwtUser,
|
|
2195
|
+
data: userData
|
|
2196
|
+
};
|
|
2262
2197
|
return {
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
userIdentifier,
|
|
2267
|
-
// Perfil completo desde BD
|
|
2268
|
-
userProfile,
|
|
2269
|
-
profileLoading,
|
|
2270
|
-
profileError,
|
|
2271
|
-
// Datos extendidos formateados
|
|
2272
|
-
extendedData,
|
|
2273
|
-
// Acciones
|
|
2198
|
+
user,
|
|
2199
|
+
loading,
|
|
2200
|
+
error,
|
|
2274
2201
|
refreshProfile,
|
|
2275
2202
|
clearProfile
|
|
2276
2203
|
};
|