fix: almacenar token inmediatamente tras signInWithPassword y redirigir si ya posee token activo
This commit is contained in:
parent
91888ed15a
commit
1bd29d296b
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -50,30 +50,37 @@ export const api = {
|
||||||
throw new Error(error.message);
|
throw new Error(error.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obtener perfil público asociado al usuario
|
// Guardar token inmediatamente
|
||||||
const { data: profile, error: profileError } = await supabase
|
if (data?.session?.access_token) {
|
||||||
.from("profiles")
|
localStorage.setItem("repairit_token", data.session.access_token);
|
||||||
.select("*, organization:organizations(*), venue:venues(*)")
|
}
|
||||||
.eq("id", data.user.id)
|
|
||||||
.single();
|
|
||||||
|
|
||||||
if (profileError) {
|
// Obtener perfil público asociado al usuario
|
||||||
throw new Error("No se pudo obtener el perfil del 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 = {
|
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;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user