From 9693e9ee88deff6b69559c4c19bb3f7eb125e56a Mon Sep 17 00:00:00 2001 From: Sisi Date: Sun, 22 Mar 2026 20:06:59 +0100 Subject: [PATCH] add logic for login register --- database.go | 2 +- http.go | 36 +++++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/database.go b/database.go index 264b630..01aa2af 100644 --- a/database.go +++ b/database.go @@ -71,7 +71,7 @@ func DbSaveClientWithoutGroups(ctx context.Context, client *Client) error { return err } -func DbGetClientByName(ctx context.Context, client *Client) error { +func DbSetClientByName(ctx context.Context, client *Client) error { err := dbConn.QueryRow(ctx, ` SELECT name, pass_hash, color_red, color_green, color_blue, created_at FROM clients WHERE name = $1 `, client.Name).Scan(&client.Name, &client.PasswordHash, client.Pronouns, client.Color[0], client.Color[1], client.Color[2], client.CreatedAt) diff --git a/http.go b/http.go index 35b2697..0be6150 100644 --- a/http.go +++ b/http.go @@ -37,8 +37,6 @@ func HttpHandleNewUser(response http.ResponseWriter, request *http.Request) { return } - ctx := request.Context() - username := request.FormValue("username") if len(username) < 4 { http.Error(response, "no or short username", http.StatusBadRequest) @@ -69,6 +67,8 @@ func HttpHandleNewUser(response http.ResponseWriter, request *http.Request) { CreatedAt: time.Now(), } + ctx := request.Context() + err = DbSaveClientWithoutGroups(ctx, newClient) if err != nil { http.Error(response, "name taken", http.StatusInternalServerError) @@ -81,20 +81,42 @@ func HttpHandleLogin(response http.ResponseWriter, request *http.Request) { return } - ctx := request.Context() - username := request.FormValue("username") if len(username) < 4 { http.Error(response, "no or short username", http.StatusBadRequest) return } - password := request.FormValue("password") - if len(password) < 8 { + var client = Client{Name: username} + if len(client.Name) < 8 { http.Error(response, "no or short password", http.StatusBadRequest) return } - _, err := CacheGetClientById() + ctx := request.Context() + err := DbSetClientByName(ctx, &client) + if err != nil { + return + } + + _, err = CacheGetClientById(client.Id) + if err == nil { + otherLoggedIn, err := CacheGetClientById(client.Id) + if err == nil { + otherLoggedIn.WsConn.CloseNow() + } + } + + token, err := TokenCreate(client.Id) + if err != nil { + http.Error(response, "internal server error", http.StatusInternalServerError) + } + response.Write([]byte(token)) +} + +func HttpHandleGroupCreate(response http.ResponseWriter, request *http.Request) { + if !isMethodAllowed(&response, request) { + return + } }