add logic for login register

This commit is contained in:
2026-03-22 20:06:59 +01:00
parent d756023952
commit 9693e9ee88
2 changed files with 30 additions and 8 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ func DbSaveClientWithoutGroups(ctx context.Context, client *Client) error {
return err return err
} }
func DbGetClientByName(ctx context.Context, client *Client) error { func DbSetClientByName(ctx context.Context, client *Client) error {
err := dbConn.QueryRow(ctx, ` err := dbConn.QueryRow(ctx, `
SELECT name, pass_hash, color_red, color_green, color_blue, created_at FROM clients WHERE name = $1 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) `, client.Name).Scan(&client.Name, &client.PasswordHash, client.Pronouns, client.Color[0], client.Color[1], client.Color[2], client.CreatedAt)
+29 -7
View File
@@ -37,8 +37,6 @@ func HttpHandleNewUser(response http.ResponseWriter, request *http.Request) {
return return
} }
ctx := request.Context()
username := request.FormValue("username") username := request.FormValue("username")
if len(username) < 4 { if len(username) < 4 {
http.Error(response, "no or short username", http.StatusBadRequest) http.Error(response, "no or short username", http.StatusBadRequest)
@@ -69,6 +67,8 @@ func HttpHandleNewUser(response http.ResponseWriter, request *http.Request) {
CreatedAt: time.Now(), CreatedAt: time.Now(),
} }
ctx := request.Context()
err = DbSaveClientWithoutGroups(ctx, newClient) err = DbSaveClientWithoutGroups(ctx, newClient)
if err != nil { if err != nil {
http.Error(response, "name taken", http.StatusInternalServerError) http.Error(response, "name taken", http.StatusInternalServerError)
@@ -81,20 +81,42 @@ func HttpHandleLogin(response http.ResponseWriter, request *http.Request) {
return return
} }
ctx := request.Context()
username := request.FormValue("username") username := request.FormValue("username")
if len(username) < 4 { if len(username) < 4 {
http.Error(response, "no or short username", http.StatusBadRequest) http.Error(response, "no or short username", http.StatusBadRequest)
return return
} }
password := request.FormValue("password") var client = Client{Name: username}
if len(password) < 8 { if len(client.Name) < 8 {
http.Error(response, "no or short password", http.StatusBadRequest) http.Error(response, "no or short password", http.StatusBadRequest)
return 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
}
} }