From 87788421cc1c238ddc2e8c6eaf04c6955aa74e5e Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Mon, 10 Jun 2024 18:44:04 +0100 Subject: [PATCH] Login uses standard form instead of passing a dumb JSON --- src/assets/htmx/login.html | 112 ++++++++++++++++-------------- src/controllers/authentication.go | 37 ++-------- 2 files changed, 65 insertions(+), 84 deletions(-) diff --git a/src/assets/htmx/login.html b/src/assets/htmx/login.html index 763136a..bfc86de 100644 --- a/src/assets/htmx/login.html +++ b/src/assets/htmx/login.html @@ -7,7 +7,11 @@



- + @@ -15,66 +19,66 @@ {{ if .script }} {{ end }} \ No newline at end of file diff --git a/src/controllers/authentication.go b/src/controllers/authentication.go index 375ce22..774ddb5 100644 --- a/src/controllers/authentication.go +++ b/src/controllers/authentication.go @@ -1,10 +1,8 @@ package controllers import ( - "encoding/json" "errors" "fmt" - "io" "log" "net/http" "thermalFax/models" @@ -75,22 +73,14 @@ func createSessionCookie(token string) *http.Cookie { func Authenticate(w http.ResponseWriter, r *http.Request) { // first we have to verify if this request brings any login data - - data, err := io.ReadAll(r.Body) - if err != nil { - log.Printf("Error: %s", err) - return - } - - var loginForm models.LoginForm - err = json.Unmarshal(data, &loginForm) - if err != nil { - log.Printf("Error: %s", err) - return + r.ParseForm() + loginForm := models.LoginForm{ + Username: r.FormValue("login-user"), + Password: r.FormValue("login-pass"), } if !models.ValidateCredentials(&loginForm) { - log.Printf("Invalid credentials.") + log.Println("| Invalid credentials.") w.Header().Set("Content-Type", "text/json") w.Write([]byte("{\"state\":\"wrong credentials\"}")) return @@ -119,14 +109,7 @@ func Authenticate(w http.ResponseWriter, r *http.Request) { } http.SetCookie(w, &cookie) - // w.Header().Set("HX-Redirect", localUserSession.Wants) - w.Header().Set("Content-Type", "text/json") - w.Write([]byte(` - { - "state":"success", - "page":"` + localUserSession.Wants + `" - } - `)) + w.Header().Set("HX-Redirect", localUserSession.Wants) } else { // Create a new session authCookie, err := createAuthCookie(loginForm) @@ -155,13 +138,7 @@ func Authenticate(w http.ResponseWriter, r *http.Request) { log.Printf(`| {"user": %s, "time": %d}`, loginForm.Username, time.Now().Unix()) - w.Header().Set("Content-Type", "text/json") - w.Write([]byte(` - { - "state":"success", - "page":"` + "/editor" + `" - } - `)) + w.Header().Add("HX-Redirect", "/editor") } }