Add assets/js/login.js

login page with api
This commit is contained in:
Зоткин Владимир 2024-08-29 13:41:09 +00:00
parent ea92027a3c
commit fe065f3390

44
assets/js/login.js Normal file
View File

@ -0,0 +1,44 @@
document.addEventListener('DOMContentLoaded', function() {
let nickname, password;
function handleSubmit(event) {
event.preventDefault();
nickname = document.getElementById('nickname').value;
password = document.getElementById('password').value;
pageStatusUpdate();
}
function pageStatusUpdate() {
const req = {
nickname,
password
};
const apiUrl = 'http://127.0.0.1:1025/login';
fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(req)
})
.then(response => {
if (response.ok) {
return response.json();
}
})
.then(data => {
if (data && data.status === 0) {
window.location.href = '/list-rooms';
}
});
}
const form = document.querySelector('form');
form.addEventListener('submit', handleSubmit);
setInterval(pageStatusUpdate, 1000);
});