mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-20 16:17:32 -03:00
ahora login usa la store directamente
This commit is contained in:
@@ -1,38 +1,30 @@
|
|||||||
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;
|
credentials: 'include',
|
||||||
})();
|
body: JSON.stringify(dto)
|
||||||
const req = await fetch(baseUrl + "/api/auth/login", {
|
});
|
||||||
method: "POST",
|
if (req.ok) {
|
||||||
headers:{
|
const token = await req.json();
|
||||||
"Content-Type": "application/json"
|
sesionStore.set(token);
|
||||||
},
|
goto('/');
|
||||||
credentials: 'include',
|
} else {
|
||||||
body: JSON.stringify(dto)
|
callbackfn();
|
||||||
});
|
}
|
||||||
if (req.ok) {
|
} catch {
|
||||||
const token = await req.json();
|
callbackfn();
|
||||||
sesionStore.set(token);
|
console.error('fallo al intentar alcanzar el servidor');
|
||||||
goto("/")
|
}
|
||||||
} else {
|
|
||||||
callbackfn();
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch {
|
|
||||||
callbackfn();
|
|
||||||
console.error("fallo al intentar alcanzar el servidor")
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user