46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
const alertBox = (type, message) => {
|
|
const alert = document.getElementById('message');
|
|
alert.classList.add(type);
|
|
alert.innerText = message;
|
|
alert.style.display = 'block';
|
|
};
|
|
|
|
document.getElementById('loginForm').addEventListener('submit', function (event) {
|
|
event.preventDefault();
|
|
|
|
const username = document.getElementById('username').value;
|
|
const password = document.getElementById('password').value;
|
|
const totp = document.getElementById('totp').value;
|
|
|
|
const body = { username, password };
|
|
if (document.getElementById('totpField').style.display !== 'none') {
|
|
body.totp = totp;
|
|
}
|
|
|
|
fetch(window.location.href, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(body)
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
console.log(data)
|
|
if (data.success) {
|
|
// Handle successful login
|
|
window.location.href = data.redirect || window.location.href.split('/').slice(0, -1).join('/');
|
|
} else if (data.totpRequired) {
|
|
// Prompt for TOTP
|
|
document.getElementById('totpField').style.display = 'block';
|
|
alertBox('alert-danger', data.message);
|
|
} else {
|
|
// Show error modal
|
|
alertBox('alert-danger', data.message);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
alertBox('alert-danger', 'An error has occurred. Please try again later.');
|
|
});
|
|
}); |