Files
FACULTATE-LICENTA/UC/frontend/public/js/login.js
T

39 lines
1.2 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
const loginForm = document.getElementById('loginForm');
loginForm.addEventListener('submit', async function (e) {
e.preventDefault();
const formData = new FormData(loginForm);
const email = formData.get('email');
const password = formData.get('password');
const data = { email, password };
try {
const response = await fetch('/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
const result = await response.json();
if (response.ok && result.success) {
// Set cookie with email and name
document.cookie = `email=${result.user.email};`;
document.cookie = `name=${result.user.name};`;
window.location.href = '/';
} else {
alert('Incorrect credentials. Please try again.');
}
} catch (error) {
console.error('Error during fetch:', error);
alert('An error occurred. Please try again.');
}
});
});