Login uses standard form instead of passing a dumb JSON

This commit is contained in:
2024-06-10 18:44:04 +01:00
parent 975654539f
commit 87788421cc
2 changed files with 65 additions and 84 deletions
+7 -30
View File
@@ -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")
}
}