From 579ee358a664ec3831a8cc7bedfac2fb2a23de5a Mon Sep 17 00:00:00 2001 From: MarioVicente Date: Tue, 7 Oct 2025 10:45:30 -0600 Subject: [PATCH] Fixing auth issues --- src/App.jsx | 23 ++++++++++++++++++----- staticwebapp.config.json | 8 +++----- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 696af97..b325ec6 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -15,15 +15,28 @@ const FinanceTracker = () => { // --- Sesión Entra ID (SWA) --- const [user, setUser] = useState(null); const [loadingUser, setLoadingUser] = useState(true); + useEffect(() => { - (async () => { - try { setUser(await getUser()); } - finally { setLoadingUser(false); } - })(); + let mounted = true; + (async () => { + try { + const u = await getUser(); + if (mounted) setUser(u); + } finally { + if (mounted) setLoadingUser(false); + } + })(); + return () => { mounted = false; }; }, []); if (loadingUser) { - return
Cargando sesión…
; + return
Cargando sesión…
; + } + + if (!user) { + // Con el config ya te redirige a /login en 401, pero este fallback ayuda + window.location.href = '/login'; + return null; } // Si no hay usuario aún, puedes mostrar un loader o un botón de login. diff --git a/staticwebapp.config.json b/staticwebapp.config.json index 752f92f..944e606 100644 --- a/staticwebapp.config.json +++ b/staticwebapp.config.json @@ -3,12 +3,10 @@ "routes": [ { "route": "/login", "redirect": "/.auth/login/aad?post_login_redirect_uri=/", "statusCode": 302 }, { "route": "/logout", "redirect": "/.auth/logout?post_logout_redirect_uri=/", "statusCode": 302 }, - - // protege API y frontend - { "route": "/api/*", "allowedRoles": ["authenticated"] }, - { "route": "/*", "allowedRoles": ["authenticated"] } + { "route": "/api/*", "allowedRoles": ["authenticated"] }, + { "route": "/*", "allowedRoles": ["authenticated"] } ], "responseOverrides": { - "401": { "redirect": "/login", "statusCode": 302 } // <- si no estás logueado, redirige al login + "401": { "redirect": "/login", "statusCode": 302 } } }