From 1bd29d296bbdba1f67e4b3bb500bf5a372ce924d Mon Sep 17 00:00:00 2001 From: Javi_Desktop Date: Wed, 19 Aug 2026 18:54:21 -0300 Subject: [PATCH] fix: almacenar token inmediatamente tras signInWithPassword y redirigir si ya posee token activo --- frontend/src/App.jsx | 6 ++++++ frontend/src/services/api.js | 37 +++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 1ca2f1f..6a6ad9a 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -35,6 +35,12 @@ export default function App() { if (hostname.includes("repairit.cloud")) { const token = localStorage.getItem("repairit_token"); + if (hostname === "app.repairit.cloud" && pathname === "/login") { + if (token) { + window.location.href = "/dashboard"; + return; + } + } if (hostname !== "app.repairit.cloud" && pathname === "/login") { window.location.href = "https://app.repairit.cloud/login"; return; diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js index 2354754..cf3a02a 100644 --- a/frontend/src/services/api.js +++ b/frontend/src/services/api.js @@ -50,30 +50,37 @@ export const api = { throw new Error(error.message); } - // Obtener perfil público asociado al usuario - const { data: profile, error: profileError } = await supabase - .from("profiles") - .select("*, organization:organizations(*), venue:venues(*)") - .eq("id", data.user.id) - .single(); + // Guardar token inmediatamente + if (data?.session?.access_token) { + localStorage.setItem("repairit_token", data.session.access_token); + } - if (profileError) { - throw new Error("No se pudo obtener el perfil del usuario."); + // Obtener perfil público asociado al usuario + let profile = null; + try { + const { data: profData } = await supabase + .from("profiles") + .select("*, organization:organizations(*), venue:venues(*)") + .eq("id", data.user.id) + .maybeSingle(); + profile = profData; + } catch (pErr) { + console.error("No se pudo cargar el perfil detallado:", pErr); } const userData = { - _id: profile.id, - name: profile.name, + _id: profile?.id || data.user.id, + name: profile?.name || data.user.email.split("@")[0], email: data.user.email, - role: profile.role, - organizationId: profile.organization_id, - venueId: profile.venue_id, + role: profile?.role || "admin", + organizationId: profile?.organization_id || null, + venueId: profile?.venue_id || null, + subscriptionPlan: profile?.organization?.subscription_plan || "Multi-Taller Pro", + subscriptionStatus: profile?.organization?.subscription_status || "activo", token: data.session.access_token, }; - localStorage.setItem("repairit_token", data.session.access_token); localStorage.setItem("repairit_user", JSON.stringify(userData)); - return userData; },