Skip to content
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
41 changes: 13 additions & 28 deletions api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,45 +80,30 @@ 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,
Password: hashedPassword,
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
Expand Down
2 changes: 1 addition & 1 deletion database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion static/js/admin/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
2 changes: 1 addition & 1 deletion static/sw.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CACHE_NAME = 'athena-v9';
const CACHE_NAME = 'athena-v10';

const SHELL_FILES = [
'/index.html',
Expand Down
Loading