-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathapiKeyGenerator.js
More file actions
76 lines (67 loc) · 2.11 KB
/
apiKeyGenerator.js
File metadata and controls
76 lines (67 loc) · 2.11 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
async function callAPIKeyRoute(url = "", data = {}) {
const response = await fetch(url, {
method: "POST",
body: JSON.stringify(data),
});
return response.json();
}
async function buildAPIContent(state) {
callAPIKeyRoute("/api/generate_key", { user: user_id, refresh: state }).then(
(data) => {
let keyText = document.getElementById("apiKeyHolder");
keyText.textContent = "API Key: " + data.key;
}
);
}
async function removeAPIContent() {
callAPIKeyRoute("/api/delete_key", { user: user_id }).then((data) => {
if (data.success) {
let keyText = document.getElementById("apiKeyHolder");
let refreshButton = document.getElementById("apiKeyRefresh");
keyText.insertAdjacentHTML(
"beforebegin",
'<button id="apiKeyGen" type="button" class="btn btn-primary">Generate API Key</button>'
);
keyText.remove();
refreshButton.remove();
buildGenButton();
}
});
}
function activateGenButton(genButton) {
genButton.insertAdjacentHTML(
"afterend",
'<p id="apiKeyHolder" class="m-0"></p><button id="apiKeyRefresh" type="button" class="btn btn-primary">Refresh API Key</button><button id="apiKeyDelete" type="button" class="btn btn-primary">Delete API Key</button>'
);
buildAPIContent(false).then(genButton.remove());
buildRefreshButton();
buildDeleteButton();
}
function buildRefreshButton() {
const refreshButton = document.getElementById("apiKeyRefresh");
refreshButton.addEventListener("click", function () {
buildAPIContent(true);
});
}
function buildDeleteButton() {
const delButton = document.getElementById("apiKeyDelete");
delButton.addEventListener("click", function () {
removeAPIContent();
delButton.remove();
});
}
function buildGenButton() {
const genButton = document.getElementById("apiKeyGen");
genButton.addEventListener("click", function () {
activateGenButton(genButton);
});
}
if (document.getElementById("apiKeyGen")) {
buildGenButton();
}
if (document.getElementById("apiKeyRefresh")) {
buildRefreshButton();
}
if (document.getElementById("apiKeyDelete")) {
buildDeleteButton();
}