diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..98e6ef6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.db diff --git a/internal/server/admin.go b/internal/server/admin.go index 1c989ec..88e2d9e 100644 --- a/internal/server/admin.go +++ b/internal/server/admin.go @@ -553,10 +553,50 @@ func (s *Server) handleAdminGetAdmins(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(result) } +// handleAdminChangePassword handles admin password change +func (s *Server) handleAdminChangePassword(w http.ResponseWriter, r *http.Request) { + admin, err := s.authenticateAdmin(r) + if err != nil || admin == nil { + http.Error(w, "Unauthorized", http.StatusUnauthorized) + return + } + + if r.Method != http.MethodPost { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + var req struct { + ID int `json:"id"` + Password string `json:"password"` + } + + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + http.Error(w, "Invalid request body", http.StatusBadRequest) + return + } + + if req.Password == "" { + http.Error(w, "Password required", http.StatusBadRequest) + return + } + + if err := s.db.UpdateAdminPassword(req.ID, hashAPIKey(req.Password)); err != nil { + http.Error(w, "Failed to update password", http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]interface{}{ + "success": true, + "message": "Password changed successfully", + }) +} + // handleAdminUI serves the admin panel UI func (s *Server) handleAdminUI(w http.ResponseWriter, r *http.Request) { // Serve the embedded admin UI HTML - w.Header().Set("Content-Type", "text/html") + w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte(adminPanelHTML)) } @@ -586,9 +626,12 @@ const adminPanelHTML = ` .btn-danger:hover { background: #c0392b; } .btn-success { background: #27ae60; } .btn-success:hover { background: #229954; } - .btn-sm { padding: 6px 12px; font-size: 12px; } + .btn-warning { background: #f39c12; } + .btn-warning:hover { background: #d68910; } + .btn-sm { padding: 6px 12px; font-size: 12px; margin: 2px; } .error { color: #e74c3c; margin-bottom: 20px; text-align: center; } - .hidden { display: none; } + .success { color: #27ae60; margin-bottom: 20px; text-align: center; } + .hidden { display: none !important; } .card { background: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); margin-bottom: 20px; } .card-header { padding: 20px; border-bottom: 1px solid #eee; display: flex; justify-content: space-between; align-items: center; } .card-header h3 { color: #2c3e50; } @@ -605,11 +648,12 @@ const adminPanelHTML = ` .stat-card { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); text-align: center; } .stat-card h4 { color: #666; font-size: 14px; margin-bottom: 10px; } .stat-card .value { font-size: 32px; font-weight: bold; color: #2c3e50; } - .modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 1000; } - .modal-content { background: white; padding: 30px; border-radius: 8px; max-width: 500px; width: 90%; } + .modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 1000; } + .modal-content { background: white; padding: 30px; border-radius: 8px; max-width: 500px; width: 90%; max-height: 90vh; overflow-y: auto; } .modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; } .modal-header h3 { color: #2c3e50; } - .close-btn { background: none; border: none; font-size: 24px; cursor: pointer; color: #666; } + .close-btn { background: none; border: none; font-size: 28px; cursor: pointer; color: #666; line-height: 1; } + .close-btn:hover { color: #333; } .tabs { display: flex; gap: 10px; margin-bottom: 20px; } .tab { padding: 10px 20px; background: white; border: none; border-radius: 4px; cursor: pointer; color: #666; } .tab.active { background: #3498db; color: white; } @@ -619,26 +663,27 @@ const adminPanelHTML = `
-

🔐 Admin Login

+

Admin Login

- +
- +
+

Default: admin / admin123