lavina_bridge/webpages/tutorials/login-form/js_script.js

71 lines
1.8 KiB
JavaScript

var x = document.getElementById("login");
var y = document.getElementById("register");
var z = document.getElementById("btn");
function register_form() {
x.style.left = "-400px";
y.style.left = "50px";
z.style.left = "120px";
}
function login_form() {
x.style.left = "50px";
y.style.left = "450px";
z.style.left = "0";
}
document.getElementById("login").addEventListener("submit", async function(event) {
var username = document.getElementById("login_input").value;
var password = document.getElementById("password_input").value;
await newPlayer(username, password);
});
const host = '127.0.0.1'
// Функция newPlayer
async function newPlayer(name, password) {
const urlCreatePlayer = 'http://'+host+':8080/mgmt/create_player';
const urlSetPassword = 'http://'+host+':8080/mgmt/set_password';
console.info(urlCreatePlayer);
const createQuery = {
name: name
};
try {
alert('Ready?');
fetch(urlCreatePlayer, {
method: 'POST',
mode: 'no-cors',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(createQuery)
});
alert('User created');
const setPasswordQuery = {
player_name: name,
password: password
};
fetch(urlSetPassword, {
method: 'POST',
mode: 'no-cors',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(setPasswordQuery)
});
alert('Password setted');
console.info('Игрок успешно создан и пароль установлен.');
} catch (error) {
console.error(`Произошла ошибка: ${error}`);
}
alert('End of creation');
}