From 88b7e15d23d72508303323e194572466f25e353b Mon Sep 17 00:00:00 2001 From: Felix Schlegel Date: Mon, 9 Dec 2024 12:48:10 +0100 Subject: [PATCH] feat: add sign up api call --- src/lib/controller/backend-controller.ts | 12 +++++++++-- src/routes/(auth)/signup/+page.svelte | 26 ++++++++++++++---------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/lib/controller/backend-controller.ts b/src/lib/controller/backend-controller.ts index c322f62d..a8fbcbfb 100644 --- a/src/lib/controller/backend-controller.ts +++ b/src/lib/controller/backend-controller.ts @@ -64,9 +64,17 @@ export class BackendController implements IBackendController { throw new Error("Method not implemented."); } - // eslint-disable-next-line @typescript-eslint/no-unused-vars async createUser(personalInfo: UserSpec, password: string): Promise { - throw new Error("Method not implemented."); + const payload = { + email: personalInfo.email, + sender: { + firstName: personalInfo.firstName, + lastName: personalInfo.lastName, + password: password, + }, + }; + + return this.client.post("users", payload).then((response) => response.json()); } user(userId: number): IUserController { diff --git a/src/routes/(auth)/signup/+page.svelte b/src/routes/(auth)/signup/+page.svelte index d206f445..da0c0c44 100644 --- a/src/routes/(auth)/signup/+page.svelte +++ b/src/routes/(auth)/signup/+page.svelte @@ -3,6 +3,8 @@ import PasswordInput from "$lib/components/composites/input/PasswordInput.svelte"; import { Button } from "$lib/components/primitives/button/index.js"; import * as Card from "$lib/components/primitives/card/index.js"; + import { BackendController } from "$lib/controller/backend-controller"; + import type { UserSpec } from "$lib/model/backend"; import { Schema } from "$lib/schemas"; let firstNameInput: Input; @@ -10,26 +12,28 @@ let emailInput: Input; let passwordInput: PasswordInput; - function handleSubmit(event: Event) { + async function handleSubmit(event: Event) { event.preventDefault(); - const areInputsValid = - firstNameInput.validate() && - lastNameInput.validate() && - emailInput.validate() && - passwordInput.validate(); - if (!areInputsValid) { + const isFirstNameValid = firstNameInput.validate(); + const isLastNameValid = lastNameInput.validate(); + const isEmailValid = emailInput.validate(); + const isPasswordValid = passwordInput.validate(); + if (!(isFirstNameValid && isLastNameValid && isEmailValid && isPasswordValid)) { return; } - const signUpData = { + const userSpec: UserSpec = { firstName: firstNameInput.getValue(), lastName: lastNameInput.getValue(), email: emailInput.getValue(), - password: passwordInput.getValue(), }; - // TODO: CALL API TO SIGN UP - console.log("sign up with", signUpData); + const user = await BackendController.getInstance().createUser( + userSpec, + passwordInput.getValue(), + ); + // TODO: Login and redirect to the home page + console.log(user); }