// Autenticación real contra el backend Lumen de Sky Balloons. import { apiFetch, guardarToken, API_BASE } from "./api-client"; export { API_BASE }; export type LoginRespuesta = { message: string; token: string; token_type: string; expires_at: string; socio: { id: number; email: string }; }; type LoginCruda = Partial & { access_token?: string; expires_in?: number; socio_comercial?: { id?: number; email?: string }; data?: { token?: string; access_token?: string; socio?: { id?: number; email?: string } }; }; export async function loginSocio(email: string, password: string): Promise { const cruda = await apiFetch("/socios/login", { method: "POST", body: { email, password }, mensajeError: "No pudimos iniciar sesión. Intenta de nuevo.", }); const token = cruda.token ?? cruda.access_token ?? cruda.data?.token ?? cruda.data?.access_token ?? ""; if (!token) throw new Error("No pudimos iniciar sesión. Intenta de nuevo."); const socio = cruda.socio ?? cruda.socio_comercial ?? cruda.data?.socio ?? {}; const expira = cruda.expires_at ?? new Date(Date.now() + (cruda.expires_in ? cruda.expires_in * 1000 : 12 * 60 * 60 * 1000)).toISOString(); // Persistimos el token para adjuntarlo como Bearer en las rutas protegidas. guardarToken(token); return { message: cruda.message ?? "Sesión iniciada", token, token_type: cruda.token_type ?? "Bearer", expires_at: expira, socio: { id: Number(socio.id ?? 0), email: String(socio.email ?? email) }, }; }