File scripts.js of Package mddb

let phase = "command";
let currentUsername = "";  // Global variable to store the current username

document.getElementById("alert").addEventListener("click", function(event) {
    if (event.target === event.currentTarget) {
        closeModal();
        resetTerminal();
    }
});

document.getElementById("securityInput").addEventListener("keydown", function(event) {
    if (event.key === "Enter") {
        const securityPhrase = document.getElementById("securityInput").value;
        verifyData("security", securityPhrase);
        event.target.value = "";  // Clear the security input field
    }
});

document.getElementById("input").addEventListener("keydown", function(event) {
    if (event.key === "Enter") {
        const inputData = event.target.value.trim();
        verifyData(phase, inputData);
        event.target.value = "";  // Clear the main input field
    }
});

function updatePrefix(text) {
    document.querySelector(".prefix").innerText = text;
}

function showModal() {
    document.getElementById("alert").style.visibility = "visible";
    phase = "security";
}

function closeModal() {
    document.getElementById("alert").style.visibility = "hidden";
}

function resetTerminal() {
    phase = "command";
    currentUsername = "";
    updatePrefix("root@MDInvestments:~$");
}

function verifyData(stage, inputData) {
    event.target.value = "";  // Clear input field
    let payload = { stage: stage };

    if (stage === "command" && inputData === "login") {
        phase = "username";
        updatePrefix("login:");
        return;
    } else if (stage === "username") {
        currentUsername = inputData;  // Update currentUsername
        payload.username = inputData;
        phase = "password";
        updatePrefix("hasło:");
    } else if (stage === "password") {
        payload.username = currentUsername;  // Use currentUsername
        payload.password = inputData;
        showModal();
        return;
    } else if (stage === "security") {
        payload.username = currentUsername;  // Use currentUsername
        payload.securityPhrase = inputData;
    }

    fetch('login.php', {
        method: 'POST',
        body: JSON.stringify(payload),
        headers: {
            'Content-Type': 'application/json'
        }
    })
    .then(response => response.json())
    .then(data => {
        if (data.status === 'success') {
            if (stage === "security") {
                closeModal();
                resetTerminal();
                alert("Pomyślnie zalogowano!");
                window.location.href = "dashboard.html";
            }
        } else {
            alert(data.message);
            phase = "command";
            updatePrefix("root@MDInvestments:~$");
        }
    });
}

function extractFromOutput(prefix) {
    const outputContent = document.getElementById("output").innerText;
    const line = outputContent.split("\n").find(line => line.startsWith(prefix));
    return line ? line.split(":")[1].trim() : "";
}
openSUSE Build Service is sponsored by