From 40a8c07b0db15aea93a1328071d96b4d0e40cb04 Mon Sep 17 00:00:00 2001 From: fede Date: Mon, 5 Jan 2026 16:21:54 -0300 Subject: [PATCH] ahora login usa la store directamente --- src/lib/hooks/login.ts | 64 ++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/src/lib/hooks/login.ts b/src/lib/hooks/login.ts index 7d9dc2d..2de7926 100644 --- a/src/lib/hooks/login.ts +++ b/src/lib/hooks/login.ts @@ -1,38 +1,30 @@ -import { apiBase } from "@/stores/url"; -import type { LoginDto } from "../../types"; -import { sesionStore } from "@/stores/usuario"; -import { goto } from "$app/navigation"; +import { apiBase } from '@/stores/url'; +import type { LoginDto } from '../../types'; +import { sesionStore } from '@/stores/usuario'; +import { goto } from '$app/navigation'; +import { get } from 'svelte/store'; -export async function login(e:SubmitEvent, dto: LoginDto, callbackfn:()=>void){ - e.preventDefault(); - if (dto.password == "" || dto.username == "") return; - try { - - const { subscribe } = apiBase; - let baseUrl: string = ''; - - subscribe((value) => { - baseUrl = value; - })(); - const req = await fetch(baseUrl + "/api/auth/login", { - method: "POST", - headers:{ - "Content-Type": "application/json" - }, - credentials: 'include', - body: JSON.stringify(dto) - }); - if (req.ok) { - const token = await req.json(); - sesionStore.set(token); - goto("/") - } else { - callbackfn(); - } - - } catch { - callbackfn(); - console.error("fallo al intentar alcanzar el servidor") - - } +export async function login(e: SubmitEvent, dto: LoginDto, callbackfn: () => void) { + e.preventDefault(); + if (dto.password == '' || dto.username == '') return; + try { + const req = await fetch(get(apiBase) + '/api/auth/login', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + credentials: 'include', + body: JSON.stringify(dto) + }); + if (req.ok) { + const token = await req.json(); + sesionStore.set(token); + goto('/'); + } else { + callbackfn(); + } + } catch { + callbackfn(); + console.error('fallo al intentar alcanzar el servidor'); + } }