Add saving user, add nonpersistant register login handling

This commit is contained in:
2026-03-17 21:40:48 +01:00
parent e496cb0017
commit d3fc2a65d9
9 changed files with 160 additions and 25 deletions
+61
View File
@@ -16,4 +16,65 @@ func RegisterHandler(response http.ResponseWriter, request *http.Request) {
}
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 {
http.Error(response, "no or short password", http.StatusBadRequest)
return
}
newClient := Client{
Name: username,
PasswordHash: password,
}
err := CreateClient(ctx, &newClient)
if err != nil {
http.Error(response, "taken", http.StatusBadRequest)
return
}
response.Write([]byte("registered"))
}
func LoginHandler(response http.ResponseWriter, request *http.Request) {
if !isMethodAllowed(&response, 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 {
http.Error(response, "no or short password", http.StatusBadRequest)
return
}
id, err := GetIdFromClientName(ctx, username)
if err != nil {
http.Error(response, "bad login", http.StatusBadRequest)
}
err = CheckPassword(ctx, id, password)
if err != nil {
http.Error(response, "bad login", http.StatusBadRequest)
}
token, err := GetToken(id)
if err != nil {
http.Error(response, "Internal error", http.StatusInternalServerError)
}
response.Write([]byte(token))
}