diff --git a/playbeat/src/pages/register.astro b/playbeat/src/pages/register.astro index 043f49a..59af510 100644 --- a/playbeat/src/pages/register.astro +++ b/playbeat/src/pages/register.astro @@ -1,25 +1,10 @@ --- import NormalLayout from '../layouts/NormalLayout.astro' -const errors = { username: "", email: "", password: "" }; +import {registerUser} from '@/utils/register.ts' +const errors = { username: "", email: "", password: "", peticion:"" }; const values = { username: "", email: "", password: "", password2:"" }; -async function registerUser(data: { name: FormDataEntryValue | null; email: FormDataEntryValue | null; password: FormDataEntryValue | null; }) { - try { - console.log(data); - const response = await fetch('', { - method: 'POST', - body: JSON.stringify(data) - }); - - if (!response.ok) { - throw new Error('Error al registrar usuario'); - } - return response.json(); - } catch (error) { - throw new Error((error as Error).message); - } -} @@ -59,10 +44,18 @@ if (Astro.request.method === "POST") { } // Do something with the data const hasErrors = Object.values(errors).some(msg => msg) - if (!hasErrors) { - await registerUser({name, email, password}); - console.log("Usuario registrado"); + if (!hasErrors) { + const response = await registerUser({name, email, password}); + console.log(`Response: ${response}`) + if (response && response.ok == true) { + console.log("Registrado"); + const data = await response.json(); + const token = data.token; + Astro.cookies.set("token", token); return Astro.redirect("/"); + } else { + errors.peticion = "Error al registrar, intentelo de nuevo"; + } } } catch (error) { if (error instanceof Error) { @@ -102,6 +95,8 @@ if (Astro.request.method === "POST") {

{errors.username}

{errors.email}

{errors.password}

+

{errors.peticion}

+ } diff --git a/playbeat/src/utils/register.ts b/playbeat/src/utils/register.ts new file mode 100644 index 0000000..a76ecc0 --- /dev/null +++ b/playbeat/src/utils/register.ts @@ -0,0 +1,20 @@ + +import {Global} from "@/globalState/globalUrl.js" + + +async function registerUser(data: { name: FormDataEntryValue | null; email: FormDataEntryValue | null; password: FormDataEntryValue | null; }) { + try { + console.log(data); + const response = await fetch(Global.url+'auth/signup', { + method: 'POST', + body: JSON.stringify(data) + }); + console.log(response); + return response; + } catch (error) { + throw new Error((error as Error).message); + } + } + + + export {registerUser} \ No newline at end of file