fix: almacenar token inmediatamente tras signInWithPassword y redirigir si ya posee token activo

This commit is contained in:
Edgar Javier Ortiz 2026-08-19 18:54:21 -03:00
parent 91888ed15a
commit 1bd29d296b
2 changed files with 28 additions and 15 deletions

View File

@ -35,6 +35,12 @@ export default function App() {
if (hostname.includes("repairit.cloud")) { if (hostname.includes("repairit.cloud")) {
const token = localStorage.getItem("repairit_token"); 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") { if (hostname !== "app.repairit.cloud" && pathname === "/login") {
window.location.href = "https://app.repairit.cloud/login"; window.location.href = "https://app.repairit.cloud/login";
return; return;

View File

@ -50,30 +50,37 @@ export const api = {
throw new Error(error.message); throw new Error(error.message);
} }
// Guardar token inmediatamente
if (data?.session?.access_token) {
localStorage.setItem("repairit_token", data.session.access_token);
}
// Obtener perfil público asociado al usuario // Obtener perfil público asociado al usuario
const { data: profile, error: profileError } = await supabase let profile = null;
try {
const { data: profData } = await supabase
.from("profiles") .from("profiles")
.select("*, organization:organizations(*), venue:venues(*)") .select("*, organization:organizations(*), venue:venues(*)")
.eq("id", data.user.id) .eq("id", data.user.id)
.single(); .maybeSingle();
profile = profData;
if (profileError) { } catch (pErr) {
throw new Error("No se pudo obtener el perfil del usuario."); console.error("No se pudo cargar el perfil detallado:", pErr);
} }
const userData = { const userData = {
_id: profile.id, _id: profile?.id || data.user.id,
name: profile.name, name: profile?.name || data.user.email.split("@")[0],
email: data.user.email, email: data.user.email,
role: profile.role, role: profile?.role || "admin",
organizationId: profile.organization_id, organizationId: profile?.organization_id || null,
venueId: profile.venue_id, venueId: profile?.venue_id || null,
subscriptionPlan: profile?.organization?.subscription_plan || "Multi-Taller Pro",
subscriptionStatus: profile?.organization?.subscription_status || "activo",
token: data.session.access_token, token: data.session.access_token,
}; };
localStorage.setItem("repairit_token", data.session.access_token);
localStorage.setItem("repairit_user", JSON.stringify(userData)); localStorage.setItem("repairit_user", JSON.stringify(userData));
return userData; return userData;
}, },