From a96aa05833ca92edffc9ce4e50d538c114e4f760 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 10 Jun 2026 13:24:46 +0000 Subject: [PATCH 1/2] Initial plan From 34d15ace9b818add6b6b8e2bd860eb335f4abae0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 10 Jun 2026 13:59:14 +0000 Subject: [PATCH 2/2] Fix user management page: handle null responses, force cache refresh, fix register handler --- api/auth.go | 41 +++++++++++++--------------------------- database/user.go | 2 +- static/js/admin/users.js | 3 ++- static/sw.js | 2 +- 4 files changed, 17 insertions(+), 31 deletions(-) diff --git a/api/auth.go b/api/auth.go index 9d4d2aa..8a2ed6a 100644 --- a/api/auth.go +++ b/api/auth.go @@ -80,7 +80,9 @@ func (h *Handler) RegisterHandler(w http.ResponseWriter, r *http.Request) { return } - // Create user with hashed password + // Create user with hashed password. + // Category defaults to "java" since the public registration form does not + // collect a category; an admin can change it via the User Management page. user := &models.User{ Username: req.Username, Email: req.Email, @@ -88,37 +90,20 @@ func (h *Handler) RegisterHandler(w http.ResponseWriter, r *http.Request) { IsAdmin: false, IsActive: true, IsActiveSet: true, + Category: "java", TotalPoints: 0, CreatedAt: time.Now(), } - // Try to create the user in SQLite database - if sqlDB, ok := h.db.(*database.SQLiteDB); ok { - result, err := sqlDB.DB().Exec( - "INSERT INTO users (username, email, password, is_admin, is_active, total_points, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)", - user.Username, user.Email, user.Password, user.IsAdmin, user.IsActive, user.TotalPoints, user.CreatedAt, - ) - if err != nil { - h.logger.WithContext(r.Context()).WithFields(map[string]interface{}{ - "username": user.Username, - "email": user.Email, - "error": err.Error(), - }).Error("Failed to create user in database") - http.Error(w, "Username or email already exists", http.StatusConflict) - return - } - id, _ := result.LastInsertId() - user.ID = int(id) - } else { - // Fallback for in-memory DB - if err := h.db.CreateUser(user); err != nil { - h.logger.WithContext(r.Context()).WithFields(map[string]interface{}{ - "username": user.Username, - "error": err.Error(), - }).Error("Failed to create user in in-memory database") - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + // Create the user in the database using the shared interface + if err := h.db.CreateUser(user); err != nil { + h.logger.WithContext(r.Context()).WithFields(map[string]interface{}{ + "username": user.Username, + "email": user.Email, + "error": err.Error(), + }).Error("Failed to create user in database") + http.Error(w, "Username or email already exists", http.StatusConflict) + return } // Don't send password back diff --git a/database/user.go b/database/user.go index 637bf17..0e63c30 100644 --- a/database/user.go +++ b/database/user.go @@ -87,7 +87,7 @@ func (db *SQLiteDB) ListUsers() ([]*models.User, error) { } defer rows.Close() - var users []*models.User + users := make([]*models.User, 0) for rows.Next() { user := &models.User{} if err := rows.Scan(&user.ID, &user.Username, &user.Email, &user.IsAdmin, &user.IsActive, &user.Category, &user.TotalPoints, &user.CreatedAt); err != nil { diff --git a/static/js/admin/users.js b/static/js/admin/users.js index 596ecf7..5bca70f 100644 --- a/static/js/admin/users.js +++ b/static/js/admin/users.js @@ -16,7 +16,8 @@ export async function loadUsers() { if (!adminsTable || !activeStudentsTable || !inactiveStudentsTable) return; try { console.log('Loading users with X-User-ID:', currentUser.id.toString()); - const users = await API.get('/admin/users'); + const rawUsers = await API.get('/admin/users'); + const users = Array.isArray(rawUsers) ? rawUsers : []; console.log('Loaded users:', users.length, 'users'); adminsTable.innerHTML = ''; activeStudentsTable.innerHTML = ''; diff --git a/static/sw.js b/static/sw.js index 363e7c6..f705d8f 100644 --- a/static/sw.js +++ b/static/sw.js @@ -1,4 +1,4 @@ -const CACHE_NAME = 'athena-v9'; +const CACHE_NAME = 'athena-v10'; const SHELL_FILES = [ '/index.html',