ahora login usa la store directamente

This commit is contained in:
2026-01-05 16:21:54 -03:00
parent 7f68f61ce2
commit 40a8c07b0d
+13 -21
View File
@@ -1,23 +1,17 @@
import { apiBase } from "@/stores/url"; import { apiBase } from '@/stores/url';
import type { LoginDto } from "../../types"; import type { LoginDto } from '../../types';
import { sesionStore } from "@/stores/usuario"; import { sesionStore } from '@/stores/usuario';
import { goto } from "$app/navigation"; import { goto } from '$app/navigation';
import { get } from 'svelte/store';
export async function login(e:SubmitEvent, dto: LoginDto, callbackfn:()=>void){ export async function login(e: SubmitEvent, dto: LoginDto, callbackfn: () => void) {
e.preventDefault(); e.preventDefault();
if (dto.password == "" || dto.username == "") return; if (dto.password == '' || dto.username == '') return;
try { try {
const req = await fetch(get(apiBase) + '/api/auth/login', {
const { subscribe } = apiBase; method: 'POST',
let baseUrl: string = ''; headers: {
'Content-Type': 'application/json'
subscribe((value) => {
baseUrl = value;
})();
const req = await fetch(baseUrl + "/api/auth/login", {
method: "POST",
headers:{
"Content-Type": "application/json"
}, },
credentials: 'include', credentials: 'include',
body: JSON.stringify(dto) body: JSON.stringify(dto)
@@ -25,14 +19,12 @@ export async function login(e:SubmitEvent, dto: LoginDto, callbackfn:()=>void){
if (req.ok) { if (req.ok) {
const token = await req.json(); const token = await req.json();
sesionStore.set(token); sesionStore.set(token);
goto("/") goto('/');
} else { } else {
callbackfn(); callbackfn();
} }
} catch { } catch {
callbackfn(); callbackfn();
console.error("fallo al intentar alcanzar el servidor") console.error('fallo al intentar alcanzar el servidor');
} }
} }