2023-05-05 15:16:54 +02:00
|
|
|
// Link overview
|
|
|
|
|
2024-05-06 13:11:46 +02:00
|
|
|
async function HandleLinkIndexDelete(id) {
|
2023-05-05 15:16:54 +02:00
|
|
|
let response = await fetch("/api/v1/links/" + id, {
|
|
|
|
credentials: "include",
|
|
|
|
mode: "same-origin",
|
|
|
|
method: "DELETE"
|
|
|
|
});
|
|
|
|
if (!response.ok) {
|
|
|
|
console.log("error deleting " + id + ": " + response.statusText)
|
|
|
|
}
|
|
|
|
document.location = "/admin/"
|
|
|
|
}
|
|
|
|
|
|
|
|
async function HandleLinkIndexCopy(id) {
|
|
|
|
let host = window.location.protocol + "//" + window.location.host;
|
|
|
|
await navigator.clipboard.writeText(host + "/" + id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Link Add
|
|
|
|
|
|
|
|
async function HandleLinkAddSubmit() {
|
2023-06-16 12:46:08 +02:00
|
|
|
await LinkAction("add")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link edit
|
|
|
|
|
|
|
|
async function HandleLinkEditSubmit() {
|
|
|
|
await LinkAction("edit")
|
|
|
|
}
|
|
|
|
|
2024-05-06 13:11:46 +02:00
|
|
|
async function LinkAction(action) {
|
2023-05-05 15:16:54 +02:00
|
|
|
document.getElementById("submit").active = false
|
|
|
|
let slug = document.getElementById("linkname").value
|
|
|
|
let url = document.getElementById("link").value
|
|
|
|
let description = document.getElementById("description").value
|
2023-06-16 12:46:08 +02:00
|
|
|
|
|
|
|
let method, endpoint = ""
|
|
|
|
let body;
|
2024-05-06 13:11:46 +02:00
|
|
|
switch (action) {
|
2023-06-16 12:46:08 +02:00
|
|
|
case "add":
|
|
|
|
method = "POST"
|
|
|
|
endpoint = "/api/v1/links/"
|
|
|
|
body = {
|
2024-05-06 13:11:46 +02:00
|
|
|
"id": slug,
|
|
|
|
"url": url,
|
|
|
|
"description": description
|
2023-06-16 12:46:08 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "edit":
|
|
|
|
method = "PUT"
|
|
|
|
endpoint = "/api/v1/links/" + slug
|
|
|
|
body = {
|
2024-05-06 13:11:46 +02:00
|
|
|
"url": url,
|
|
|
|
"description": description
|
2023-06-16 12:46:08 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return
|
2023-05-05 15:16:54 +02:00
|
|
|
}
|
|
|
|
|
2023-06-16 12:46:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
let response = await fetch(endpoint, {
|
2023-05-05 15:16:54 +02:00
|
|
|
credentials: "include",
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
mode: "same-origin",
|
2023-06-16 12:46:08 +02:00
|
|
|
method: method
|
2023-05-05 15:16:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
document.getElementById("dialog-heading").textContent = "Error"
|
|
|
|
document.getElementById("dialog-text").textContent = "The following error occured during the request: " + response.statusText
|
|
|
|
document.getElementById('dialog-info').showModal()
|
|
|
|
|
|
|
|
document.getElementById("submit").active = true
|
|
|
|
}
|
|
|
|
document.location = "/admin/"
|
|
|
|
}
|
|
|
|
|
|
|
|
async function HandleLinkFieldChange() {
|
|
|
|
console.log("HandleChange")
|
|
|
|
let buttonactive = true
|
2024-05-06 13:11:46 +02:00
|
|
|
if (document.getElementById("link").value === "") {
|
2023-05-05 15:16:54 +02:00
|
|
|
buttonactive = false
|
|
|
|
}
|
|
|
|
document.getElementById("submit").active = buttonactive
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApiKey Add
|
|
|
|
|
|
|
|
async function HandleApiKeyNewSubmit() {
|
|
|
|
let button = document.getElementById("submit")
|
|
|
|
let description = document.getElementById("description")
|
|
|
|
button.active = false
|
|
|
|
button.setAttribute("aria-busy", "true")
|
|
|
|
|
|
|
|
let body = {
|
2024-05-06 13:11:46 +02:00
|
|
|
"description": description
|
2023-05-05 15:16:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let response = await fetch("/api/v1/apikeys", {
|
|
|
|
credentials: "include",
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
mode: "same-origin",
|
|
|
|
method: "POST"
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
let data = await response.json()
|
|
|
|
document.getElementById("dialog-heading").textContent = "New API-Key"
|
|
|
|
document.getElementById("dialog-text").textContent = "Here is your new API Key. Copy it NOW, it won't be shown again."
|
|
|
|
document.getElementById("dialog-apikey").textContent = data.key
|
|
|
|
document.getElementById('dialog-info').showModal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-04 17:06:01 +02:00
|
|
|
async function HandleMFASetupTokenSubmit() {
|
|
|
|
let token = document.getElementById("token").value
|
2024-05-10 11:44:42 +02:00
|
|
|
let button = document.getElementById("button-submit")
|
|
|
|
|
|
|
|
button.disabled = true
|
|
|
|
button.setAttribute('aria-busy', 'true')
|
2024-05-04 17:06:01 +02:00
|
|
|
|
|
|
|
let body = {
|
2024-05-06 13:11:46 +02:00
|
|
|
"token": token
|
2024-05-04 17:06:01 +02:00
|
|
|
}
|
|
|
|
|
2024-05-10 11:44:42 +02:00
|
|
|
let response = await fetch("/admin/account/mfa/confirm", {
|
2024-05-04 17:06:01 +02:00
|
|
|
credentials: "include",
|
|
|
|
body: JSON.stringify(body),
|
2024-05-06 13:11:46 +02:00
|
|
|
headers: { "Content-Type": "application/json", },
|
2024-05-04 17:06:01 +02:00
|
|
|
mode: "same-origin",
|
|
|
|
method: "POST"
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
let data = await response.json()
|
|
|
|
|
2024-05-06 15:32:53 +02:00
|
|
|
document.getElementById("dialog-tokens").textContent = data["recovery_tokens"].join(" ")
|
2024-05-10 11:44:42 +02:00
|
|
|
document.getElementById('user-dialog').showModal()
|
|
|
|
} else if (response.status == 400) {
|
|
|
|
// document.cookie = 'gourlsh_mfa_setup=; Max-Age=-1; path=/; domain=' + location.hostname;
|
|
|
|
|
|
|
|
document.getElementById('token').setAttribute('aria-invalid', 'true')
|
|
|
|
button.setAttribute('aria-busy', 'false')
|
|
|
|
button.disabled = false
|
|
|
|
document.getElementById('error-message').setAttribute('class', '')
|
2024-05-04 17:06:01 +02:00
|
|
|
}
|
2023-05-05 15:16:54 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 15:02:43 +02:00
|
|
|
async function HandleMFARemovalRequest() {
|
|
|
|
document.getElementById('mfa-disable-dialog').showModal()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function HandleMFARemoval() {
|
|
|
|
let keepButton = document.getElementById('button-keep')
|
|
|
|
let deleteButton = document.getElementById('button-delete')
|
|
|
|
|
|
|
|
keepButton.disabled = true
|
|
|
|
deleteButton.disabled = true
|
|
|
|
deleteButton.value = 'Please wait...'
|
|
|
|
deleteButton.setAttribute('aria-busy', 'true')
|
|
|
|
|
|
|
|
endpoint = "/admin/account/mfa"
|
|
|
|
let response = await fetch(endpoint, {
|
|
|
|
credentials: "include",
|
|
|
|
mode: "same-origin",
|
|
|
|
method: "DELETE"
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
document.location = "/admin/account"
|
|
|
|
} //else {
|
|
|
|
// TODO
|
|
|
|
// Handle error condition
|
|
|
|
//document.cookie = 'gourlsh_mfa_setup=; Max-Age=-1; path=/; domain=' + location.hostname;
|
|
|
|
|
|
|
|
//document.getElementById("dialog-heading").textContent = "Error!"
|
|
|
|
//document.getElementById("dialog-text").textContent = "Something didnt work!"
|
|
|
|
//document.getElementById('dialog-error').showModal()
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
2024-05-06 13:11:46 +02:00
|
|
|
async function HandleMFALoginTokenPost() {
|
|
|
|
document.getElementById("submit").disabled = true;
|
|
|
|
document.getElementById("token").disabled = true;
|
|
|
|
let token = document.getElementById("token").value
|
|
|
|
let body = {
|
|
|
|
"token": token
|
|
|
|
}
|
|
|
|
|
|
|
|
let response = await fetch("/admin/login/multifactor", {
|
|
|
|
credentials: "include",
|
|
|
|
headers: { "Content-Type": "application/json", },
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
mode: "same-origin",
|
|
|
|
method: "POST"
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
document.location = "/admin/"
|
|
|
|
} else {
|
|
|
|
document.getElementById("submit").disabled = false;
|
|
|
|
document.getElementById("token").disabled = false;
|
|
|
|
document.getElementById('error-message').innerHTML = "Two Factor Authentication failed. Please try again."
|
|
|
|
document.getElementById('error-message').setAttribute("class", "")
|
|
|
|
document.getElementById('token').value = ""
|
|
|
|
document.getElementById('token').setAttribute("aria-invalid", "true")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 15:02:43 +02:00
|
|
|
function HandleModalClose(id, redir) {
|
|
|
|
document.getElementById(id).close();
|
2024-05-04 17:06:01 +02:00
|
|
|
|
|
|
|
if (redir) {
|
|
|
|
document.location = redir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-05 15:16:54 +02:00
|
|
|
// General
|
|
|
|
|
|
|
|
function Logout() {
|
|
|
|
document.cookie = 'gourlsh_auth=; Max-Age=-1; path=/; domain=' + location.hostname;
|
|
|
|
document.location = "/admin/login"
|
|
|
|
}
|