From b97eae10a3ba6dc787446fcc876956534d33e4de Mon Sep 17 00:00:00 2001 From: John Costa Date: Sat, 10 May 2025 21:03:32 +0100 Subject: [PATCH] hack: allowing demo@email.com to login straight away for test flight submittion --- backend/main.go | 43 +++++++++++++++++++++++++++++++---- frontend/src/Login.tsx | 12 +++++++++- frontend/src/network/index.ts | 14 +++++++++++- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/backend/main.go b/backend/main.go index 6dcc2c5..31cd2a7 100644 --- a/backend/main.go +++ b/backend/main.go @@ -294,17 +294,17 @@ func main() { w.WriteHeader(http.StatusOK) }) + type CodeReturn struct { + Access string `json:"access"` + Refresh string `json:"refresh"` + } + r.Post("/code", func(w http.ResponseWriter, r *http.Request) { type CodeBody struct { Email string `json:"email"` Code string `json:"code"` } - type CodeReturn struct { - Access string `json:"access"` - Refresh string `json:"refresh"` - } - codeBody := CodeBody{} if err := json.NewDecoder(r.Body).Decode(&codeBody); err != nil { log.Println(err) @@ -358,6 +358,39 @@ func main() { fmt.Fprint(w, string(json)) }) + r.Get("/demo-login", func(w http.ResponseWriter, r *http.Request) { + uuid, err := userModel.GetUserIdFromEmail(r.Context(), "demo@email.com") + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "Something went wrong.") + return + } + + refresh := CreateRefreshToken(uuid) + access := CreateAccessToken(uuid) + + codeReturn := CodeReturn{ + Access: access, + Refresh: refresh, + } + + fmt.Println(codeReturn) + + json, err := json.Marshal(codeReturn) + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "Something went wrong.") + return + } + + w.WriteHeader(http.StatusOK) + w.Header().Add("Content-Type", "application/json") + + fmt.Fprint(w, string(json)) + }) + logWriter := DatabaseWriter{ dbPool: db, } diff --git a/frontend/src/Login.tsx b/frontend/src/Login.tsx index 387cf6f..87ad86d 100644 --- a/frontend/src/Login.tsx +++ b/frontend/src/Login.tsx @@ -3,7 +3,7 @@ import { TextField } from "@kobalte/core/text-field"; import { Navigate } from "@solidjs/router"; import { type Component, Show, createSignal } from "solid-js"; import { isTokenValid } from "./ProtectedRoute"; -import { postCode, postLogin } from "./network"; +import { postCode, postDemoLogin, postLogin } from "./network"; export const Login: Component = () => { let form: HTMLFormElement | undefined; @@ -18,6 +18,16 @@ export const Login: Component = () => { throw new Error("bruh, no email"); } + if (email.toString() === "demo@email.com") { + const { access, refresh } = await postDemoLogin(); + + localStorage.setItem("access", access); + localStorage.setItem("refresh", refresh); + + window.location.href = "/"; + return; + } + if (!submitted()) { await postLogin(email.toString()); setSubmitted(true); diff --git a/frontend/src/network/index.ts b/frontend/src/network/index.ts index cb57522..e80b804 100644 --- a/frontend/src/network/index.ts +++ b/frontend/src/network/index.ts @@ -20,7 +20,7 @@ type BaseRequestParams = Partial<{ method: "GET" | "POST"; }>; -export const base = "http://localhost:3040"; +export const base = "https://haystack.johncosta.tech"; const getBaseRequest = ({ path, body, method }: BaseRequestParams): Request => { return new Request(`${base}/${path}`, { @@ -202,6 +202,18 @@ export const postLogin = async (email: string): Promise => { await fetch(request); }; +export const postDemoLogin = async (): Promise< + InferOutput +> => { + const request = getBaseRequest({ + path: "demo-login", + }); + + const res = await fetch(request).then((res) => res.json()); + + return parse(codeValidator, res); +}; + const codeValidator = strictObject({ access: string(), refresh: string(),