From 2302ba5eeb32346a5a2c8f183d5ad9c48f734dae Mon Sep 17 00:00:00 2001 From: John Costa Date: Fri, 11 Apr 2025 11:08:33 +0100 Subject: [PATCH] feat: minimal UI for login --- backend/main.go | 4 +++ frontend/src/App.tsx | 2 ++ frontend/src/Login.tsx | 48 +++++++++++++++++++++++++++++++++++ frontend/src/index.tsx | 2 ++ frontend/src/network/index.ts | 30 ++++++++++++++++++++++ 5 files changed, 86 insertions(+) create mode 100644 frontend/src/Login.tsx diff --git a/backend/main.go b/backend/main.go index cedf257..4a0b08c 100644 --- a/backend/main.go +++ b/backend/main.go @@ -62,6 +62,10 @@ func main() { }) }) + r.Options("/*", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + r.Get("/image", func(w http.ResponseWriter, r *http.Request) { token := r.Header.Get("Authorization")[7:] diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index ee2cc90..4ab8981 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -7,6 +7,7 @@ import { SearchCardLocation } from "./components/search-card/SearchCardLocation" import { UserImage, getUserImages } from "./network"; import { getCardSize } from "./utils/getCardSize"; import { SearchCardNote } from "./components/search-card/SearchCardNote"; +import { A } from "@solidjs/router"; const getCardComponent = (item: UserImage) => { switch (item.type) { @@ -99,6 +100,7 @@ function App() { return ( <>
+ login
diff --git a/frontend/src/Login.tsx b/frontend/src/Login.tsx new file mode 100644 index 0000000..5b435eb --- /dev/null +++ b/frontend/src/Login.tsx @@ -0,0 +1,48 @@ +import { Button } from "@kobalte/core/button"; +import { TextField } from "@kobalte/core/text-field"; +import { createSignal, Show, type Component } from "solid-js"; +import { postCode, postLogin } from "./network"; + +export const Login: Component = () => { + let form: HTMLFormElement | undefined; + + const [submitted, setSubmitted] = createSignal(false); + + const onSubmit: HTMLFormElement["onsubmit"] = async (e) => { + e.preventDefault(); + const formData = new FormData(form); + const email = formData.get("email"); + if (email == null) { + throw new Error("bruh, no email"); + } + + if (!submitted()) { + await postLogin(email.toString()); + setSubmitted(true); + } else { + const code = formData.get("code"); + if (code == null) { + throw new Error("bruh, no code"); + } + + const res = await postCode(email.toString(), code.toString()); + console.log(res); + } + }; + + return ( +
+ + Email + + + + + Code + + + + +
+ ); +}; diff --git a/frontend/src/index.tsx b/frontend/src/index.tsx index c5cd292..61dd91b 100644 --- a/frontend/src/index.tsx +++ b/frontend/src/index.tsx @@ -4,11 +4,13 @@ import App from "./App"; import "./index.css"; import { Route, Router } from "@solidjs/router"; import { ImagePage } from "./ImagePage"; +import { Login } from "./Login"; render( () => ( + ), diff --git a/frontend/src/network/index.ts b/frontend/src/network/index.ts index 70a96f5..7e4084b 100644 --- a/frontend/src/network/index.ts +++ b/frontend/src/network/index.ts @@ -113,3 +113,33 @@ export const getUserImages = async (): Promise => { return parse(getUserImagesResponseValidator, res); }; + +export const postLogin = async (email: string): Promise => { + const request = getBaseRequest({ + path: "login", + body: JSON.stringify({ email }), + method: "POST", + }); + + await fetch(request); +}; + +const codeValidator = strictObject({ + access: string(), + refresh: string(), +}); + +export const postCode = async ( + email: string, + code: string, +): Promise> => { + const request = getBaseRequest({ + path: "code", + body: JSON.stringify({ email, code }), + method: "POST", + }); + + const res = await fetch(request).then((res) => res.json()); + + return parse(codeValidator, res); +};