From d711146674299da19ec88cc0d12097d26ae29b72 Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 26 Aug 2026 09:50:43 -0500 Subject: [PATCH] Actualizar frontend/src/lib/auth.jsx --- frontend/src/lib/auth.jsx | 66 ++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/frontend/src/lib/auth.jsx b/frontend/src/lib/auth.jsx index a930b8c..81b7a79 100644 --- a/frontend/src/lib/auth.jsx +++ b/frontend/src/lib/auth.jsx @@ -42,27 +42,14 @@ export function AuthProvider({ children }) { useEffect(() => { let activo = true; - // Respaldo: si en 10s no termina (sesion colgada), apaga "Cargando..." igual. - const tSeguridad = setTimeout(() => { if (activo) setCargando(false); }, 10000); - (async () => { - try { - // getSession con timeout de 8s: si no responde, no bloquea el arranque. - const conTimeout = (p, ms) => Promise.race([ - p, new Promise((_, rej) => setTimeout(() => rej(new Error("timeout")), ms)), - ]); - const { data } = await conTimeout(supabase.auth.getSession(), 8000); - if (!activo) return; - const s = data?.session || null; - setSession(s); - if (s?.user) await cargarPerfil(s.user.id); - } catch (e) { - console.error("[auth] getSession falló:", e); - } finally { - clearTimeout(tSeguridad); - if (activo) setCargando(false); - } - })(); + let resuelto = false; + const terminar = () => { + if (activo) { setCargando(false); resuelto = true; sessionStorage.removeItem("auth_reload"); } + }; + + // Fuente principal: onAuthStateChange dispara INITIAL_SESSION al arrancar + // con la sesión persistida (renueva token solo). Maneja login/logout en vivo. const { data: sub } = supabase.auth.onAuthStateChange(async (_evt, s) => { try { setSession(s); @@ -71,11 +58,46 @@ export function AuthProvider({ children }) { } catch (e) { console.error("[auth] onAuthStateChange falló:", e); } finally { - if (activo) setCargando(false); + terminar(); } }); - return () => { activo = false; clearTimeout(tSeguridad); sub?.subscription?.unsubscribe(); }; + // Respaldo: si el listener no disparó en 6s, intenta getSession una vez. + // Si trae sesión la usa; si no, NO fuerza login (deja que el listener resuelva). + const tRespaldo = setTimeout(async () => { + if (resuelto || !activo) return; + try { + const { data } = await supabase.auth.getSession(); + const s = data?.session || null; + if (s?.user && activo) { + setSession(s); + await cargarPerfil(s.user.id); + } + } catch (e) { + console.error("[auth] getSession (respaldo) falló:", e); + } finally { + terminar(); + } + }, 6000); + + // Si a los 8s sigue sin resolver, recarga la página UNA vez (F5 automático) + // en vez de mostrar el login pegado. sessionStorage evita bucle. + const tSeguridad = setTimeout(() => { + if (!activo || resuelto) return; + if (!sessionStorage.getItem("auth_reload")) { + sessionStorage.setItem("auth_reload", "1"); + window.location.reload(); + } else { + setCargando(false); + } + }, 8000); + + return () => { + activo = false; + clearTimeout(tRespaldo); + clearTimeout(tSeguridad); + sub?.subscription?.unsubscribe(); + }; }, [cargarPerfil]); const login = async (email, password) => {