From 3960203d263198dabac06f08a4289046e63b1036 Mon Sep 17 00:00:00 2001 From: John Costa Date: Fri, 11 Apr 2025 11:34:32 +0100 Subject: [PATCH] feat(cookies): using HTTP setCookie instead of manually doing it --- backend/main.go | 23 +++++++++++++++++++++-- frontend/src/Login.tsx | 3 +-- frontend/src/network/index.ts | 14 ++------------ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/backend/main.go b/backend/main.go index 4a0b08c..c2dd9de 100644 --- a/backend/main.go +++ b/backend/main.go @@ -12,6 +12,7 @@ import ( "screenmark/screenmark/.gen/haystack/haystack/model" "screenmark/screenmark/agents/client" "screenmark/screenmark/models" + "time" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" @@ -310,10 +311,28 @@ func main() { return } + accessCookie := http.Cookie{ + Name: "access", + Value: access, + Expires: time.Now().Add(1 * time.Hour), + Secure: false, + HttpOnly: true, + } + + refreshCookie := http.Cookie{ + Name: "refresh", + Value: access, + Expires: time.Now().Add(24 * 30 * time.Hour), + Secure: false, + HttpOnly: true, + Path: "/refresh", + } + + http.SetCookie(w, &accessCookie) // TODO + http.SetCookie(w, &refreshCookie) // TODO + w.WriteHeader(http.StatusOK) w.Header().Add("Content-Type", "application/json") - - fmt.Fprint(w, string(json)) }) log.Println("Listening and serving on port 3040.") diff --git a/frontend/src/Login.tsx b/frontend/src/Login.tsx index 5b435eb..8cb802f 100644 --- a/frontend/src/Login.tsx +++ b/frontend/src/Login.tsx @@ -25,8 +25,7 @@ export const Login: Component = () => { throw new Error("bruh, no code"); } - const res = await postCode(email.toString(), code.toString()); - console.log(res); + await postCode(email.toString(), code.toString()); } }; diff --git a/frontend/src/network/index.ts b/frontend/src/network/index.ts index 7e4084b..8642ee6 100644 --- a/frontend/src/network/index.ts +++ b/frontend/src/network/index.ts @@ -124,22 +124,12 @@ export const postLogin = async (email: string): Promise => { await fetch(request); }; -const codeValidator = strictObject({ - access: string(), - refresh: string(), -}); - -export const postCode = async ( - email: string, - code: string, -): Promise> => { +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); + await fetch(request).then((res) => res.json()); };