<div class="login-container">
    <h2>Log in</h2>
    <form id="login-form" action="https://aghep.org/login/index.php" method="post">
        <input type="hidden" id="logintoken" name="logintoken" value="">
        <div class="form-group">
            <input type="text" name="username" placeholder="Username" required>
        </div>
        <div class="form-group">
            <input type="password" name="password" placeholder="Password" required>
        </div>
        <button type="submit">Log in</button>
        <a href="https://aghep.org/login/forgot_password.php">Forgotten your username or password?</a>
    </form>
    <div class="signup-link">
        <p>First time here? <a href="https://aghep.org/login/signup.php">Create an account</a></p>
    </div>
</div>

<script>
    // Fetch the login page to retrieve the logintoken
    fetch('https://aghep.org/login/index.php')
        .then(response => response.text())
        .then(data => {
            // Create a DOM parser to extract the logintoken
            const parser = new DOMParser();
            const doc = parser.parseFromString(data, 'text/html');
            const tokenInput = doc.querySelector('input[name="logintoken"]');
            if (tokenInput) {
                document.getElementById('logintoken').value = tokenInput.value; // Set the token value in the form
            }
        })
        .catch(error => {
            console.error('Error fetching the login page:', error);
        });

    // Handle form submission
    document.getElementById('login-form').addEventListener('submit', function(event) {
        event.preventDefault(); // Prevent default form submission

        const formData = new FormData(this);
        const actionUrl = 'https://aghep.org/login/index.php';

        fetch(actionUrl, {
            method: 'POST',
            body: formData
        })
        .then(response => {
            if (response.redirected) {
                window.location.href = response.url; // Redirect to the Moodle dashboard or courses page
            } else {
                alert('Invalid login, please try again.');
            }
        })
        .catch(error => {
            console.error('Error during login:', error);
            alert('There was an error logging in. Please try again later.');
        });
    });
</script>

Last modified: Sunday, 6 October 2024, 9:29 PM