@nocios/crudify-ui 4.0.96 → 4.1.0
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/{LoginComponent-saSn0o5x.d.mts → LoginComponent-CSTVsfeV.d.mts} +1 -1
- package/dist/{LoginComponent-saSn0o5x.d.ts → LoginComponent-CSTVsfeV.d.ts} +1 -1
- package/dist/chunk-5JKS55SE.mjs +1 -0
- package/dist/chunk-AT74WV5W.js +1 -0
- package/dist/chunk-CTDQEJAU.mjs +1 -0
- package/dist/{chunk-WG3ZXCPF.js → chunk-GFZGBKFG.js} +1 -1
- package/dist/{chunk-JYDCSNNQ.mjs → chunk-GLR5HDJC.mjs} +1 -1
- package/dist/{chunk-Y72XFXMI.mjs → chunk-KNEHDX2M.mjs} +1 -1
- package/dist/chunk-TLGRXZCS.js +1 -0
- package/dist/{chunk-LP5NJV2P.js → chunk-X3HSMDZ7.js} +1 -1
- package/dist/components.d.mts +1 -1
- package/dist/components.d.ts +1 -1
- package/dist/components.js +1 -1
- package/dist/components.mjs +1 -1
- package/dist/{errorTranslation-DqdgLEUy.d.mts → errorTranslation-DEn4aqs6.d.mts} +1 -1
- package/dist/{errorTranslation-CBbQYNWR.d.ts → errorTranslation-DdqZg8JD.d.ts} +1 -1
- package/dist/hooks.d.mts +1 -1
- package/dist/hooks.d.ts +1 -1
- package/dist/hooks.js +1 -1
- package/dist/hooks.mjs +1 -1
- package/dist/{index-CUbUeMMS.d.mts → index-BwF68SOh.d.mts} +13 -2
- package/dist/{index-DZdMugLk.d.ts → index-Fkm9ErmY.d.ts} +13 -2
- package/dist/index.d.mts +128 -13
- package/dist/index.d.ts +128 -13
- package/dist/index.js +6 -1
- package/dist/index.mjs +6 -1
- package/dist/utils.d.mts +77 -3
- package/dist/utils.d.ts +77 -3
- package/dist/utils.js +1 -1
- package/dist/utils.mjs +1 -1
- package/package.json +24 -4
- package/vitest.config.ts +27 -0
- package/dist/chunk-ATAGEVFK.js +0 -1
- package/dist/chunk-HMJY3MMZ.mjs +0 -1
- package/dist/chunk-MMYGRMGB.mjs +0 -1
- package/dist/chunk-RHG74IMW.js +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocios/crudify-ui",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "Biblioteca de componentes UI para Crudify",
|
|
5
5
|
"author": "Nocios",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,10 +33,13 @@
|
|
|
33
33
|
"build": "tsup",
|
|
34
34
|
"build:analyze": "tsup --metafile",
|
|
35
35
|
"start": "vite example",
|
|
36
|
+
"test": "vitest",
|
|
37
|
+
"test:ui": "vitest --ui",
|
|
38
|
+
"test:coverage": "vitest --coverage",
|
|
36
39
|
"prepublishOnly": "npm run build"
|
|
37
40
|
},
|
|
38
41
|
"dependencies": {
|
|
39
|
-
"@nocios/crudify-browser": "^4.0.
|
|
42
|
+
"@nocios/crudify-browser": "^4.0.8",
|
|
40
43
|
"crypto-js": "^4.2.0",
|
|
41
44
|
"dompurify": "^3.2.7",
|
|
42
45
|
"uuid": "^13.0.0"
|
|
@@ -51,14 +54,31 @@
|
|
|
51
54
|
"i18next-http-backend": "^3.0.2",
|
|
52
55
|
"react": "^19.1.0",
|
|
53
56
|
"react-dom": "^19.1.0",
|
|
54
|
-
"react-i18next": "^15.5.2"
|
|
57
|
+
"react-i18next": "^15.5.2",
|
|
58
|
+
"react-router-dom": "^6.0.0 || ^7.0.0"
|
|
55
59
|
},
|
|
56
60
|
"devDependencies": {
|
|
61
|
+
"@emotion/react": "^11.14.0",
|
|
62
|
+
"@emotion/styled": "^11.14.1",
|
|
63
|
+
"@mui/icons-material": "^7.3.4",
|
|
64
|
+
"@mui/material": "^7.3.4",
|
|
65
|
+
"@mui/x-data-grid": "^8.13.1",
|
|
66
|
+
"@testing-library/jest-dom": "^6.1.5",
|
|
67
|
+
"@testing-library/react": "^16.3.0",
|
|
68
|
+
"@testing-library/user-event": "^14.5.1",
|
|
57
69
|
"@types/crypto-js": "^4.2.2",
|
|
58
70
|
"@types/react": "^19.0.3",
|
|
59
71
|
"@types/react-dom": "^19.0.1",
|
|
72
|
+
"@vitest/coverage-v8": "^1.1.0",
|
|
73
|
+
"@vitest/ui": "^1.1.0",
|
|
74
|
+
"i18next-browser-languagedetector": "^8.2.0",
|
|
75
|
+
"i18next-http-backend": "^3.0.2",
|
|
76
|
+
"jsdom": "^23.0.1",
|
|
77
|
+
"react-i18next": "^16.0.0",
|
|
78
|
+
"react-router-dom": "^7.9.3",
|
|
60
79
|
"tsup": "^8.4.0",
|
|
61
|
-
"typescript": "^5.1.3"
|
|
80
|
+
"typescript": "^5.1.3",
|
|
81
|
+
"vitest": "^1.1.0"
|
|
62
82
|
},
|
|
63
83
|
"publishConfig": {
|
|
64
84
|
"access": "public"
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { defineConfig } from "vitest/config";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
test: {
|
|
6
|
+
globals: true,
|
|
7
|
+
environment: "jsdom",
|
|
8
|
+
setupFiles: ["./src/__tests__/setup.ts"],
|
|
9
|
+
coverage: {
|
|
10
|
+
provider: "v8",
|
|
11
|
+
reporter: ["text", "json", "html"],
|
|
12
|
+
exclude: [
|
|
13
|
+
"node_modules/",
|
|
14
|
+
"src/__tests__/",
|
|
15
|
+
"dist/",
|
|
16
|
+
"**/*.d.ts",
|
|
17
|
+
"**/*.config.*",
|
|
18
|
+
"**/mockData.ts",
|
|
19
|
+
],
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
resolve: {
|
|
23
|
+
alias: {
|
|
24
|
+
"@": path.resolve(__dirname, "./src"),
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
});
|
package/dist/chunk-ATAGEVFK.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var p=e=>{let t=document.cookie.match(new RegExp("(^|;)\\s*"+e+"=([^;]+)"));return t?t[2]:null};var h=["errors.{category}.{code}","errors.{code}","login.{code}","error.{code}","messages.{code}","{code}"],R={INVALID_CREDENTIALS:"auth",UNAUTHORIZED:"auth",INVALID_API_KEY:"auth",USER_NOT_FOUND:"auth",USER_NOT_ACTIVE:"auth",NO_PERMISSION:"auth",SESSION_EXPIRED:"auth",ITEM_NOT_FOUND:"data",NOT_FOUND:"data",IN_USE:"data",DUPLICATE_ENTRY:"data",FIELD_ERROR:"validation",BAD_REQUEST:"validation",INVALID_EMAIL:"validation",INVALID_CODE:"validation",REQUIRED_FIELD:"validation",INTERNAL_SERVER_ERROR:"system",DATABASE_CONNECTION_ERROR:"system",INVALID_CONFIGURATION:"system",UNKNOWN_OPERATION:"system",TIMEOUT_ERROR:"system",NETWORK_ERROR:"system",TOO_MANY_REQUESTS:"rate_limit"},f={INVALID_CREDENTIALS:"Invalid username or password",UNAUTHORIZED:"You are not authorized to perform this action",SESSION_EXPIRED:"Your session has expired. Please log in again.",USER_NOT_FOUND:"User not found",ITEM_NOT_FOUND:"Item not found",FIELD_ERROR:"Invalid field value",INTERNAL_SERVER_ERROR:"An internal error occurred",NETWORK_ERROR:"Network connection error",TIMEOUT_ERROR:"Request timeout",UNKNOWN_OPERATION:"Unknown operation",INVALID_EMAIL:"Invalid email format",INVALID_CODE:"Invalid code",TOO_MANY_REQUESTS:"Too many requests, please try again later"};function l(e,t){let{translateFn:r,currentLanguage:n,enableDebug:o}=t;o&&console.log(`\u{1F50D} [ErrorTranslation] Translating error code: ${e} (lang: ${n||"unknown"})`);let s=e.toUpperCase(),u=R[s],g=h.map(a=>a.replace("{category}",u||"general").replace("{code}",s));o&&console.log("\u{1F511} [ErrorTranslation] Searching keys:",g);for(let a of g){let i=r(a);if(o&&console.log(`\u{1F50D} [ErrorTranslation] Checking key: "${a}" -> result: "${i}" (same as key: ${i===a})`),i&&i!==a)return o&&console.log(`\u2705 [ErrorTranslation] Found translation at key: ${a} = "${i}"`),i}let c=f[s];if(c)return o&&console.log(`\u{1F504} [ErrorTranslation] Using default message: "${c}"`),c;let d=s.replace(/_/g," ").toLowerCase().replace(/\b\w/g,a=>a.toUpperCase());return o&&console.log(`\u26A0\uFE0F [ErrorTranslation] No translation found, using friendly code: "${d}"`),d}function I(e,t){return e.map(r=>l(r,t))}function m(e,t){let{enableDebug:r}=t;r&&console.log("\u{1F50D} [ErrorTranslation] Translating error:",e);let n=l(e.code,t);return n!==e.code.toUpperCase()&&n!==e.code?(r&&console.log(`\u2705 [ErrorTranslation] Using hierarchical translation: "${n}"`),e.field?`${e.field}: ${n}`:n):e.message&&!e.message.includes("Error:")&&e.message.length>0&&e.message!==e.code?(r&&console.log(`\u{1F504} [ErrorTranslation] No hierarchical translation found, using API message: "${e.message}"`),e.message):(r&&console.log(`\u26A0\uFE0F [ErrorTranslation] Using final fallback: "${n}"`),e.field?`${e.field}: ${n}`:n)}function N(e,t={}){let r={translateFn:e,currentLanguage:t.currentLanguage,enableDebug:t.enableDebug||!1};return{translateErrorCode:n=>l(n,r),translateErrorCodes:n=>I(n,r),translateError:n=>m(n,r),translateApiError:n=>_optionalChain([n, 'optionalAccess', _2 => _2.data, 'optionalAccess', _3 => _3.response, 'optionalAccess', _4 => _4.status])?l(n.data.response.status,r):_optionalChain([n, 'optionalAccess', _5 => _5.status])?l(n.status,r):_optionalChain([n, 'optionalAccess', _6 => _6.code])?l(n.code,r):"Unknown error"}}var E=class e{constructor(){this.listeners=new Set;this.isHandlingAuthError=!1;this.lastErrorTime=0;this.lastEventType=null;this.DEBOUNCE_TIME=1e3}static getInstance(){return e.instance||(e.instance=new e),e.instance}emit(t,r){let n=Date.now();if(this.isHandlingAuthError&&this.lastEventType===t&&n-this.lastErrorTime<this.DEBOUNCE_TIME){console.log(`\u{1F6AB} AuthEventBus: Ignoring duplicate ${t} event (debounced)`);return}this.isHandlingAuthError=!0,this.lastErrorTime=n,this.lastEventType=t,console.log(`\u{1F4E2} AuthEventBus: Emitting ${t} event`,r);let o={type:t,details:r,timestamp:n};this.listeners.forEach(s=>{try{s(o)}catch(u){console.error("AuthEventBus: Error in listener",u)}}),setTimeout(()=>{this.isHandlingAuthError=!1,this.lastEventType=null},2e3)}subscribe(t){return this.listeners.add(t),()=>{this.listeners.delete(t)}}clear(){this.listeners.clear(),this.isHandlingAuthError=!1,this.lastEventType=null}isHandling(){return this.isHandlingAuthError}},_= exports.f =E.getInstance();var T=e=>{try{let t=e.split(".");if(t.length!==3)return console.warn("Invalid JWT format: token must have 3 parts"),null;let r=t[1],n=r+"=".repeat((4-r.length%4)%4);return JSON.parse(atob(n))}catch(t){return console.warn("Failed to decode JWT token:",t),null}},v= exports.h =()=>{try{let e=null;if(e=sessionStorage.getItem("authToken"),console.log("\u{1F50D} getCurrentUserEmail - authToken:",e?`${e.substring(0,20)}...`:null),e||(e=sessionStorage.getItem("token"),console.log("\u{1F50D} getCurrentUserEmail - token:",e?`${e.substring(0,20)}...`:null)),e||(e=localStorage.getItem("authToken")||localStorage.getItem("token"),console.log("\u{1F50D} getCurrentUserEmail - localStorage:",e?`${e.substring(0,20)}...`:null)),!e)return console.warn("\u{1F50D} getCurrentUserEmail - No token found in any storage"),null;let t=T(e);if(!t)return console.warn("\u{1F50D} getCurrentUserEmail - Failed to decode token"),null;let r=t.email||t["cognito:username"]||null;return console.log("\u{1F50D} getCurrentUserEmail - Extracted email:",r),r}catch(e){return console.warn("Failed to get current user email:",e),null}},D= exports.i =e=>{try{let t=T(e);if(!t||!t.exp)return!0;let r=Math.floor(Date.now()/1e3);return t.exp<r}catch (e2){return!0}};exports.a = p; exports.b = l; exports.c = I; exports.d = m; exports.e = N; exports.f = _; exports.g = T; exports.h = v; exports.i = D;
|
package/dist/chunk-HMJY3MMZ.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var p=e=>{let t=document.cookie.match(new RegExp("(^|;)\\s*"+e+"=([^;]+)"));return t?t[2]:null};var h=["errors.{category}.{code}","errors.{code}","login.{code}","error.{code}","messages.{code}","{code}"],R={INVALID_CREDENTIALS:"auth",UNAUTHORIZED:"auth",INVALID_API_KEY:"auth",USER_NOT_FOUND:"auth",USER_NOT_ACTIVE:"auth",NO_PERMISSION:"auth",SESSION_EXPIRED:"auth",ITEM_NOT_FOUND:"data",NOT_FOUND:"data",IN_USE:"data",DUPLICATE_ENTRY:"data",FIELD_ERROR:"validation",BAD_REQUEST:"validation",INVALID_EMAIL:"validation",INVALID_CODE:"validation",REQUIRED_FIELD:"validation",INTERNAL_SERVER_ERROR:"system",DATABASE_CONNECTION_ERROR:"system",INVALID_CONFIGURATION:"system",UNKNOWN_OPERATION:"system",TIMEOUT_ERROR:"system",NETWORK_ERROR:"system",TOO_MANY_REQUESTS:"rate_limit"},f={INVALID_CREDENTIALS:"Invalid username or password",UNAUTHORIZED:"You are not authorized to perform this action",SESSION_EXPIRED:"Your session has expired. Please log in again.",USER_NOT_FOUND:"User not found",ITEM_NOT_FOUND:"Item not found",FIELD_ERROR:"Invalid field value",INTERNAL_SERVER_ERROR:"An internal error occurred",NETWORK_ERROR:"Network connection error",TIMEOUT_ERROR:"Request timeout",UNKNOWN_OPERATION:"Unknown operation",INVALID_EMAIL:"Invalid email format",INVALID_CODE:"Invalid code",TOO_MANY_REQUESTS:"Too many requests, please try again later"};function l(e,t){let{translateFn:r,currentLanguage:n,enableDebug:o}=t;o&&console.log(`\u{1F50D} [ErrorTranslation] Translating error code: ${e} (lang: ${n||"unknown"})`);let s=e.toUpperCase(),u=R[s],g=h.map(a=>a.replace("{category}",u||"general").replace("{code}",s));o&&console.log("\u{1F511} [ErrorTranslation] Searching keys:",g);for(let a of g){let i=r(a);if(o&&console.log(`\u{1F50D} [ErrorTranslation] Checking key: "${a}" -> result: "${i}" (same as key: ${i===a})`),i&&i!==a)return o&&console.log(`\u2705 [ErrorTranslation] Found translation at key: ${a} = "${i}"`),i}let c=f[s];if(c)return o&&console.log(`\u{1F504} [ErrorTranslation] Using default message: "${c}"`),c;let d=s.replace(/_/g," ").toLowerCase().replace(/\b\w/g,a=>a.toUpperCase());return o&&console.log(`\u26A0\uFE0F [ErrorTranslation] No translation found, using friendly code: "${d}"`),d}function I(e,t){return e.map(r=>l(r,t))}function m(e,t){let{enableDebug:r}=t;r&&console.log("\u{1F50D} [ErrorTranslation] Translating error:",e);let n=l(e.code,t);return n!==e.code.toUpperCase()&&n!==e.code?(r&&console.log(`\u2705 [ErrorTranslation] Using hierarchical translation: "${n}"`),e.field?`${e.field}: ${n}`:n):e.message&&!e.message.includes("Error:")&&e.message.length>0&&e.message!==e.code?(r&&console.log(`\u{1F504} [ErrorTranslation] No hierarchical translation found, using API message: "${e.message}"`),e.message):(r&&console.log(`\u26A0\uFE0F [ErrorTranslation] Using final fallback: "${n}"`),e.field?`${e.field}: ${n}`:n)}function N(e,t={}){let r={translateFn:e,currentLanguage:t.currentLanguage,enableDebug:t.enableDebug||!1};return{translateErrorCode:n=>l(n,r),translateErrorCodes:n=>I(n,r),translateError:n=>m(n,r),translateApiError:n=>n?.data?.response?.status?l(n.data.response.status,r):n?.status?l(n.status,r):n?.code?l(n.code,r):"Unknown error"}}var E=class e{constructor(){this.listeners=new Set;this.isHandlingAuthError=!1;this.lastErrorTime=0;this.lastEventType=null;this.DEBOUNCE_TIME=1e3}static getInstance(){return e.instance||(e.instance=new e),e.instance}emit(t,r){let n=Date.now();if(this.isHandlingAuthError&&this.lastEventType===t&&n-this.lastErrorTime<this.DEBOUNCE_TIME){console.log(`\u{1F6AB} AuthEventBus: Ignoring duplicate ${t} event (debounced)`);return}this.isHandlingAuthError=!0,this.lastErrorTime=n,this.lastEventType=t,console.log(`\u{1F4E2} AuthEventBus: Emitting ${t} event`,r);let o={type:t,details:r,timestamp:n};this.listeners.forEach(s=>{try{s(o)}catch(u){console.error("AuthEventBus: Error in listener",u)}}),setTimeout(()=>{this.isHandlingAuthError=!1,this.lastEventType=null},2e3)}subscribe(t){return this.listeners.add(t),()=>{this.listeners.delete(t)}}clear(){this.listeners.clear(),this.isHandlingAuthError=!1,this.lastEventType=null}isHandling(){return this.isHandlingAuthError}},_=E.getInstance();var T=e=>{try{let t=e.split(".");if(t.length!==3)return console.warn("Invalid JWT format: token must have 3 parts"),null;let r=t[1],n=r+"=".repeat((4-r.length%4)%4);return JSON.parse(atob(n))}catch(t){return console.warn("Failed to decode JWT token:",t),null}},v=()=>{try{let e=null;if(e=sessionStorage.getItem("authToken"),console.log("\u{1F50D} getCurrentUserEmail - authToken:",e?`${e.substring(0,20)}...`:null),e||(e=sessionStorage.getItem("token"),console.log("\u{1F50D} getCurrentUserEmail - token:",e?`${e.substring(0,20)}...`:null)),e||(e=localStorage.getItem("authToken")||localStorage.getItem("token"),console.log("\u{1F50D} getCurrentUserEmail - localStorage:",e?`${e.substring(0,20)}...`:null)),!e)return console.warn("\u{1F50D} getCurrentUserEmail - No token found in any storage"),null;let t=T(e);if(!t)return console.warn("\u{1F50D} getCurrentUserEmail - Failed to decode token"),null;let r=t.email||t["cognito:username"]||null;return console.log("\u{1F50D} getCurrentUserEmail - Extracted email:",r),r}catch(e){return console.warn("Failed to get current user email:",e),null}},D=e=>{try{let t=T(e);if(!t||!t.exp)return!0;let r=Math.floor(Date.now()/1e3);return t.exp<r}catch{return!0}};export{p as a,l as b,I as c,m as d,N as e,_ as f,T as g,v as h,D as i};
|
package/dist/chunk-MMYGRMGB.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{a as L,b as _,f as I,g as V,h as Y}from"./chunk-HMJY3MMZ.mjs";import C from"crypto-js";var a=class a{static setStorageType(t){a.storageType=t}static generateEncryptionKey(){let t=[navigator.userAgent,navigator.language,navigator.platform,screen.width,screen.height,Date.now().toString(),Math.random().toString(36)].join("|");return C.SHA256(t).toString()}static getEncryptionKey(){if(a.encryptionKey)return a.encryptionKey;let t=window.localStorage;if(!t)return a.encryptionKey=a.generateEncryptionKey(),a.encryptionKey;try{let e=t.getItem(a.ENCRYPTION_KEY_STORAGE);return(!e||e.length<32)&&(e=a.generateEncryptionKey(),t.setItem(a.ENCRYPTION_KEY_STORAGE,e)),a.encryptionKey=e,e}catch{return console.warn("Crudify: Cannot persist encryption key, using temporary key"),a.encryptionKey=a.generateEncryptionKey(),a.encryptionKey}}static isStorageAvailable(t){try{let e=window[t],r="__storage_test__";return e.setItem(r,"test"),e.removeItem(r),!0}catch{return!1}}static getStorage(){return a.storageType==="none"?null:a.isStorageAvailable(a.storageType)?window[a.storageType]:(console.warn(`Crudify: ${a.storageType} not available, tokens won't persist`),null)}static encrypt(t){try{let e=a.getEncryptionKey();return C.AES.encrypt(t,e).toString()}catch(e){return console.error("Crudify: Encryption failed",e),t}}static decrypt(t){try{let e=a.getEncryptionKey();return C.AES.decrypt(t,e).toString(C.enc.Utf8)||t}catch(e){return console.error("Crudify: Decryption failed",e),t}}static saveTokens(t){let e=a.getStorage();if(e)try{let r={accessToken:t.accessToken,refreshToken:t.refreshToken,expiresAt:t.expiresAt,refreshExpiresAt:t.refreshExpiresAt,savedAt:Date.now()},n=a.encrypt(JSON.stringify(r));e.setItem(a.TOKEN_KEY,n),console.debug("Crudify: Tokens saved successfully")}catch(r){console.error("Crudify: Failed to save tokens",r)}}static getTokens(){let t=a.getStorage();if(!t)return null;try{let e=t.getItem(a.TOKEN_KEY);if(!e)return null;let r=a.decrypt(e),n=JSON.parse(r);return!n.accessToken||!n.refreshToken||!n.expiresAt||!n.refreshExpiresAt?(console.warn("Crudify: Incomplete token data found, clearing storage"),a.clearTokens(),null):Date.now()>=n.refreshExpiresAt?(console.info("Crudify: Refresh token expired, clearing storage"),a.clearTokens(),null):{accessToken:n.accessToken,refreshToken:n.refreshToken,expiresAt:n.expiresAt,refreshExpiresAt:n.refreshExpiresAt}}catch(e){return console.error("Crudify: Failed to retrieve tokens",e),a.clearTokens(),null}}static clearTokens(){let t=a.getStorage();if(t)try{t.removeItem(a.TOKEN_KEY),console.debug("Crudify: Tokens cleared from storage")}catch(e){console.error("Crudify: Failed to clear tokens",e)}}static rotateEncryptionKey(){try{a.clearTokens(),a.encryptionKey=null;let t=window.localStorage;t&&t.removeItem(a.ENCRYPTION_KEY_STORAGE),console.info("Crudify: Encryption key rotated successfully")}catch(t){console.error("Crudify: Failed to rotate encryption key",t)}}static hasValidTokens(){return a.getTokens()!==null}static getExpirationInfo(){let t=a.getTokens();if(!t)return null;let e=Date.now();return{accessExpired:e>=t.expiresAt,refreshExpired:e>=t.refreshExpiresAt,accessExpiresIn:Math.max(0,t.expiresAt-e),refreshExpiresIn:Math.max(0,t.refreshExpiresAt-e)}}static updateAccessToken(t,e){let r=a.getTokens();if(!r){console.warn("Crudify: Cannot update access token, no existing tokens found");return}a.saveTokens({...r,accessToken:t,expiresAt:e})}static subscribeToChanges(t){let e=r=>{if(r.key===a.TOKEN_KEY){if(r.newValue===null){console.debug("Crudify: Tokens removed in another tab"),t(null);return}if(r.newValue){console.debug("Crudify: Tokens updated in another tab");let n=a.getTokens();t(n)}}};return window.addEventListener("storage",e),()=>{window.removeEventListener("storage",e)}}};a.TOKEN_KEY="crudify_tokens",a.ENCRYPTION_KEY_STORAGE="crudify_enc_key",a.encryptionKey=null,a.storageType="localStorage";var g=a;import E from"@nocios/crudify-browser";var O=class o{constructor(){this.config={};this.initialized=!1;this.lastActivityTime=0}static getInstance(){return o.instance||(o.instance=new o),o.instance}async initialize(t={}){if(this.initialized){console.warn("SessionManager: Already initialized");return}this.config={storageType:"localStorage",autoRestore:!0,enableLogging:!1,...t},g.setStorageType(this.config.storageType||"localStorage"),this.config.enableLogging,E.setTokenInvalidationCallback(()=>{this.log("\u{1F514} Tokens invalidated by crudify-core"),I.emit("SESSION_EXPIRED",{message:"Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente.",source:"crudify-core.clearTokensAndRefreshState"})}),this.config.autoRestore&&await this.restoreSession(),this.initialized=!0,this.log("SessionManager initialized successfully")}async login(t,e){try{this.log("Attempting login...");let r=await E.login(t,e);if(!r.success)return this.log("Login failed:",r.errors),{success:!1,error:this.formatError(r.errors),rawResponse:r};let n={accessToken:r.data.token,refreshToken:r.data.refreshToken,expiresAt:r.data.expiresAt,refreshExpiresAt:r.data.refreshExpiresAt};return g.saveTokens(n),this.lastActivityTime=Date.now(),this.log("Login successful, tokens saved"),this.config.onLoginSuccess?.(n),{success:!0,tokens:n,data:r.data}}catch(r){return this.log("Login error:",r),{success:!1,error:r instanceof Error?r.message:"Unknown error"}}}async logout(){try{this.log("Logging out..."),await E.logout(),g.clearTokens(),this.log("Logout successful"),this.config.onLogout?.()}catch(t){this.log("Logout error:",t),g.clearTokens()}}async restoreSession(){try{this.log("Attempting to restore session...");let t=g.getTokens();if(!t)return this.log("No valid tokens found in storage"),!1;if(Date.now()>=t.refreshExpiresAt)return this.log("Refresh token expired, clearing storage"),g.clearTokens(),!1;if(E.setTokens({accessToken:t.accessToken,refreshToken:t.refreshToken,expiresAt:t.expiresAt,refreshExpiresAt:t.refreshExpiresAt}),E.getTokenData().isValid===!1){if(this.log("Restored access token is invalid or expired"),Date.now()<t.refreshExpiresAt&&(this.log("Access token expired but refresh is valid, attempting refresh..."),await this.refreshTokens())){this.log("Session restored successfully via token refresh");let n=g.getTokens();return n&&this.config.onSessionRestored?.(n),!0}return g.clearTokens(),await E.logout(),!1}return this.log("Session restored successfully"),this.lastActivityTime=Date.now(),this.config.onSessionRestored?.(t),!0}catch(t){return this.log("Session restore error:",t),g.clearTokens(),await E.logout(),!1}}isAuthenticated(){return E.isLogin()||g.hasValidTokens()}getTokenInfo(){let t=E.getTokenData(),e=g.getExpirationInfo();return{isLoggedIn:this.isAuthenticated(),crudifyTokens:t,storageInfo:e,hasValidTokens:g.hasValidTokens()}}async refreshTokens(){try{this.log("Manually refreshing tokens...");let t=await E.refreshAccessToken();if(!t.success)return this.log("Token refresh failed:",t.errors),g.clearTokens(),this.config.showNotification?.(this.getSessionExpiredMessage(),"warning"),this.config.onSessionExpired?.(),!1;let e={accessToken:t.data.token,refreshToken:t.data.refreshToken,expiresAt:t.data.expiresAt,refreshExpiresAt:t.data.refreshExpiresAt};return g.saveTokens(e),this.log("Tokens refreshed and saved successfully"),this.lastActivityTime=Date.now(),!0}catch(t){return this.log("Token refresh error:",t),g.clearTokens(),this.config.showNotification?.(this.getSessionExpiredMessage(),"warning"),this.config.onSessionExpired?.(),!1}}setupResponseInterceptor(){E.setResponseInterceptor(async t=>{this.updateLastActivity();let e=this.detectAuthorizationError(t);if(e.isAuthError){if(this.log("\u{1F6A8} Authorization error detected:",{errorType:e.errorType,shouldLogout:e.shouldTriggerLogout,message:e.userFriendlyMessage}),e.isRefreshTokenInvalid||e.isTokenRefreshFailed)return this.log("Refresh token invalid, emitting TOKEN_REFRESH_FAILED event"),I.emit("TOKEN_REFRESH_FAILED",{message:e.userFriendlyMessage,error:e.errorDetails,source:"SessionManager.setupResponseInterceptor"}),t;e.shouldTriggerLogout&&(g.hasValidTokens()&&!e.isIrrecoverable?(this.log("Attempting token refresh after auth error..."),await this.refreshTokens()||(this.log("Token refresh failed, emitting SESSION_EXPIRED event"),I.emit("SESSION_EXPIRED",{message:e.userFriendlyMessage,error:e.errorDetails,source:"SessionManager.setupResponseInterceptor (refresh failed)"}))):(this.log("No valid tokens or irrecoverable error, emitting SESSION_EXPIRED event"),I.emit("SESSION_EXPIRED",{message:e.userFriendlyMessage,error:e.errorDetails,source:"SessionManager.setupResponseInterceptor (no tokens)"})))}return t}),this.log("Response interceptor configured")}detectAuthorizationError(t){let e={isAuthError:!1,isRefreshTokenInvalid:!1,isTokenRefreshFailed:!1,isTokenExpired:!1,isUnauthorized:!1,isIrrecoverable:!1,shouldTriggerLogout:!1,errorType:"",errorDetails:null,userFriendlyMessage:""};if(t.errors&&Array.isArray(t.errors)){let r=t.errors.find(n=>n.errorType==="Unauthorized"||n.message?.includes("Unauthorized")||n.message?.includes("Not Authorized")||n.message?.includes("NOT_AUTHORIZED")||n.message?.includes("Token")||n.message?.includes("TOKEN")||n.message?.includes("Authentication")||n.message?.includes("UNAUTHENTICATED")||n.extensions?.code==="UNAUTHENTICATED"||n.extensions?.code==="FORBIDDEN");r&&(e.isAuthError=!0,e.errorType="GraphQL Array",e.errorDetails=r,e.shouldTriggerLogout=!0,e.userFriendlyMessage="Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente.",(r.message?.includes("TOKEN")||r.message?.includes("Token"))&&(e.isTokenExpired=!0),r.extensions?.code==="UNAUTHENTICATED"&&(e.isUnauthorized=!0))}if(!e.isAuthError&&t.errors&&typeof t.errors=="object"&&!Array.isArray(t.errors)){let n=Object.values(t.errors).flat().find(u=>typeof u=="string"&&(u.includes("NOT_AUTHORIZED")||u.includes("TOKEN_REFRESH_FAILED")||u.includes("TOKEN_HAS_EXPIRED")||u.includes("PLEASE_LOGIN")||u.includes("Unauthorized")||u.includes("UNAUTHENTICATED")||u.includes("SESSION_EXPIRED")||u.includes("INVALID_TOKEN")));n&&typeof n=="string"&&(e.isAuthError=!0,e.errorType="GraphQL Object",e.errorDetails=t.errors,e.shouldTriggerLogout=!0,n.includes("TOKEN_REFRESH_FAILED")?(e.isTokenRefreshFailed=!0,e.isRefreshTokenInvalid=!0,e.isIrrecoverable=!0,e.userFriendlyMessage="Tu sesi\xF3n ha caducado. Por favor, inicia sesi\xF3n nuevamente."):n.includes("TOKEN_HAS_EXPIRED")||n.includes("SESSION_EXPIRED")?(e.isTokenExpired=!0,e.userFriendlyMessage="Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente."):n.includes("INVALID_TOKEN")?(e.isTokenExpired=!0,e.isIrrecoverable=!0,e.userFriendlyMessage="Token inv\xE1lido. Por favor, inicia sesi\xF3n nuevamente."):e.userFriendlyMessage="Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente.")}if(!e.isAuthError&&t.data?.response?.status){let r=t.data.response.status.toUpperCase();(r==="UNAUTHORIZED"||r==="UNAUTHENTICATED")&&(e.isAuthError=!0,e.errorType="Status",e.errorDetails=t.data.response,e.isUnauthorized=!0,e.shouldTriggerLogout=!0,e.isIrrecoverable=!0,e.userFriendlyMessage="Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente.")}if(!e.isAuthError&&t.data?.response?.data)try{let r=typeof t.data.response.data=="string"?JSON.parse(t.data.response.data):t.data.response.data;(r.error==="REFRESH_TOKEN_INVALID"||r.error==="TOKEN_EXPIRED"||r.error==="INVALID_TOKEN")&&(e.isAuthError=!0,e.errorType="Parsed Data",e.errorDetails=r,e.shouldTriggerLogout=!0,e.isIrrecoverable=!0,r.error==="REFRESH_TOKEN_INVALID"?(e.isRefreshTokenInvalid=!0,e.isTokenRefreshFailed=!0,e.userFriendlyMessage="Tu sesi\xF3n ha caducado. Por favor, inicia sesi\xF3n nuevamente."):(e.isTokenExpired=!0,e.userFriendlyMessage="Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente."))}catch{}if(!e.isAuthError&&t.errorCode){let r=t.errorCode.toUpperCase();(r==="UNAUTHORIZED"||r==="UNAUTHENTICATED"||r==="TOKEN_EXPIRED"||r==="INVALID_TOKEN")&&(e.isAuthError=!0,e.errorType="Error Code",e.errorDetails={errorCode:r},e.shouldTriggerLogout=!0,r==="TOKEN_EXPIRED"?e.isTokenExpired=!0:e.isUnauthorized=!0,e.userFriendlyMessage="Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente.")}return e}updateLastActivity(){this.lastActivityTime=Date.now(),this.log("Last activity updated")}getTimeSinceLastActivity(){return this.lastActivityTime===0?0:Date.now()-this.lastActivityTime}checkInactivity(){let t=this.getTimeSinceLastActivity(),e=E.getTokenData();if(this.lastActivityTime===0)return"none";let r=900*1e3,n=300*1e3,u=300*1e3;return t>r?(this.log(`Inactivity timeout: ${Math.floor(t/6e4)} minutes since last activity`),"logout"):t<n&&e.expiresIn<u&&e.expiresIn>0?(this.log(`User active recently (${Math.floor(t/6e4)}min ago) and token expiring soon, should refresh`),"refresh"):"none"}clearSession(){g.clearTokens(),E.logout(),this.lastActivityTime=0,this.log("Session cleared completely")}getSessionExpiredMessage(){return this.config.translateFn?_("SESSION_EXPIRED",{translateFn:this.config.translateFn,enableDebug:this.config.enableLogging}):"Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente."}log(t,...e){this.config.enableLogging&&console.log(`[SessionManager] ${t}`,...e)}formatError(t){return t?typeof t=="string"?t:typeof t=="object"?Object.values(t).flat().join(", "):"Authentication failed":"Unknown error"}};import{useState as te,useEffect as H,useCallback as R}from"react";function X(o={}){let[t,e]=te({isAuthenticated:!1,isLoading:!0,isInitialized:!1,tokens:null,error:null}),r=O.getInstance(),n=R(async()=>{try{e(c=>({...c,isLoading:!0,error:null}));let l={autoRestore:o.autoRestore??!0,enableLogging:o.enableLogging??!1,showNotification:o.showNotification,translateFn:o.translateFn,onSessionExpired:()=>{e(c=>({...c,isAuthenticated:!1,tokens:null,error:"Session expired"})),o.onSessionExpired?.()},onSessionRestored:c=>{e(f=>({...f,isAuthenticated:!0,tokens:c,error:null})),o.onSessionRestored?.(c)},onLoginSuccess:c=>{e(f=>({...f,isAuthenticated:!0,tokens:c,error:null}))},onLogout:()=>{e(c=>({...c,isAuthenticated:!1,tokens:null,error:null}))}};await r.initialize(l),r.setupResponseInterceptor();let s=r.isAuthenticated(),i=r.getTokenInfo();e(c=>({...c,isAuthenticated:s,isInitialized:!0,isLoading:!1,tokens:i.crudifyTokens.accessToken?{accessToken:i.crudifyTokens.accessToken,refreshToken:i.crudifyTokens.refreshToken,expiresAt:i.crudifyTokens.expiresAt,refreshExpiresAt:i.crudifyTokens.refreshExpiresAt}:null}))}catch(l){let s=l instanceof Error?l.message:"Initialization failed";e(i=>({...i,isLoading:!1,isInitialized:!0,error:s}))}},[o.autoRestore,o.enableLogging,o.onSessionExpired,o.onSessionRestored]),u=R(async(l,s)=>{e(i=>({...i,isLoading:!0,error:null}));try{let i=await r.login(l,s);return i.success&&i.tokens?e(c=>({...c,isAuthenticated:!0,tokens:i.tokens,isLoading:!1,error:null})):e(c=>({...c,isAuthenticated:!1,tokens:null,isLoading:!1,error:null})),i}catch(i){let c=i instanceof Error?i.message:"Login failed",f=c.includes("INVALID_CREDENTIALS")||c.includes("Invalid email")||c.includes("Invalid password")||c.includes("credentials");return e(T=>({...T,isAuthenticated:!1,tokens:null,isLoading:!1,error:f?null:c})),{success:!1,error:c}}},[r]),m=R(async()=>{e(l=>({...l,isLoading:!0}));try{await r.logout(),e(l=>({...l,isAuthenticated:!1,tokens:null,isLoading:!1,error:null}))}catch(l){e(s=>({...s,isAuthenticated:!1,tokens:null,isLoading:!1,error:l instanceof Error?l.message:"Logout error"}))}},[r]),p=R(async()=>{try{let l=await r.refreshTokens();if(l){let s=r.getTokenInfo();e(i=>({...i,tokens:s.crudifyTokens.accessToken?{accessToken:s.crudifyTokens.accessToken,refreshToken:s.crudifyTokens.refreshToken,expiresAt:s.crudifyTokens.expiresAt,refreshExpiresAt:s.crudifyTokens.refreshExpiresAt}:null,error:null}))}else e(s=>({...s,isAuthenticated:!1,tokens:null,error:"Token refresh failed"}));return l}catch(l){return e(s=>({...s,isAuthenticated:!1,tokens:null,error:l instanceof Error?l.message:"Token refresh failed"})),!1}},[r]),S=R(()=>{e(l=>({...l,error:null}))},[]),k=R(()=>r.getTokenInfo(),[r]);H(()=>{n()},[n]),H(()=>{if(!t.isAuthenticated||!t.tokens)return;let l=()=>{r.updateLastActivity(),o.enableLogging&&console.log("\u{1F4CD} User navigating - activity updated")};window.addEventListener("popstate",l);let s=window.history.pushState,i=window.history.replaceState;window.history.pushState=function(...f){let T=s.apply(this,f);return l(),T},window.history.replaceState=function(...f){let T=i.apply(this,f);return l(),T};let c=setInterval(async()=>{let f=r.checkInactivity();if(f==="logout")o.enableLogging&&console.log("\u23F1\uFE0F Inactivity timeout - logging out user"),await m();else if(f==="refresh"&&(o.enableLogging&&console.log("\u{1F504} User active, token expiring soon - refreshing..."),await r.refreshTokens())){let h=r.getTokenInfo();e(b=>({...b,tokens:h.crudifyTokens.accessToken?{accessToken:h.crudifyTokens.accessToken,refreshToken:h.crudifyTokens.refreshToken,expiresAt:h.crudifyTokens.expiresAt,refreshExpiresAt:h.crudifyTokens.refreshExpiresAt}:null}))}},120*1e3);return()=>{clearInterval(c),window.removeEventListener("popstate",l),window.history.pushState=s,window.history.replaceState=i}},[t.isAuthenticated,t.tokens,r,o.enableLogging,m]),H(()=>{let l=g.subscribeToChanges(s=>{s?(o.enableLogging&&console.log("\u{1F504} Tokens updated in another tab"),e(i=>({...i,tokens:s,isAuthenticated:!0}))):(o.enableLogging&&console.log("\u{1F504} Logout detected in another tab"),e(i=>({...i,isAuthenticated:!1,tokens:null})),I.emit("SESSION_EXPIRED",{message:"Sesi\xF3n cerrada en otra pesta\xF1a",source:"CrossTabSync"}))});return()=>l()},[o.enableLogging]);let x=R(()=>{r.updateLastActivity()},[r]);return{...t,login:u,logout:m,refreshTokens:p,clearError:S,getTokenInfo:k,updateActivity:x,isExpiringSoon:t.tokens?t.tokens.expiresAt-Date.now()<300*1e3:!1,expiresIn:t.tokens?Math.max(0,t.tokens.expiresAt-Date.now()):0,refreshExpiresIn:t.tokens?Math.max(0,t.tokens.refreshExpiresAt-Date.now()):0}}import{useState as j,createContext as re,useContext as oe,useCallback as U,useEffect as se}from"react";import{Snackbar as ne,Alert as ie,Box as ae,Portal as ce}from"@mui/material";import{v4 as le}from"uuid";import ue from"dompurify";import{jsx as D,jsxs as ge}from"react/jsx-runtime";var B=re(null),de=o=>ue.sanitize(o,{ALLOWED_TAGS:["b","i","em","strong","br","span"],ALLOWED_ATTR:["class"],FORBID_TAGS:["script","iframe","object","embed"],FORBID_ATTR:["onload","onerror","onclick","onmouseover","onfocus","onblur"],WHOLE_DOCUMENT:!1,RETURN_DOM:!1,RETURN_DOM_FRAGMENT:!1,RETURN_TRUSTED_TYPE:!1}),W=({children:o,maxNotifications:t=5,defaultAutoHideDuration:e=6e3,position:r={vertical:"top",horizontal:"right"},enabled:n=!1,allowHtml:u=!1})=>{let[m,p]=j([]),S=U((s,i="info",c)=>{if(!n)return"";if(!s||typeof s!="string")return console.warn("\u26A0\uFE0F GlobalNotificationProvider: Invalid message provided"),"";s.length>1e3&&(console.warn("\u26A0\uFE0F GlobalNotificationProvider: Message too long, truncating"),s=s.substring(0,1e3)+"...");let f=le(),T={id:f,message:s,severity:i,autoHideDuration:c?.autoHideDuration??e,persistent:c?.persistent??!1,allowHtml:c?.allowHtml??u};return p(h=>[...h.length>=t?h.slice(-(t-1)):h,T]),f},[t,e,n,u]),k=U(s=>{p(i=>i.filter(c=>c.id!==s))},[]),x=U(()=>{p([])},[]),l={showNotification:S,hideNotification:k,clearAllNotifications:x};return ge(B.Provider,{value:l,children:[o,n&&D(ce,{children:D(ae,{sx:{position:"fixed",zIndex:9999,[r.vertical]:(r.vertical==="top",24),[r.horizontal]:r.horizontal==="right"||r.horizontal==="left"?24:"50%",...r.horizontal==="center"&&{transform:"translateX(-50%)"},display:"flex",flexDirection:r.vertical==="top"?"column":"column-reverse",gap:1,maxWidth:"400px",width:"auto"},children:m.map(s=>D(fe,{notification:s,onClose:()=>k(s.id)},s.id))})})]})},fe=({notification:o,onClose:t})=>{let[e,r]=j(!0),n=U((u,m)=>{m!=="clickaway"&&(r(!1),setTimeout(t,300))},[t]);return se(()=>{if(!o.persistent&&o.autoHideDuration){let u=setTimeout(()=>{n()},o.autoHideDuration);return()=>clearTimeout(u)}},[o.autoHideDuration,o.persistent,n]),D(ne,{open:e,onClose:n,sx:{position:"relative","& .MuiSnackbarContent-root":{minWidth:"auto"}},TransitionProps:{enter:!0,exit:!0},children:D(ie,{variant:"filled",severity:o.severity,onClose:n,sx:{width:"100%",minWidth:"280px",maxWidth:"400px",wordBreak:"break-word"},children:o.allowHtml?D("span",{dangerouslySetInnerHTML:{__html:de(o.message)}}):D("span",{children:o.message})})})},J=()=>{let o=oe(B);if(!o)throw new Error("useGlobalNotification debe ser usado dentro de un GlobalNotificationProvider");return o};import pe,{createContext as he,useContext as ye,useMemo as $}from"react";import{Fragment as z,jsx as y,jsxs as v}from"react/jsx-runtime";var Z=he(void 0);function Te({children:o,options:t={},config:e,showNotifications:r=!1,notificationOptions:n={}}){let u;try{let{showNotification:s}=J();u=s}catch{}let m=pe.useMemo(()=>({...t,showNotification:u,onSessionExpired:()=>{t.onSessionExpired?.()}}),[t,u]),p=X(m),S=$(()=>{let s,i,c,f,T,h="unknown";if(e?.publicApiKey&&(s=e.publicApiKey,h="props"),e?.env&&(i=e.env),e?.appName&&(c=e.appName),e?.loginActions&&(f=e.loginActions),e?.logo&&(T=e.logo),!s){let b=L("publicApiKey"),N=L("environment"),w=L("appName"),A=L("loginActions"),d=L("logo");b&&(s=b,h="cookies"),N&&["dev","stg","prod"].includes(N)&&(i=N),w&&(c=decodeURIComponent(w)),A&&(f=decodeURIComponent(A).split(",").map(P=>P.trim()).filter(Boolean)),d&&(T=decodeURIComponent(d))}return{publicApiKey:s,env:i,appName:c,loginActions:f,logo:T}},[e]),k=$(()=>{if(!p.tokens?.accessToken||!p.isAuthenticated)return null;try{let s=V(p.tokens.accessToken);if(s&&s.sub&&s.email&&s.subscriber){let i={_id:s.sub,email:s.email,subscriberKey:s.subscriber};return Object.keys(s).forEach(c=>{["sub","email","subscriber"].includes(c)||(i[c]=s[c])}),i}}catch(s){console.error("Error decoding JWT token for sessionData:",s)}return null},[p.tokens?.accessToken,p.isAuthenticated]),x={...p,sessionData:k,config:S},l={enabled:r,maxNotifications:n.maxNotifications||5,defaultAutoHideDuration:n.defaultAutoHideDuration||6e3,position:n.position||{vertical:"top",horizontal:"right"}};return y(Z.Provider,{value:x,children:o})}function Ze(o){let t={enabled:o.showNotifications,maxNotifications:o.notificationOptions?.maxNotifications||5,defaultAutoHideDuration:o.notificationOptions?.defaultAutoHideDuration||6e3,position:o.notificationOptions?.position||{vertical:"top",horizontal:"right"},allowHtml:o.notificationOptions?.allowHtml||!1};return y(W,{...t,children:y(Te,{...o})})}function q(){let o=ye(Z);if(o===void 0)throw new Error("useSessionContext must be used within a SessionProvider");return o}function qe({children:o,fallback:t=y("div",{children:"Please log in to access this content"}),redirectTo:e}){let{isAuthenticated:r,isLoading:n,isInitialized:u}=q();return!u||n?y("div",{children:"Loading..."}):r?y(z,{children:o}):e?(e(),null):y(z,{children:t})}function Qe(){let o=q();return o.isInitialized?v("div",{style:{padding:"10px",margin:"10px",border:"1px solid #ccc",borderRadius:"4px",fontSize:"12px",fontFamily:"monospace"},children:[y("h4",{children:"Session Debug Info"}),v("div",{children:[y("strong",{children:"Authenticated:"})," ",o.isAuthenticated?"Yes":"No"]}),v("div",{children:[y("strong",{children:"Loading:"})," ",o.isLoading?"Yes":"No"]}),v("div",{children:[y("strong",{children:"Error:"})," ",o.error||"None"]}),o.tokens&&v(z,{children:[v("div",{children:[y("strong",{children:"Access Token:"})," ",o.tokens.accessToken.substring(0,20),"..."]}),v("div",{children:[y("strong",{children:"Refresh Token:"})," ",o.tokens.refreshToken.substring(0,20),"..."]}),v("div",{children:[y("strong",{children:"Access Expires In:"})," ",Math.round(o.expiresIn/1e3/60)," minutes"]}),v("div",{children:[y("strong",{children:"Refresh Expires In:"})," ",Math.round(o.refreshExpiresIn/1e3/60/60)," hours"]}),v("div",{children:[y("strong",{children:"Expiring Soon:"})," ",o.isExpiringSoon?"Yes":"No"]})]})]}):y("div",{children:"Session not initialized"})}import{useState as K,useEffect as Q,useCallback as ee,useRef as M}from"react";import ke from"@nocios/crudify-browser";var nt=(o={})=>{let{autoFetch:t=!0,retryOnError:e=!1,maxRetries:r=3}=o,[n,u]=K(null),[m,p]=K(!1),[S,k]=K(null),[x,l]=K({}),s=M(null),i=M(!0),c=M(0),f=M(0),T=ee(()=>{u(null),k(null),p(!1),l({})},[]),h=ee(async()=>{let b=Y();if(!b){i.current&&(k("No user email available"),p(!1));return}s.current&&s.current.abort();let N=new AbortController;s.current=N;let w=++c.current;try{i.current&&(p(!0),k(null));let A=await ke.readItems("users",{filter:{email:b},pagination:{limit:1}});if(w===c.current&&i.current&&!N.signal.aborted)if(A.success&&A.data&&A.data.length>0){let d=A.data[0];u(d);let F={fullProfile:d,totalFields:Object.keys(d).length,displayData:{id:d.id,email:d.email,username:d.username,firstName:d.firstName,lastName:d.lastName,fullName:d.fullName||`${d.firstName||""} ${d.lastName||""}`.trim(),role:d.role,permissions:d.permissions||[],isActive:d.isActive,lastLogin:d.lastLogin,createdAt:d.createdAt,updatedAt:d.updatedAt,...Object.keys(d).filter(P=>!["id","email","username","firstName","lastName","fullName","role","permissions","isActive","lastLogin","createdAt","updatedAt"].includes(P)).reduce((P,G)=>({...P,[G]:d[G]}),{})}};l(F),k(null),f.current=0}else k("User profile not found"),u(null),l({})}catch(A){if(w===c.current&&i.current){let d=A;if(d.name==="AbortError")return;e&&f.current<r&&(d.message?.includes("Network Error")||d.message?.includes("Failed to fetch"))?(f.current++,setTimeout(()=>{i.current&&h()},1e3*f.current)):(k("Failed to load user profile"),u(null),l({}))}}finally{w===c.current&&i.current&&p(!1),s.current===N&&(s.current=null)}},[e,r]);return Q(()=>{t&&h()},[t,h]),Q(()=>(i.current=!0,()=>{i.current=!1,s.current&&(s.current.abort(),s.current=null)}),[]),{userProfile:n,loading:m,error:S,extendedData:x,refreshProfile:h,clearProfile:T}};export{g as a,O as b,X as c,W as d,J as e,Ze as f,q as g,qe as h,Qe as i,nt as j};
|
package/dist/chunk-RHG74IMW.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkATAGEVFKjs = require('./chunk-ATAGEVFK.js');var _cryptojs = require('crypto-js'); var _cryptojs2 = _interopRequireDefault(_cryptojs);var a=class a{static setStorageType(t){a.storageType=t}static generateEncryptionKey(){let t=[navigator.userAgent,navigator.language,navigator.platform,screen.width,screen.height,Date.now().toString(),Math.random().toString(36)].join("|");return _cryptojs2.default.SHA256(t).toString()}static getEncryptionKey(){if(a.encryptionKey)return a.encryptionKey;let t=window.localStorage;if(!t)return a.encryptionKey=a.generateEncryptionKey(),a.encryptionKey;try{let e=t.getItem(a.ENCRYPTION_KEY_STORAGE);return(!e||e.length<32)&&(e=a.generateEncryptionKey(),t.setItem(a.ENCRYPTION_KEY_STORAGE,e)),a.encryptionKey=e,e}catch (e2){return console.warn("Crudify: Cannot persist encryption key, using temporary key"),a.encryptionKey=a.generateEncryptionKey(),a.encryptionKey}}static isStorageAvailable(t){try{let e=window[t],r="__storage_test__";return e.setItem(r,"test"),e.removeItem(r),!0}catch (e3){return!1}}static getStorage(){return a.storageType==="none"?null:a.isStorageAvailable(a.storageType)?window[a.storageType]:(console.warn(`Crudify: ${a.storageType} not available, tokens won't persist`),null)}static encrypt(t){try{let e=a.getEncryptionKey();return _cryptojs2.default.AES.encrypt(t,e).toString()}catch(e){return console.error("Crudify: Encryption failed",e),t}}static decrypt(t){try{let e=a.getEncryptionKey();return _cryptojs2.default.AES.decrypt(t,e).toString(_cryptojs2.default.enc.Utf8)||t}catch(e){return console.error("Crudify: Decryption failed",e),t}}static saveTokens(t){let e=a.getStorage();if(e)try{let r={accessToken:t.accessToken,refreshToken:t.refreshToken,expiresAt:t.expiresAt,refreshExpiresAt:t.refreshExpiresAt,savedAt:Date.now()},n=a.encrypt(JSON.stringify(r));e.setItem(a.TOKEN_KEY,n),console.debug("Crudify: Tokens saved successfully")}catch(r){console.error("Crudify: Failed to save tokens",r)}}static getTokens(){let t=a.getStorage();if(!t)return null;try{let e=t.getItem(a.TOKEN_KEY);if(!e)return null;let r=a.decrypt(e),n=JSON.parse(r);return!n.accessToken||!n.refreshToken||!n.expiresAt||!n.refreshExpiresAt?(console.warn("Crudify: Incomplete token data found, clearing storage"),a.clearTokens(),null):Date.now()>=n.refreshExpiresAt?(console.info("Crudify: Refresh token expired, clearing storage"),a.clearTokens(),null):{accessToken:n.accessToken,refreshToken:n.refreshToken,expiresAt:n.expiresAt,refreshExpiresAt:n.refreshExpiresAt}}catch(e){return console.error("Crudify: Failed to retrieve tokens",e),a.clearTokens(),null}}static clearTokens(){let t=a.getStorage();if(t)try{t.removeItem(a.TOKEN_KEY),console.debug("Crudify: Tokens cleared from storage")}catch(e){console.error("Crudify: Failed to clear tokens",e)}}static rotateEncryptionKey(){try{a.clearTokens(),a.encryptionKey=null;let t=window.localStorage;t&&t.removeItem(a.ENCRYPTION_KEY_STORAGE),console.info("Crudify: Encryption key rotated successfully")}catch(t){console.error("Crudify: Failed to rotate encryption key",t)}}static hasValidTokens(){return a.getTokens()!==null}static getExpirationInfo(){let t=a.getTokens();if(!t)return null;let e=Date.now();return{accessExpired:e>=t.expiresAt,refreshExpired:e>=t.refreshExpiresAt,accessExpiresIn:Math.max(0,t.expiresAt-e),refreshExpiresIn:Math.max(0,t.refreshExpiresAt-e)}}static updateAccessToken(t,e){let r=a.getTokens();if(!r){console.warn("Crudify: Cannot update access token, no existing tokens found");return}a.saveTokens({...r,accessToken:t,expiresAt:e})}static subscribeToChanges(t){let e=r=>{if(r.key===a.TOKEN_KEY){if(r.newValue===null){console.debug("Crudify: Tokens removed in another tab"),t(null);return}if(r.newValue){console.debug("Crudify: Tokens updated in another tab");let n=a.getTokens();t(n)}}};return window.addEventListener("storage",e),()=>{window.removeEventListener("storage",e)}}};a.TOKEN_KEY="crudify_tokens",a.ENCRYPTION_KEY_STORAGE="crudify_enc_key",a.encryptionKey=null,a.storageType="localStorage";var g=a;var _crudifybrowser = require('@nocios/crudify-browser'); var _crudifybrowser2 = _interopRequireDefault(_crudifybrowser);var O=class o{constructor(){this.config={};this.initialized=!1;this.lastActivityTime=0}static getInstance(){return o.instance||(o.instance=new o),o.instance}async initialize(t={}){if(this.initialized){console.warn("SessionManager: Already initialized");return}this.config={storageType:"localStorage",autoRestore:!0,enableLogging:!1,...t},g.setStorageType(this.config.storageType||"localStorage"),this.config.enableLogging,_crudifybrowser2.default.setTokenInvalidationCallback(()=>{this.log("\u{1F514} Tokens invalidated by crudify-core"),_chunkATAGEVFKjs.f.emit("SESSION_EXPIRED",{message:"Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente.",source:"crudify-core.clearTokensAndRefreshState"})}),this.config.autoRestore&&await this.restoreSession(),this.initialized=!0,this.log("SessionManager initialized successfully")}async login(t,e){try{this.log("Attempting login...");let r=await _crudifybrowser2.default.login(t,e);if(!r.success)return this.log("Login failed:",r.errors),{success:!1,error:this.formatError(r.errors),rawResponse:r};let n={accessToken:r.data.token,refreshToken:r.data.refreshToken,expiresAt:r.data.expiresAt,refreshExpiresAt:r.data.refreshExpiresAt};return g.saveTokens(n),this.lastActivityTime=Date.now(),this.log("Login successful, tokens saved"),_optionalChain([this, 'access', _2 => _2.config, 'access', _3 => _3.onLoginSuccess, 'optionalCall', _4 => _4(n)]),{success:!0,tokens:n,data:r.data}}catch(r){return this.log("Login error:",r),{success:!1,error:r instanceof Error?r.message:"Unknown error"}}}async logout(){try{this.log("Logging out..."),await _crudifybrowser2.default.logout(),g.clearTokens(),this.log("Logout successful"),_optionalChain([this, 'access', _5 => _5.config, 'access', _6 => _6.onLogout, 'optionalCall', _7 => _7()])}catch(t){this.log("Logout error:",t),g.clearTokens()}}async restoreSession(){try{this.log("Attempting to restore session...");let t=g.getTokens();if(!t)return this.log("No valid tokens found in storage"),!1;if(Date.now()>=t.refreshExpiresAt)return this.log("Refresh token expired, clearing storage"),g.clearTokens(),!1;if(_crudifybrowser2.default.setTokens({accessToken:t.accessToken,refreshToken:t.refreshToken,expiresAt:t.expiresAt,refreshExpiresAt:t.refreshExpiresAt}),_crudifybrowser2.default.getTokenData().isValid===!1){if(this.log("Restored access token is invalid or expired"),Date.now()<t.refreshExpiresAt&&(this.log("Access token expired but refresh is valid, attempting refresh..."),await this.refreshTokens())){this.log("Session restored successfully via token refresh");let n=g.getTokens();return n&&_optionalChain([this, 'access', _8 => _8.config, 'access', _9 => _9.onSessionRestored, 'optionalCall', _10 => _10(n)]),!0}return g.clearTokens(),await _crudifybrowser2.default.logout(),!1}return this.log("Session restored successfully"),this.lastActivityTime=Date.now(),_optionalChain([this, 'access', _11 => _11.config, 'access', _12 => _12.onSessionRestored, 'optionalCall', _13 => _13(t)]),!0}catch(t){return this.log("Session restore error:",t),g.clearTokens(),await _crudifybrowser2.default.logout(),!1}}isAuthenticated(){return _crudifybrowser2.default.isLogin()||g.hasValidTokens()}getTokenInfo(){let t=_crudifybrowser2.default.getTokenData(),e=g.getExpirationInfo();return{isLoggedIn:this.isAuthenticated(),crudifyTokens:t,storageInfo:e,hasValidTokens:g.hasValidTokens()}}async refreshTokens(){try{this.log("Manually refreshing tokens...");let t=await _crudifybrowser2.default.refreshAccessToken();if(!t.success)return this.log("Token refresh failed:",t.errors),g.clearTokens(),_optionalChain([this, 'access', _14 => _14.config, 'access', _15 => _15.showNotification, 'optionalCall', _16 => _16(this.getSessionExpiredMessage(),"warning")]),_optionalChain([this, 'access', _17 => _17.config, 'access', _18 => _18.onSessionExpired, 'optionalCall', _19 => _19()]),!1;let e={accessToken:t.data.token,refreshToken:t.data.refreshToken,expiresAt:t.data.expiresAt,refreshExpiresAt:t.data.refreshExpiresAt};return g.saveTokens(e),this.log("Tokens refreshed and saved successfully"),this.lastActivityTime=Date.now(),!0}catch(t){return this.log("Token refresh error:",t),g.clearTokens(),_optionalChain([this, 'access', _20 => _20.config, 'access', _21 => _21.showNotification, 'optionalCall', _22 => _22(this.getSessionExpiredMessage(),"warning")]),_optionalChain([this, 'access', _23 => _23.config, 'access', _24 => _24.onSessionExpired, 'optionalCall', _25 => _25()]),!1}}setupResponseInterceptor(){_crudifybrowser2.default.setResponseInterceptor(async t=>{this.updateLastActivity();let e=this.detectAuthorizationError(t);if(e.isAuthError){if(this.log("\u{1F6A8} Authorization error detected:",{errorType:e.errorType,shouldLogout:e.shouldTriggerLogout,message:e.userFriendlyMessage}),e.isRefreshTokenInvalid||e.isTokenRefreshFailed)return this.log("Refresh token invalid, emitting TOKEN_REFRESH_FAILED event"),_chunkATAGEVFKjs.f.emit("TOKEN_REFRESH_FAILED",{message:e.userFriendlyMessage,error:e.errorDetails,source:"SessionManager.setupResponseInterceptor"}),t;e.shouldTriggerLogout&&(g.hasValidTokens()&&!e.isIrrecoverable?(this.log("Attempting token refresh after auth error..."),await this.refreshTokens()||(this.log("Token refresh failed, emitting SESSION_EXPIRED event"),_chunkATAGEVFKjs.f.emit("SESSION_EXPIRED",{message:e.userFriendlyMessage,error:e.errorDetails,source:"SessionManager.setupResponseInterceptor (refresh failed)"}))):(this.log("No valid tokens or irrecoverable error, emitting SESSION_EXPIRED event"),_chunkATAGEVFKjs.f.emit("SESSION_EXPIRED",{message:e.userFriendlyMessage,error:e.errorDetails,source:"SessionManager.setupResponseInterceptor (no tokens)"})))}return t}),this.log("Response interceptor configured")}detectAuthorizationError(t){let e={isAuthError:!1,isRefreshTokenInvalid:!1,isTokenRefreshFailed:!1,isTokenExpired:!1,isUnauthorized:!1,isIrrecoverable:!1,shouldTriggerLogout:!1,errorType:"",errorDetails:null,userFriendlyMessage:""};if(t.errors&&Array.isArray(t.errors)){let r=t.errors.find(n=>n.errorType==="Unauthorized"||_optionalChain([n, 'access', _26 => _26.message, 'optionalAccess', _27 => _27.includes, 'call', _28 => _28("Unauthorized")])||_optionalChain([n, 'access', _29 => _29.message, 'optionalAccess', _30 => _30.includes, 'call', _31 => _31("Not Authorized")])||_optionalChain([n, 'access', _32 => _32.message, 'optionalAccess', _33 => _33.includes, 'call', _34 => _34("NOT_AUTHORIZED")])||_optionalChain([n, 'access', _35 => _35.message, 'optionalAccess', _36 => _36.includes, 'call', _37 => _37("Token")])||_optionalChain([n, 'access', _38 => _38.message, 'optionalAccess', _39 => _39.includes, 'call', _40 => _40("TOKEN")])||_optionalChain([n, 'access', _41 => _41.message, 'optionalAccess', _42 => _42.includes, 'call', _43 => _43("Authentication")])||_optionalChain([n, 'access', _44 => _44.message, 'optionalAccess', _45 => _45.includes, 'call', _46 => _46("UNAUTHENTICATED")])||_optionalChain([n, 'access', _47 => _47.extensions, 'optionalAccess', _48 => _48.code])==="UNAUTHENTICATED"||_optionalChain([n, 'access', _49 => _49.extensions, 'optionalAccess', _50 => _50.code])==="FORBIDDEN");r&&(e.isAuthError=!0,e.errorType="GraphQL Array",e.errorDetails=r,e.shouldTriggerLogout=!0,e.userFriendlyMessage="Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente.",(_optionalChain([r, 'access', _51 => _51.message, 'optionalAccess', _52 => _52.includes, 'call', _53 => _53("TOKEN")])||_optionalChain([r, 'access', _54 => _54.message, 'optionalAccess', _55 => _55.includes, 'call', _56 => _56("Token")]))&&(e.isTokenExpired=!0),_optionalChain([r, 'access', _57 => _57.extensions, 'optionalAccess', _58 => _58.code])==="UNAUTHENTICATED"&&(e.isUnauthorized=!0))}if(!e.isAuthError&&t.errors&&typeof t.errors=="object"&&!Array.isArray(t.errors)){let n=Object.values(t.errors).flat().find(u=>typeof u=="string"&&(u.includes("NOT_AUTHORIZED")||u.includes("TOKEN_REFRESH_FAILED")||u.includes("TOKEN_HAS_EXPIRED")||u.includes("PLEASE_LOGIN")||u.includes("Unauthorized")||u.includes("UNAUTHENTICATED")||u.includes("SESSION_EXPIRED")||u.includes("INVALID_TOKEN")));n&&typeof n=="string"&&(e.isAuthError=!0,e.errorType="GraphQL Object",e.errorDetails=t.errors,e.shouldTriggerLogout=!0,n.includes("TOKEN_REFRESH_FAILED")?(e.isTokenRefreshFailed=!0,e.isRefreshTokenInvalid=!0,e.isIrrecoverable=!0,e.userFriendlyMessage="Tu sesi\xF3n ha caducado. Por favor, inicia sesi\xF3n nuevamente."):n.includes("TOKEN_HAS_EXPIRED")||n.includes("SESSION_EXPIRED")?(e.isTokenExpired=!0,e.userFriendlyMessage="Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente."):n.includes("INVALID_TOKEN")?(e.isTokenExpired=!0,e.isIrrecoverable=!0,e.userFriendlyMessage="Token inv\xE1lido. Por favor, inicia sesi\xF3n nuevamente."):e.userFriendlyMessage="Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente.")}if(!e.isAuthError&&_optionalChain([t, 'access', _59 => _59.data, 'optionalAccess', _60 => _60.response, 'optionalAccess', _61 => _61.status])){let r=t.data.response.status.toUpperCase();(r==="UNAUTHORIZED"||r==="UNAUTHENTICATED")&&(e.isAuthError=!0,e.errorType="Status",e.errorDetails=t.data.response,e.isUnauthorized=!0,e.shouldTriggerLogout=!0,e.isIrrecoverable=!0,e.userFriendlyMessage="Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente.")}if(!e.isAuthError&&_optionalChain([t, 'access', _62 => _62.data, 'optionalAccess', _63 => _63.response, 'optionalAccess', _64 => _64.data]))try{let r=typeof t.data.response.data=="string"?JSON.parse(t.data.response.data):t.data.response.data;(r.error==="REFRESH_TOKEN_INVALID"||r.error==="TOKEN_EXPIRED"||r.error==="INVALID_TOKEN")&&(e.isAuthError=!0,e.errorType="Parsed Data",e.errorDetails=r,e.shouldTriggerLogout=!0,e.isIrrecoverable=!0,r.error==="REFRESH_TOKEN_INVALID"?(e.isRefreshTokenInvalid=!0,e.isTokenRefreshFailed=!0,e.userFriendlyMessage="Tu sesi\xF3n ha caducado. Por favor, inicia sesi\xF3n nuevamente."):(e.isTokenExpired=!0,e.userFriendlyMessage="Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente."))}catch (e4){}if(!e.isAuthError&&t.errorCode){let r=t.errorCode.toUpperCase();(r==="UNAUTHORIZED"||r==="UNAUTHENTICATED"||r==="TOKEN_EXPIRED"||r==="INVALID_TOKEN")&&(e.isAuthError=!0,e.errorType="Error Code",e.errorDetails={errorCode:r},e.shouldTriggerLogout=!0,r==="TOKEN_EXPIRED"?e.isTokenExpired=!0:e.isUnauthorized=!0,e.userFriendlyMessage="Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente.")}return e}updateLastActivity(){this.lastActivityTime=Date.now(),this.log("Last activity updated")}getTimeSinceLastActivity(){return this.lastActivityTime===0?0:Date.now()-this.lastActivityTime}checkInactivity(){let t=this.getTimeSinceLastActivity(),e=_crudifybrowser2.default.getTokenData();if(this.lastActivityTime===0)return"none";let r=900*1e3,n=300*1e3,u=300*1e3;return t>r?(this.log(`Inactivity timeout: ${Math.floor(t/6e4)} minutes since last activity`),"logout"):t<n&&e.expiresIn<u&&e.expiresIn>0?(this.log(`User active recently (${Math.floor(t/6e4)}min ago) and token expiring soon, should refresh`),"refresh"):"none"}clearSession(){g.clearTokens(),_crudifybrowser2.default.logout(),this.lastActivityTime=0,this.log("Session cleared completely")}getSessionExpiredMessage(){return this.config.translateFn?_chunkATAGEVFKjs.b.call(void 0, "SESSION_EXPIRED",{translateFn:this.config.translateFn,enableDebug:this.config.enableLogging}):"Tu sesi\xF3n ha expirado. Por favor, inicia sesi\xF3n nuevamente."}log(t,...e){this.config.enableLogging&&console.log(`[SessionManager] ${t}`,...e)}formatError(t){return t?typeof t=="string"?t:typeof t=="object"?Object.values(t).flat().join(", "):"Authentication failed":"Unknown error"}};var _react = require('react'); var _react2 = _interopRequireDefault(_react);function X(o={}){let[t,e]=_react.useState.call(void 0, {isAuthenticated:!1,isLoading:!0,isInitialized:!1,tokens:null,error:null}),r=O.getInstance(),n=_react.useCallback.call(void 0, async()=>{try{e(c=>({...c,isLoading:!0,error:null}));let l={autoRestore:_nullishCoalesce(o.autoRestore, () => (!0)),enableLogging:_nullishCoalesce(o.enableLogging, () => (!1)),showNotification:o.showNotification,translateFn:o.translateFn,onSessionExpired:()=>{e(c=>({...c,isAuthenticated:!1,tokens:null,error:"Session expired"})),_optionalChain([o, 'access', _65 => _65.onSessionExpired, 'optionalCall', _66 => _66()])},onSessionRestored:c=>{e(f=>({...f,isAuthenticated:!0,tokens:c,error:null})),_optionalChain([o, 'access', _67 => _67.onSessionRestored, 'optionalCall', _68 => _68(c)])},onLoginSuccess:c=>{e(f=>({...f,isAuthenticated:!0,tokens:c,error:null}))},onLogout:()=>{e(c=>({...c,isAuthenticated:!1,tokens:null,error:null}))}};await r.initialize(l),r.setupResponseInterceptor();let s=r.isAuthenticated(),i=r.getTokenInfo();e(c=>({...c,isAuthenticated:s,isInitialized:!0,isLoading:!1,tokens:i.crudifyTokens.accessToken?{accessToken:i.crudifyTokens.accessToken,refreshToken:i.crudifyTokens.refreshToken,expiresAt:i.crudifyTokens.expiresAt,refreshExpiresAt:i.crudifyTokens.refreshExpiresAt}:null}))}catch(l){let s=l instanceof Error?l.message:"Initialization failed";e(i=>({...i,isLoading:!1,isInitialized:!0,error:s}))}},[o.autoRestore,o.enableLogging,o.onSessionExpired,o.onSessionRestored]),u=_react.useCallback.call(void 0, async(l,s)=>{e(i=>({...i,isLoading:!0,error:null}));try{let i=await r.login(l,s);return i.success&&i.tokens?e(c=>({...c,isAuthenticated:!0,tokens:i.tokens,isLoading:!1,error:null})):e(c=>({...c,isAuthenticated:!1,tokens:null,isLoading:!1,error:null})),i}catch(i){let c=i instanceof Error?i.message:"Login failed",f=c.includes("INVALID_CREDENTIALS")||c.includes("Invalid email")||c.includes("Invalid password")||c.includes("credentials");return e(T=>({...T,isAuthenticated:!1,tokens:null,isLoading:!1,error:f?null:c})),{success:!1,error:c}}},[r]),m=_react.useCallback.call(void 0, async()=>{e(l=>({...l,isLoading:!0}));try{await r.logout(),e(l=>({...l,isAuthenticated:!1,tokens:null,isLoading:!1,error:null}))}catch(l){e(s=>({...s,isAuthenticated:!1,tokens:null,isLoading:!1,error:l instanceof Error?l.message:"Logout error"}))}},[r]),p=_react.useCallback.call(void 0, async()=>{try{let l=await r.refreshTokens();if(l){let s=r.getTokenInfo();e(i=>({...i,tokens:s.crudifyTokens.accessToken?{accessToken:s.crudifyTokens.accessToken,refreshToken:s.crudifyTokens.refreshToken,expiresAt:s.crudifyTokens.expiresAt,refreshExpiresAt:s.crudifyTokens.refreshExpiresAt}:null,error:null}))}else e(s=>({...s,isAuthenticated:!1,tokens:null,error:"Token refresh failed"}));return l}catch(l){return e(s=>({...s,isAuthenticated:!1,tokens:null,error:l instanceof Error?l.message:"Token refresh failed"})),!1}},[r]),S=_react.useCallback.call(void 0, ()=>{e(l=>({...l,error:null}))},[]),k=_react.useCallback.call(void 0, ()=>r.getTokenInfo(),[r]);_react.useEffect.call(void 0, ()=>{n()},[n]),_react.useEffect.call(void 0, ()=>{if(!t.isAuthenticated||!t.tokens)return;let l=()=>{r.updateLastActivity(),o.enableLogging&&console.log("\u{1F4CD} User navigating - activity updated")};window.addEventListener("popstate",l);let s=window.history.pushState,i=window.history.replaceState;window.history.pushState=function(...f){let T=s.apply(this,f);return l(),T},window.history.replaceState=function(...f){let T=i.apply(this,f);return l(),T};let c=setInterval(async()=>{let f=r.checkInactivity();if(f==="logout")o.enableLogging&&console.log("\u23F1\uFE0F Inactivity timeout - logging out user"),await m();else if(f==="refresh"&&(o.enableLogging&&console.log("\u{1F504} User active, token expiring soon - refreshing..."),await r.refreshTokens())){let h=r.getTokenInfo();e(b=>({...b,tokens:h.crudifyTokens.accessToken?{accessToken:h.crudifyTokens.accessToken,refreshToken:h.crudifyTokens.refreshToken,expiresAt:h.crudifyTokens.expiresAt,refreshExpiresAt:h.crudifyTokens.refreshExpiresAt}:null}))}},120*1e3);return()=>{clearInterval(c),window.removeEventListener("popstate",l),window.history.pushState=s,window.history.replaceState=i}},[t.isAuthenticated,t.tokens,r,o.enableLogging,m]),_react.useEffect.call(void 0, ()=>{let l=g.subscribeToChanges(s=>{s?(o.enableLogging&&console.log("\u{1F504} Tokens updated in another tab"),e(i=>({...i,tokens:s,isAuthenticated:!0}))):(o.enableLogging&&console.log("\u{1F504} Logout detected in another tab"),e(i=>({...i,isAuthenticated:!1,tokens:null})),_chunkATAGEVFKjs.f.emit("SESSION_EXPIRED",{message:"Sesi\xF3n cerrada en otra pesta\xF1a",source:"CrossTabSync"}))});return()=>l()},[o.enableLogging]);let x=_react.useCallback.call(void 0, ()=>{r.updateLastActivity()},[r]);return{...t,login:u,logout:m,refreshTokens:p,clearError:S,getTokenInfo:k,updateActivity:x,isExpiringSoon:t.tokens?t.tokens.expiresAt-Date.now()<300*1e3:!1,expiresIn:t.tokens?Math.max(0,t.tokens.expiresAt-Date.now()):0,refreshExpiresIn:t.tokens?Math.max(0,t.tokens.refreshExpiresAt-Date.now()):0}}var _material = require('@mui/material');var _uuid = require('uuid');var _dompurify = require('dompurify'); var _dompurify2 = _interopRequireDefault(_dompurify);var _jsxruntime = require('react/jsx-runtime');var B=_react.createContext.call(void 0, null),de=o=>_dompurify2.default.sanitize(o,{ALLOWED_TAGS:["b","i","em","strong","br","span"],ALLOWED_ATTR:["class"],FORBID_TAGS:["script","iframe","object","embed"],FORBID_ATTR:["onload","onerror","onclick","onmouseover","onfocus","onblur"],WHOLE_DOCUMENT:!1,RETURN_DOM:!1,RETURN_DOM_FRAGMENT:!1,RETURN_TRUSTED_TYPE:!1}),W= exports.d =({children:o,maxNotifications:t=5,defaultAutoHideDuration:e=6e3,position:r={vertical:"top",horizontal:"right"},enabled:n=!1,allowHtml:u=!1})=>{let[m,p]=_react.useState.call(void 0, []),S=_react.useCallback.call(void 0, (s,i="info",c)=>{if(!n)return"";if(!s||typeof s!="string")return console.warn("\u26A0\uFE0F GlobalNotificationProvider: Invalid message provided"),"";s.length>1e3&&(console.warn("\u26A0\uFE0F GlobalNotificationProvider: Message too long, truncating"),s=s.substring(0,1e3)+"...");let f=_uuid.v4.call(void 0, ),T={id:f,message:s,severity:i,autoHideDuration:_nullishCoalesce(_optionalChain([c, 'optionalAccess', _69 => _69.autoHideDuration]), () => (e)),persistent:_nullishCoalesce(_optionalChain([c, 'optionalAccess', _70 => _70.persistent]), () => (!1)),allowHtml:_nullishCoalesce(_optionalChain([c, 'optionalAccess', _71 => _71.allowHtml]), () => (u))};return p(h=>[...h.length>=t?h.slice(-(t-1)):h,T]),f},[t,e,n,u]),k=_react.useCallback.call(void 0, s=>{p(i=>i.filter(c=>c.id!==s))},[]),x=_react.useCallback.call(void 0, ()=>{p([])},[]),l={showNotification:S,hideNotification:k,clearAllNotifications:x};return _jsxruntime.jsxs.call(void 0, B.Provider,{value:l,children:[o,n&&_jsxruntime.jsx.call(void 0, _material.Portal,{children:_jsxruntime.jsx.call(void 0, _material.Box,{sx:{position:"fixed",zIndex:9999,[r.vertical]:(r.vertical==="top",24),[r.horizontal]:r.horizontal==="right"||r.horizontal==="left"?24:"50%",...r.horizontal==="center"&&{transform:"translateX(-50%)"},display:"flex",flexDirection:r.vertical==="top"?"column":"column-reverse",gap:1,maxWidth:"400px",width:"auto"},children:m.map(s=>_jsxruntime.jsx.call(void 0, fe,{notification:s,onClose:()=>k(s.id)},s.id))})})]})},fe=({notification:o,onClose:t})=>{let[e,r]=_react.useState.call(void 0, !0),n=_react.useCallback.call(void 0, (u,m)=>{m!=="clickaway"&&(r(!1),setTimeout(t,300))},[t]);return _react.useEffect.call(void 0, ()=>{if(!o.persistent&&o.autoHideDuration){let u=setTimeout(()=>{n()},o.autoHideDuration);return()=>clearTimeout(u)}},[o.autoHideDuration,o.persistent,n]),_jsxruntime.jsx.call(void 0, _material.Snackbar,{open:e,onClose:n,sx:{position:"relative","& .MuiSnackbarContent-root":{minWidth:"auto"}},TransitionProps:{enter:!0,exit:!0},children:_jsxruntime.jsx.call(void 0, _material.Alert,{variant:"filled",severity:o.severity,onClose:n,sx:{width:"100%",minWidth:"280px",maxWidth:"400px",wordBreak:"break-word"},children:o.allowHtml?_jsxruntime.jsx.call(void 0, "span",{dangerouslySetInnerHTML:{__html:de(o.message)}}):_jsxruntime.jsx.call(void 0, "span",{children:o.message})})})},J= exports.e =()=>{let o=_react.useContext.call(void 0, B);if(!o)throw new Error("useGlobalNotification debe ser usado dentro de un GlobalNotificationProvider");return o};var Z=_react.createContext.call(void 0, void 0);function Te({children:o,options:t={},config:e,showNotifications:r=!1,notificationOptions:n={}}){let u;try{let{showNotification:s}=J();u=s}catch (e5){}let m=_react2.default.useMemo(()=>({...t,showNotification:u,onSessionExpired:()=>{_optionalChain([t, 'access', _72 => _72.onSessionExpired, 'optionalCall', _73 => _73()])}}),[t,u]),p=X(m),S=_react.useMemo.call(void 0, ()=>{let s,i,c,f,T,h="unknown";if(_optionalChain([e, 'optionalAccess', _74 => _74.publicApiKey])&&(s=e.publicApiKey,h="props"),_optionalChain([e, 'optionalAccess', _75 => _75.env])&&(i=e.env),_optionalChain([e, 'optionalAccess', _76 => _76.appName])&&(c=e.appName),_optionalChain([e, 'optionalAccess', _77 => _77.loginActions])&&(f=e.loginActions),_optionalChain([e, 'optionalAccess', _78 => _78.logo])&&(T=e.logo),!s){let b=_chunkATAGEVFKjs.a.call(void 0, "publicApiKey"),N=_chunkATAGEVFKjs.a.call(void 0, "environment"),w=_chunkATAGEVFKjs.a.call(void 0, "appName"),A=_chunkATAGEVFKjs.a.call(void 0, "loginActions"),d=_chunkATAGEVFKjs.a.call(void 0, "logo");b&&(s=b,h="cookies"),N&&["dev","stg","prod"].includes(N)&&(i=N),w&&(c=decodeURIComponent(w)),A&&(f=decodeURIComponent(A).split(",").map(P=>P.trim()).filter(Boolean)),d&&(T=decodeURIComponent(d))}return{publicApiKey:s,env:i,appName:c,loginActions:f,logo:T}},[e]),k=_react.useMemo.call(void 0, ()=>{if(!_optionalChain([p, 'access', _79 => _79.tokens, 'optionalAccess', _80 => _80.accessToken])||!p.isAuthenticated)return null;try{let s=_chunkATAGEVFKjs.g.call(void 0, p.tokens.accessToken);if(s&&s.sub&&s.email&&s.subscriber){let i={_id:s.sub,email:s.email,subscriberKey:s.subscriber};return Object.keys(s).forEach(c=>{["sub","email","subscriber"].includes(c)||(i[c]=s[c])}),i}}catch(s){console.error("Error decoding JWT token for sessionData:",s)}return null},[_optionalChain([p, 'access', _81 => _81.tokens, 'optionalAccess', _82 => _82.accessToken]),p.isAuthenticated]),x={...p,sessionData:k,config:S},l={enabled:r,maxNotifications:n.maxNotifications||5,defaultAutoHideDuration:n.defaultAutoHideDuration||6e3,position:n.position||{vertical:"top",horizontal:"right"}};return _jsxruntime.jsx.call(void 0, Z.Provider,{value:x,children:o})}function Ze(o){let t={enabled:o.showNotifications,maxNotifications:_optionalChain([o, 'access', _83 => _83.notificationOptions, 'optionalAccess', _84 => _84.maxNotifications])||5,defaultAutoHideDuration:_optionalChain([o, 'access', _85 => _85.notificationOptions, 'optionalAccess', _86 => _86.defaultAutoHideDuration])||6e3,position:_optionalChain([o, 'access', _87 => _87.notificationOptions, 'optionalAccess', _88 => _88.position])||{vertical:"top",horizontal:"right"},allowHtml:_optionalChain([o, 'access', _89 => _89.notificationOptions, 'optionalAccess', _90 => _90.allowHtml])||!1};return _jsxruntime.jsx.call(void 0, W,{...t,children:_jsxruntime.jsx.call(void 0, Te,{...o})})}function q(){let o=_react.useContext.call(void 0, Z);if(o===void 0)throw new Error("useSessionContext must be used within a SessionProvider");return o}function qe({children:o,fallback:t=_jsxruntime.jsx.call(void 0, "div",{children:"Please log in to access this content"}),redirectTo:e}){let{isAuthenticated:r,isLoading:n,isInitialized:u}=q();return!u||n?_jsxruntime.jsx.call(void 0, "div",{children:"Loading..."}):r?_jsxruntime.jsx.call(void 0, _jsxruntime.Fragment,{children:o}):e?(e(),null):_jsxruntime.jsx.call(void 0, _jsxruntime.Fragment,{children:t})}function Qe(){let o=q();return o.isInitialized?_jsxruntime.jsxs.call(void 0, "div",{style:{padding:"10px",margin:"10px",border:"1px solid #ccc",borderRadius:"4px",fontSize:"12px",fontFamily:"monospace"},children:[_jsxruntime.jsx.call(void 0, "h4",{children:"Session Debug Info"}),_jsxruntime.jsxs.call(void 0, "div",{children:[_jsxruntime.jsx.call(void 0, "strong",{children:"Authenticated:"})," ",o.isAuthenticated?"Yes":"No"]}),_jsxruntime.jsxs.call(void 0, "div",{children:[_jsxruntime.jsx.call(void 0, "strong",{children:"Loading:"})," ",o.isLoading?"Yes":"No"]}),_jsxruntime.jsxs.call(void 0, "div",{children:[_jsxruntime.jsx.call(void 0, "strong",{children:"Error:"})," ",o.error||"None"]}),o.tokens&&_jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment,{children:[_jsxruntime.jsxs.call(void 0, "div",{children:[_jsxruntime.jsx.call(void 0, "strong",{children:"Access Token:"})," ",o.tokens.accessToken.substring(0,20),"..."]}),_jsxruntime.jsxs.call(void 0, "div",{children:[_jsxruntime.jsx.call(void 0, "strong",{children:"Refresh Token:"})," ",o.tokens.refreshToken.substring(0,20),"..."]}),_jsxruntime.jsxs.call(void 0, "div",{children:[_jsxruntime.jsx.call(void 0, "strong",{children:"Access Expires In:"})," ",Math.round(o.expiresIn/1e3/60)," minutes"]}),_jsxruntime.jsxs.call(void 0, "div",{children:[_jsxruntime.jsx.call(void 0, "strong",{children:"Refresh Expires In:"})," ",Math.round(o.refreshExpiresIn/1e3/60/60)," hours"]}),_jsxruntime.jsxs.call(void 0, "div",{children:[_jsxruntime.jsx.call(void 0, "strong",{children:"Expiring Soon:"})," ",o.isExpiringSoon?"Yes":"No"]})]})]}):_jsxruntime.jsx.call(void 0, "div",{children:"Session not initialized"})}var nt=(o={})=>{let{autoFetch:t=!0,retryOnError:e=!1,maxRetries:r=3}=o,[n,u]=_react.useState.call(void 0, null),[m,p]=_react.useState.call(void 0, !1),[S,k]=_react.useState.call(void 0, null),[x,l]=_react.useState.call(void 0, {}),s=_react.useRef.call(void 0, null),i=_react.useRef.call(void 0, !0),c=_react.useRef.call(void 0, 0),f=_react.useRef.call(void 0, 0),T=_react.useCallback.call(void 0, ()=>{u(null),k(null),p(!1),l({})},[]),h=_react.useCallback.call(void 0, async()=>{let b=_chunkATAGEVFKjs.h.call(void 0, );if(!b){i.current&&(k("No user email available"),p(!1));return}s.current&&s.current.abort();let N=new AbortController;s.current=N;let w=++c.current;try{i.current&&(p(!0),k(null));let A=await _crudifybrowser2.default.readItems("users",{filter:{email:b},pagination:{limit:1}});if(w===c.current&&i.current&&!N.signal.aborted)if(A.success&&A.data&&A.data.length>0){let d=A.data[0];u(d);let F={fullProfile:d,totalFields:Object.keys(d).length,displayData:{id:d.id,email:d.email,username:d.username,firstName:d.firstName,lastName:d.lastName,fullName:d.fullName||`${d.firstName||""} ${d.lastName||""}`.trim(),role:d.role,permissions:d.permissions||[],isActive:d.isActive,lastLogin:d.lastLogin,createdAt:d.createdAt,updatedAt:d.updatedAt,...Object.keys(d).filter(P=>!["id","email","username","firstName","lastName","fullName","role","permissions","isActive","lastLogin","createdAt","updatedAt"].includes(P)).reduce((P,G)=>({...P,[G]:d[G]}),{})}};l(F),k(null),f.current=0}else k("User profile not found"),u(null),l({})}catch(A){if(w===c.current&&i.current){let d=A;if(d.name==="AbortError")return;e&&f.current<r&&(_optionalChain([d, 'access', _91 => _91.message, 'optionalAccess', _92 => _92.includes, 'call', _93 => _93("Network Error")])||_optionalChain([d, 'access', _94 => _94.message, 'optionalAccess', _95 => _95.includes, 'call', _96 => _96("Failed to fetch")]))?(f.current++,setTimeout(()=>{i.current&&h()},1e3*f.current)):(k("Failed to load user profile"),u(null),l({}))}}finally{w===c.current&&i.current&&p(!1),s.current===N&&(s.current=null)}},[e,r]);return _react.useEffect.call(void 0, ()=>{t&&h()},[t,h]),_react.useEffect.call(void 0, ()=>(i.current=!0,()=>{i.current=!1,s.current&&(s.current.abort(),s.current=null)}),[]),{userProfile:n,loading:m,error:S,extendedData:x,refreshProfile:h,clearProfile:T}};exports.a = g; exports.b = O; exports.c = X; exports.d = W; exports.e = J; exports.f = Ze; exports.g = q; exports.h = qe; exports.i = Qe; exports.j = nt;
|