Files
rpgzrox/rpg/js/login.js

65 lines
1.6 KiB
JavaScript

function load_ficha() {
$.ajax({
url: window.api + "player",
type: "GET",
xhrFields: {
withCredentials: true,
},
success: function (resp) {
window.location.href = "index.html";
},
});
}
function verifyInputs(player, passwd) {
if (player && !player.includes(" ") && passwd && !passwd.includes(" ")) {
return true;
}
return false;
}
$(document).ready(function () {
load_ficha();
$("#loginBtn").on("click", function () {
const $btn = $(this);
$btn.prop("disabled", true).val("Logando ...");
const player = $("#loginInput[name='player']").val();
const password = $("#loginInput[name='password']").val();
if (!verifyInputs(player, password)) {
msg("Error", "Por favor, preencha todos os dados corretamente.");
return finalizarErro($btn, "Login");
}
const form = new FormData();
form.append("player", player);
form.append("password", password);
$.ajax({
url: window.api + "login",
type: "POST",
data: form,
contentType: false,
processData: false,
xhrFields: { withCredentials: true },
success: function (resp) {
msg(resp.Status, resp.Message);
$btn.val("Concluído");
window.location.href = "index.html";
},
error: function (resp) {
msg(
resp.responseJSON?.Status || "Erro",
resp.responseJSON?.Message || "Algo deu errado.",
);
finalizarErro($btn, "Login");
},
});
});
function finalizarErro($btn, texto = "Login") {
$btn.prop("disabled", false).val(texto);
}
});