Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/bug333 username character validation #339

Merged
merged 3 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading