diff --git a/internal/handlers/auth.go b/internal/handlers/auth.go index 0a1e464..bdac3ba 100644 --- a/internal/handlers/auth.go +++ b/internal/handlers/auth.go @@ -846,6 +846,8 @@ func (h *AuthHandler) DeleteUser(c echo.Context) error { // Count admin users and identify the user to be deleted adminCount := 0 targetUserRole := "" + userFound := false + for _, user := range users { if user.Role == "admin" { adminCount++ @@ -853,9 +855,19 @@ func (h *AuthHandler) DeleteUser(c echo.Context) error { // Find target user details if user.ID.Bytes == targetUserUUID.Bytes { targetUserRole = user.Role + userFound = true + // Can't break here - still need to count all admins for last admin check } } + // Check if target user exists in the database + if !userFound { + if c.Request().Header.Get("HX-Request") == "true" { + return c.HTML(http.StatusNotFound, `
User not found
`) + } + return c.JSON(http.StatusNotFound, map[string]string{"error": "user not found"}) + } + // Prevent deletion if target user is admin and this is the last admin if targetUserRole == "admin" && adminCount == 1 { if c.Request().Header.Get("HX-Request") == "true" {