feat : changed post_request to be more general and return true/false based on response

This commit is contained in:
daragh 2024-01-01 18:36:48 +00:00
parent c215c710b9
commit 5f11bbce09
10 changed files with 41 additions and 68 deletions

View file

@ -1,13 +1,12 @@
export async function post_request(url, body, form_status) { export async function request(url, body, form_status, method) {
try { try {
const req = await fetch(url, { const req = await fetch(url, {
method: 'POST', method: method,
body: JSON.stringify(body), body: JSON.stringify(body),
mode: "cors" mode: "cors"
}); });
if (req.status !== 200) { if (req.status !== 200) {
form_status.innerHTML = "<span style='background-color: red; color: white'>Failure</span>"; form_status.innerHTML = "<span style='background-color: red; color: white'>Failure</span>";
return;
} }
let temp = await req.json(); let temp = await req.json();
@ -15,8 +14,10 @@ export async function post_request(url, body, form_status) {
form_status.innerHTML = `<span style='background-color: red; color: white'>${temp.error}</span>`; form_status.innerHTML = `<span style='background-color: red; color: white'>${temp.error}</span>`;
} else { } else {
form_status.innerHTML = "<span style='background-color: green; color: white'>Success</span>"; form_status.innerHTML = "<span style='background-color: green; color: white'>Success</span>";
return true;
} }
} catch (e) { } catch (e) {
form_status.innerHTML = `<span style='background-color: red; color: white'>${e}</span>`; form_status.innerHTML = `<span style='background-color: red; color: white'>${e}</span>`;
} }
return false;
} }

View file

@ -1,4 +1,4 @@
import {post_request} from "./js/lib.js"; import {request} from "./js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -27,7 +27,7 @@ async function formHandler(listener) {
const body = {auth: {user: user, pass: pass}, field: dropdown_value, value: value}; const body = {auth: {user: user, pass: pass}, field: dropdown_value, value: value};
let url = 'https://api.account.skynet.ie/ldap/update'; let url = 'https://api.account.skynet.ie/ldap/update';
await post_request(url, body, form_status); await request(url, body, form_status, 'POST');
} }
function selectField() { function selectField() {

View file

@ -12,11 +12,11 @@
margin-left: auto; margin-left: auto;
margin-right: auto; margin-right: auto;
table, th { & table, & th {
border: 1px solid; border: 1px solid;
} }
td { & td {
border: 1px solid; border: 1px solid;
width: fit-content; width: fit-content;
white-space: nowrap; white-space: nowrap;

View file

@ -1,3 +1,5 @@
import {request} from "./js/lib";
let got_keys = false; let got_keys = false;
async function get_keys() { async function get_keys() {
@ -37,63 +39,30 @@ async function add_key() {
let username = document.getElementById("user").value; let username = document.getElementById("user").value;
let password = document.getElementById("pass").value; let password = document.getElementById("pass").value;
let key = document.getElementById("key_input").value; let key = document.getElementById("key_input").value;
let request = {auth: {user: username, pass: password}, key: key}; let body = {auth: {user: username, pass: password}, key: key};
if (key === "") { let url = 'https://api.account.skynet.ie/ldap/ssh/add';
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);
} catch (err) { if(await request(url,body, document.getElementById('formStatus'), 'POST')){
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>Error: ${err}</span>`; document.getElementById('key_table').style.display = "table";
console.log(err); add_to_table(key);
} }
} }
async function delete_key(row_idx) { async function delete_key(row_idx) {
let table = document.getElementById("key_table"); 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 username = document.getElementById("user").value;
let password = document.getElementById("pass").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>`; let body = {auth: {user: username, pass: password}, key: key};
console.log(err); 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]}`; cell_key.textContent = `${key_split[0]} ${key_split[1]}`;
//Names for SSH keys can have multiple spaces //Names for SSH keys can have multiple spaces
let comment = ""; let comment = "";
for (i = 2; i < key_split.length; i++) { for (let i = 2; i < key_split.length; i++) {
comment += " " + key_split[i]; comment += " " + key_split[i];
} }
if(key_split.length === 2) {
comment = "No comment";
}
cell_name.textContent = comment; cell_name.textContent = comment;
let cell_delete = row.insertCell(2); let cell_delete = row.insertCell(2);
let delete_button = document.createElement("button"); let delete_button = document.createElement("button");

View file

@ -1,4 +1,4 @@
import {post_request} from "./js/lib.js"; import {request} from "./js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -24,5 +24,5 @@ async function formHandler(listener) {
const body = {auth: {user: user, pass: pass}, field: "userPassword", value: newPW} const body = {auth: {user: user, pass: pass}, field: "userPassword", value: newPW}
let url = 'https://api.account.skynet.ie/ldap/update'; let url = 'https://api.account.skynet.ie/ldap/update';
await post_request(url, body, form_status); await request(url, body, form_status, 'POST');
} }

View file

@ -1,4 +1,4 @@
import {post_request} from "../js/lib.js"; import {request} from "../js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -33,5 +33,5 @@ async function formHandler(listener) {
let url = "https://api.account.skynet.ie/ldap/recover/password/auth"; let url = "https://api.account.skynet.ie/ldap/recover/password/auth";
let body = {auth: auth, pass: pass}; let body = {auth: auth, pass: pass};
await post_request(url, body, form_status); await request(url, body, form_status, 'POST');
} }

View file

@ -1,4 +1,4 @@
import {post_request} from "../js/lib.js"; import {request} from "../js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -15,5 +15,5 @@ async function formHandler(listener) {
const body = {user: formData.get('user'), email: formData.get('mail')}; const body = {user: formData.get('user'), email: formData.get('mail')};
let url = 'https://api.account.skynet.ie/ldap/recover/ssh/request'; let url = 'https://api.account.skynet.ie/ldap/recover/ssh/request';
await post_request(url, body, form_status); await request(url, body, form_status, 'POST');
} }

View file

@ -1,4 +1,4 @@
import {post_request} from "../js/lib.js"; import {request} from "../js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -21,5 +21,5 @@ async function formHandler(listener) {
let url = 'https://api.account.skynet.ie/ldap/recover/username'; let url = 'https://api.account.skynet.ie/ldap/recover/username';
await post_request(url, body, form_status); await request(url, body, form_status, 'POST');
} }

View file

@ -1,4 +1,4 @@
import {post_request} from "./js/lib.js"; import {request} from "./js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -31,5 +31,5 @@ async function formHandler(listener) {
const body = {auth: auth, user: user, pass: pass}; const body = {auth: auth, user: user, pass: pass};
let url = 'https://api.account.skynet.ie/ldap/new/account'; let url = 'https://api.account.skynet.ie/ldap/new/account';
await post_request(url, body, form_status); await request(url, body, form_status, 'POST');
} }

View file

@ -1,4 +1,4 @@
import {post_request} from "./js/lib.js"; import {request} from "./js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -19,6 +19,6 @@ async function formHandler(listener) {
let url = 'https://api.account.skynet.ie/ldap/new/email'; let url = 'https://api.account.skynet.ie/ldap/new/email';
await post_request(url, body, form_status); await request(url, body, form_status, 'POST');
} }