Skip to content

Commit

Permalink
Fix/bug333 username character validation (#339)
Browse files Browse the repository at this point in the history
* Add error logging to AddUserHandler

* Pass error message from response body to UI

* add invalid username to logs and error message to track what the input was that generated the error
  • Loading branch information
ajlacey authored Sep 21, 2024
1 parent 8a7c5e7 commit 1f5393b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 9 additions & 1 deletion backend/handlers/admin/adduser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package adminhandlers
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"regexp"
Expand All @@ -27,11 +28,14 @@ func AddUserHandler(loadEconConfig setup.EconConfigLoader) func(http.ResponseWri
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Error decoding request body", http.StatusBadRequest)
log.Printf("AddUserHandler: %v", err)
return
}

if match, _ := regexp.MatchString("^[a-zA-Z0-9]+$", req.Username); !match {
http.Error(w, "Username must only contain letters and numbers", http.StatusBadRequest)
err := fmt.Errorf("username %s must only contain letters and numbers", req.Username)
http.Error(w, err.Error(), http.StatusBadRequest)
log.Printf("AddUserHandler: %v", err)
return
}

Expand All @@ -56,17 +60,20 @@ func AddUserHandler(loadEconConfig setup.EconConfigLoader) func(http.ResponseWri
// Check uniqueness of username, displayname, and email
if err := checkUniqueFields(db, &user); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Printf("AddUserHandler: %v", err)
return
}

password := gofakeit.Password(true, true, true, false, false, 12)
if err := user.HashPassword(password); err != nil {
http.Error(w, "Failed to hash password", http.StatusInternalServerError)
log.Printf("AddUserHandler: %v", err)
return
}

if result := db.Create(&user); result.Error != nil {
http.Error(w, "Failed to create user", http.StatusInternalServerError)
log.Printf("AddUserHandler: %v", result.Error)
return
}

Expand All @@ -79,6 +86,7 @@ func AddUserHandler(loadEconConfig setup.EconConfigLoader) func(http.ResponseWri
json.NewEncoder(w).Encode(responseData)
}
}

func checkUniqueFields(db *gorm.DB, user *models.User) error {
// Check for existing users with the same username, display name, email, or API key.
var count int64
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/layouts/admin/AddUser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function AdminAddUser() {
body: JSON.stringify({ username })
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
const errMessage = await response.text()
throw new Error(`HTTP error! Status: ${response.status} Reason: ${errMessage}`);
}
const data = await response.json();
setPassword(data.password);
Expand Down

0 comments on commit 1f5393b

Please sign in to comment.