login.js 954 B

1234567891011121314151617181920212223242526
  1. document.getElementById('login-form').addEventListener('submit', function(event) {
  2. event.preventDefault();
  3. const username = document.getElementById('username').value;
  4. const password = document.getElementById('password').value;
  5. const message = document.getElementById('message');
  6. let users = [];
  7. if (localStorage.getItem('users')) {
  8. users = JSON.parse(localStorage.getItem('users'));
  9. }
  10. const user = users.find(u => u.username === username && u.password === password);
  11. if (!user) {
  12. message.textContent = "Неправильное имя пользователя или пароль!";
  13. message.style.color = "red";
  14. return;
  15. }
  16. localStorage.setItem('currentUser', JSON.stringify(user));
  17. message.textContent = "Вход выполнен успешно!";
  18. message.style.color = "green";
  19. setTimeout(() => {
  20. window.location.href = 'profile.html';
  21. }, 2000);
  22. });