File size: 1,708 Bytes
c140e55
 
 
 
 
 
3d8c947
 
 
 
c140e55
 
 
 
 
 
3d8c947
 
c140e55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d8c947
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
document.addEventListener("DOMContentLoaded", function() {
  // Prompt for admin API key (used for all admin requests)
  const adminApiKey = prompt("Enter your admin API key:");

  // Add Credit Form submission
  document.getElementById("addCreditForm").addEventListener("submit", async function(e) {
    e.preventDefault();
    const username = document.getElementById("username").value;
    const credits = parseInt(document.getElementById("credits").value, 10);
    const response = await fetch("/admin/add_credit", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "X-API-Key": adminApiKey
      },
      body: JSON.stringify({ username, credits })
    });
    const result = await response.json();
    alert(JSON.stringify(result, null, 2));
  });

  // Deactivate API Key Form submission
  document.getElementById("deactivateKeyForm").addEventListener("submit", async function(e) {
    e.preventDefault();
    const key = document.getElementById("apiKeyToDeactivate").value;
    const response = await fetch("/admin/deactivate_key", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "X-API-Key": adminApiKey
      },
      body: JSON.stringify({ key })
    });
    const result = await response.json();
    alert(JSON.stringify(result, null, 2));
  });

  // Load Users Button
  document.getElementById("loadUsers").addEventListener("click", async function() {
    const response = await fetch("/admin/users", {
      headers: {
        "X-API-Key": adminApiKey
      }
    });
    const result = await response.json();
    document.getElementById("usersList").innerText = JSON.stringify(result, null, 2);
  });
});