feat : changed post_request to be more general and return true/false based on response
This commit is contained in:
parent
c215c710b9
commit
5f11bbce09
10 changed files with 41 additions and 68 deletions
|
@ -1,3 +1,5 @@
|
|||
import {request} from "./js/lib";
|
||||
|
||||
let got_keys = false;
|
||||
|
||||
async function get_keys() {
|
||||
|
@ -37,63 +39,30 @@ async function add_key() {
|
|||
let username = document.getElementById("user").value;
|
||||
let password = document.getElementById("pass").value;
|
||||
let key = document.getElementById("key_input").value;
|
||||
let request = {auth: {user: username, pass: password}, key: key};
|
||||
if (key === "") {
|
||||
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Key cannot be empty</span>";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const response = await fetch('https://api.account.skynet.ie/ldap/ssh/add', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(request),
|
||||
mode: 'cors',
|
||||
});
|
||||
if (response.status !== 200) {
|
||||
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failed to fetch</span>";
|
||||
return;
|
||||
}
|
||||
const data = await response.json();
|
||||
if (data.result !== "success") {
|
||||
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>Error: ${data.error}</span>`;
|
||||
return;
|
||||
}
|
||||
document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Success: Key added</span>";
|
||||
document.getElementById('key_table').style.display = "table";
|
||||
add_to_table(request.key);
|
||||
let body = {auth: {user: username, pass: password}, key: key};
|
||||
let url = 'https://api.account.skynet.ie/ldap/ssh/add';
|
||||
|
||||
} catch (err) {
|
||||
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>Error: ${err}</span>`;
|
||||
console.log(err);
|
||||
if(await request(url,body, document.getElementById('formStatus'), 'POST')){
|
||||
document.getElementById('key_table').style.display = "table";
|
||||
add_to_table(key);
|
||||
}
|
||||
}
|
||||
|
||||
async function delete_key(row_idx) {
|
||||
let table = document.getElementById("key_table");
|
||||
let key = table.rows[row_idx].cells[0].innerHTML;
|
||||
let key = table.rows[row_idx].cells[1].innerHTML;
|
||||
let comment = table.rows[row_idx].cells[0].innerHTML;
|
||||
key += comment;
|
||||
|
||||
let username = document.getElementById("user").value;
|
||||
let password = document.getElementById("pass").value;
|
||||
let request = {auth: {user: username, pass: password}, key: key};
|
||||
try {
|
||||
const response = await fetch('https://api.account.skynet.ie/ldap/ssh', {
|
||||
method: 'DELETE',
|
||||
body: JSON.stringify(request),
|
||||
mode: 'cors',
|
||||
});
|
||||
if (response.status !== 200) {
|
||||
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failed to fetch</span>";
|
||||
return;
|
||||
}
|
||||
const data = await response.json();
|
||||
if (data.result !== "success") {
|
||||
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>Error: ${data.error}</span>`;
|
||||
return;
|
||||
}
|
||||
table.deleteRow(row_idx);
|
||||
document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Success: Key deleted</span>";
|
||||
|
||||
} catch (err) {
|
||||
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>Error: ${err}</span>`;
|
||||
console.log(err);
|
||||
|
||||
let body = {auth: {user: username, pass: password}, key: key};
|
||||
let url = 'https://api.account.skynet.ie/ldap/ssh';
|
||||
|
||||
if(await request(url,body, document.getElementById('formStatus'), 'DELETE')){
|
||||
table.deleteRow(row_idx);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -108,9 +77,12 @@ function add_to_table(key) {
|
|||
cell_key.textContent = `${key_split[0]} ${key_split[1]}`;
|
||||
//Names for SSH keys can have multiple spaces
|
||||
let comment = "";
|
||||
for (i = 2; i < key_split.length; i++) {
|
||||
for (let i = 2; i < key_split.length; i++) {
|
||||
comment += " " + key_split[i];
|
||||
}
|
||||
if(key_split.length === 2) {
|
||||
comment = "No comment";
|
||||
}
|
||||
cell_name.textContent = comment;
|
||||
let cell_delete = row.insertCell(2);
|
||||
let delete_button = document.createElement("button");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue