costapy/static/js/accept.js

54 lines
2.2 KiB
JavaScript

function flushResponse() {
document.getElementById("alert-response" ).style.display = 'none';
document.getElementById("alert-response" ).classList.remove('alert-success' );
document.getElementById("alert-response" ).classList.remove('alert-danger' );
document.getElementById("alert-response" ).classList.remove('alert-primary' );
}
function loadingResponse() {
flushResponse();
document.getElementById("alert-status" ).innerHTML = "Loading...";
document.getElementById("alert-desc" ).innerHTML = "Please wait...";
document.getElementById("alert-response").classList.add('alert-primary');
document.getElementById("alert-response").style.display = 'block';
}
function responseAlert(response) {
flushResponse();
const obj = JSON.parse(response);
if (obj.status == "success" ) document.getElementById("alert-response").classList.add('alert-success' );
if (obj.status == "failed" ) document.getElementById("alert-response").classList.add('alert-danger' );
document.getElementById("alert-status" ).innerHTML = obj.status;
document.getElementById("alert-desc" ).innerHTML = obj.desc;
document.getElementById("alert-response").style.display = 'block';
}
var token = document.getElementById("form-token" ).value;
var getUsername = document.getElementById("form-get-username" ).value;
if (getUsername != null && getUsername != "") document.getElementById("form-username").disabled=true;
function accept() {
loadingResponse();
var username = getUsername;
if (getUsername == null || getUsername == "") username = document.getElementById('form-username').value;
const password = document.getElementById('form-password').value;
var url = "/api/auth/accept";
var payload = {
"username" : username,
"password" : password
};
sendHttpRequest(url, "POST", payload, function (error, response) {
if (error) {
// console.error("Error:", error);
responseAlert({"status" : "failed", "desc" : error});
}
else {
// console.log("JSON Response:", response);
responseAlert(response);
}
}, "application/json", `Bearer ${token}`);
}
flushResponse();