From ccebc7934c613f04ad87c2ef5435742203dd7b43 Mon Sep 17 00:00:00 2001 From: fede Date: Fri, 14 Nov 2025 18:25:57 -0300 Subject: [PATCH] codigo de login front --- src/lib/components/signup-form.svelte | 32 ++++++++++----- .../ui/login-form/login-form.svelte | 14 +++++-- src/lib/hooks/login.ts | 37 ++++++++++++++++++ src/lib/hooks/register.ts | 39 +++++++++++++++++++ src/lib/stores/usuario.ts | 14 +++---- src/routes/login/+page.svelte | 36 +++++++++++++++-- src/types.d.ts | 16 ++++++++ 7 files changed, 163 insertions(+), 25 deletions(-) create mode 100644 src/lib/hooks/login.ts create mode 100644 src/lib/hooks/register.ts diff --git a/src/lib/components/signup-form.svelte b/src/lib/components/signup-form.svelte index 84b91c9..4db6eaf 100644 --- a/src/lib/components/signup-form.svelte +++ b/src/lib/components/signup-form.svelte @@ -1,32 +1,44 @@ - + Registrarse
-
+ register(e, dto, setAlert)}> - Nombre Completo - + Nombre de Usuario + + + + Nombre Visible + + + Email - + Contraseña - + Debe de tener por lo menos 8 caracteres. diff --git a/src/lib/components/ui/login-form/login-form.svelte b/src/lib/components/ui/login-form/login-form.svelte index 0213693..b866e05 100644 --- a/src/lib/components/ui/login-form/login-form.svelte +++ b/src/lib/components/ui/login-form/login-form.svelte @@ -3,7 +3,13 @@ import * as Card from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { FieldGroup, Field, FieldLabel, FieldDescription } from '@/components/ui/field'; - const id = $props.id(); + import type { LoginDto } from '../../../../types'; + import { login } from '@/hooks/login'; + let {id, showAlert = $bindable() } = $props(); + + let dto: LoginDto = $state({password: "", username: ""}); + + const setAlert = () => showAlert = true; @@ -12,11 +18,11 @@ ingrese su usuario para logearse en la cuenta - + Usuario - +
@@ -25,7 +31,7 @@ Te Olvidaste la contraseña?
- +
diff --git a/src/lib/hooks/login.ts b/src/lib/hooks/login.ts new file mode 100644 index 0000000..6493096 --- /dev/null +++ b/src/lib/hooks/login.ts @@ -0,0 +1,37 @@ +import { apiBase } from "@/stores/url"; +import type { LoginDto } from "../../types"; +import { sesionStore } from "@/stores/usuario"; +import { goto } from "$app/navigation"; + +export async function login(e:FormDataEvent,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" + }, + body: JSON.stringify(dto) + }); + if (req.ok) { + const token = await req.json(); + sesionStore.set({ accessToken: token }) + goto("/") + } else { + callbackfn(); + } + + } catch { + callbackfn(); + console.error("fallo al intentar alcanzar el servidor") + + } +} diff --git a/src/lib/hooks/register.ts b/src/lib/hooks/register.ts new file mode 100644 index 0000000..6c44ff1 --- /dev/null +++ b/src/lib/hooks/register.ts @@ -0,0 +1,39 @@ +import { apiBase } from "@/stores/url"; +import { sesionStore } from "@/stores/usuario"; +import { goto } from "$app/navigation"; +import type { RegisterDto } from "../../types"; + +export async function register(e:FormDataEvent,dto: RegisterDto, callbackfn:()=>void){ + e.preventDefault(); + if (dto.password == "" || dto.username == "" || + !dto.email?.includes("@") || dto.displayName=="") return; + try { + + const { subscribe } = apiBase; + let baseUrl: string = ''; + + subscribe((value) => { + baseUrl = value; + })(); + const req = await fetch(baseUrl + "/api/auth/register", { + method: "POST", + headers:{ + "Content-Type": "application/json" + }, + body: JSON.stringify(dto) + }); + if (req.ok) { + const data= await req.json(); + goto("/login", { state: { + message: data.message, + }}); + } else { + callbackfn(); + } + + } catch { + callbackfn(); + console.error("fallo al intentar alcanzar el servidor") + + } +} diff --git a/src/lib/stores/usuario.ts b/src/lib/stores/usuario.ts index a337b11..649b18b 100644 --- a/src/lib/stores/usuario.ts +++ b/src/lib/stores/usuario.ts @@ -1,11 +1,11 @@ import { writable } from 'svelte/store'; -import type { User } from '../../types'; +import type { Sesion } from '../../types'; -export const currentUser = writable(null); +export const currentSesion = writable(null); -export const userStore = { - subscribe: currentUser.subscribe, - set: currentUser.set, - update: currentUser.update, - reset: () => currentUser.set(null) +export const sesionStore = { + subscribe: currentSesion.subscribe, + set: currentSesion.set, + update: currentSesion.update, + reset: () => currentSesion.set(null) }; diff --git a/src/routes/login/+page.svelte b/src/routes/login/+page.svelte index e99aed3..e968672 100644 --- a/src/routes/login/+page.svelte +++ b/src/routes/login/+page.svelte @@ -1,9 +1,37 @@ - + +
- + + {#if showAlert} +
+ + + No se pudo iniciar sesion + + Revise su usuario o contraseña + + +
+ {/if}
diff --git a/src/types.d.ts b/src/types.d.ts index 4cb424b..d617b8e 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -25,3 +25,19 @@ export interface User { followingCount: number; refreshTokens: RefreshToken[]; } + +export interface Sesion { + accessToken:string?; +} + +export interface LoginDto { + username: string?; + password: string?; +} + +export interface RegisterDto { + username: string?; + email: string?; + password: string?; + displayName: string?; +}