Untitled

 avatar
unknown
plain_text
2 years ago
1.1 kB
5
Indexable
// HTML
<form>
  <label for="username">Username:</label>
  <input type="text" id="username" name="username"><br><br>
  <label for="password">Password:</label>
  <input type="password" id="password" name="password"><br><br>
  <input type="submit" value="Submit">
</form>

// JavaScript
const form = document.querySelector('form');

form.addEventListener('submit', (event) => {
  event.preventDefault();
  
  const username = document.querySelector('#username').value;
  const password = document.querySelector('#password').value;
  
  // Send username and password to server for authentication
  fetch('/login', {
    method: 'POST',
    body: JSON.stringify({ username, password }),
    headers: {
      'Content-Type': 'application/json'
    }
  })
  .then(response => response.json())
  .then(data => {
    if (data.success) {
      // Redirect to dashboard
      window.location.href = '/dashboard';
    } else {
      // Show error message
      const error = document.createElement('p');
      error.textContent = 'Invalid username or password';
      form.appendChild(error);
    }
  });
});
Editor is loading...