This commit is contained in:
AuroraLS3 2021-07-10 07:14:40 +00:00
parent 1eab71a1a5
commit bfd229d427
3 changed files with 65 additions and 33 deletions

View File

@ -56,40 +56,68 @@ function refreshingJsonRequest(address, callback, tabID, skipOldData) {
}
/**
* Make an XMLHttpRequest for JSON data.
* Make a GET XMLHttpRequest for JSON data.
* @param address Address to request from
* @param callback function with (json, error) parameters to call after the request.
*/
function jsonRequest(address, callback) {
setTimeout(function () {
const xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
try {
if (this.status === 200 || (this.status === 0 && this.responseText)) {
var json = JSON.parse(this.responseText);
setTimeout(function () {
callback(json, null)
}, 0);
} else if (this.status === 404 || this.status === 403 || this.status === 500) {
callback(null, "HTTP " + this.status + " (See " + address + ")")
} else if (this.status === 400) {
const json = JSON.parse(this.responseText);
callback(json, json.error)
} else if (this.status === 0) {
callback(null, "Request did not reach the server. (Server offline / Adblocker?)")
}
} catch (e) {
callback(null, e.message + " (See " + address + ")")
}
}
};
xhr.timeout = 45000;
xhr.ontimeout = function () {
callback(null, "Timed out after 45 seconds. (" + address + ")")
};
const xhr = newConfiguredXHR(callback);
xhr.open("GET", address, true);
xhr.send();
}, 0);
}
/**
* Make a POST XMLHttpRequest for JSON data.
* @param address Address to request from
* @param postBody POST body (form).
* @param callback function with (json, error) parameters to call after the request.
*/
function jsonPostRequest(address, postBody, callback) {
setTimeout(function () {
const xhr = newConfiguredXHR(callback);
xhr.open("POST", address, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(postBody);
}, 0);
}
/**
* Create new XMLHttpRequest configured for methods such as jsonRequest
* @param callback function with (json, error) parameters to call after the request.
*/
function newConfiguredXHR(callback) {
const xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
try {
if (this.status === 200 || (this.status === 0 && this.responseText)) {
var json = JSON.parse(this.responseText);
setTimeout(function () {
callback(json, null)
}, 0);
} else if (this.status === 404 || this.status === 403 || this.status === 500) {
callback(null, "HTTP " + this.status + " (See " + address + ")")
} else if (this.status === 400) {
const json = JSON.parse(this.responseText);
callback(json, json.error)
} else if (this.status === 0) {
callback(null, "Request did not reach the server. (Server offline / Adblocker?)")
}
} catch (e) {
callback(null, e.message + " (See " + address + ")")
}
}
};
xhr.timeout = 45000;
xhr.ontimeout = function () {
callback(null, "Timed out after 45 seconds. (" + address + ")")
};
return xhr;
}

View File

@ -41,7 +41,7 @@
if (!password || password.length < 1) {
return displayError('You need to specify a Password');
}
jsonRequest(`./auth/login?user=${encodeURIComponent(user)}&password=${encodeURIComponent(password)}`, (json, error) => {
jsonPostRequest(`./auth/login`, `user=${encodeURIComponent(user)}&password=${encodeURIComponent(password)}`, (json, error) => {
if (error) {
if (error.includes("HTTP 403")) {
location.reload();
@ -74,7 +74,7 @@
<div class="col-12 mt-5 text-center">
<img alt="logo" class="w-15" src="img/Flaticon_circle.png">
</div>
<div class="row justify-content-center">
<div class="row justify-content-center container-fluid">
<div class="col-xl-6 col-lg-7 col-md-9">
<div class="card o-hidden border-0 shadow-lg my-5">
<div class="card-body p-0">

View File

@ -33,7 +33,7 @@
<div class="col-12 mt-5 text-center">
<img alt="logo" class="w-15" src="img/Flaticon_circle.png">
</div>
<div class="row justify-content-center">
<div class="row justify-content-center container-fluid">
<div class="col-xl-6 col-lg-7 col-md-9">
<div class="card o-hidden border-0 shadow-lg my-5">
<div class="card-body p-0">
@ -185,6 +185,7 @@
<script id="mainScript">
const errorElement = document.getElementById("fail-msg");
let finalizeModal;
function displayError(message) {
errorElement.innerText = message;
@ -194,9 +195,11 @@
function checkIfRegistered(code) {
jsonRequest(`./auth/register?code=${encodeURIComponent(code)}`, (json, error) => {
if (error) {
finalizeModal.hide();
displayError('Checking registration status failed: ' + error)
}
if (json && json.success) {
finalizeModal.hide();
window.location.href = "./login";
} else {
setTimeout(() => checkIfRegistered(code), 5000);
@ -217,13 +220,14 @@
if (!password || password.length < 1) {
return displayError('You need to specify a Password');
}
jsonRequest(`./auth/register?user=${encodeURIComponent(user)}&password=${encodeURIComponent(password)}`, (json, error) => {
jsonPostRequest(`./auth/register`, `user=${encodeURIComponent(user)}&password=${encodeURIComponent(password)}`, (json, error) => {
if (error) {
return displayError('Registration failed: ' + error);
}
const code = json.code;
$('.register-code').text(code);
$('#finalizeModal').modal();
finalizeModal = new bootstrap.Modal(document.getElementById("finalizeModal"));
finalizeModal.show();
setTimeout(() => checkIfRegistered(code), 10000);
});
})