// Main JavaScript for BavulumKatar'da document.addEventListener('DOMContentLoaded', function() { // Initialize animations const animateElements = document.querySelectorAll('.animate-fade-in'); animateElements.forEach((element, index) => { element.style.animationDelay = `${index * 0.1}s`; }); // Form validation and submission for signup const signupForm = document.getElementById('signup-form'); if (signupForm) { signupForm.addEventListener('submit', async function(e) { e.preventDefault(); const firstName = document.getElementById('firstName').value; const lastName = document.getElementById('lastName').value; const email = document.getElementById('email').value; const phone = document.getElementById('phone').value; const password = document.getElementById('password').value; const confirmPassword = document.getElementById('confirmPassword').value; if (password !== confirmPassword) { alert('Passwords do not match!'); return; } if (password.length < 8) { alert('Password must be at least 8 characters long!'); return; } try { const response = await fetch('http://localhost:5000/api/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ firstName, lastName, email, phone, password }) }); const data = await response.json(); if (!response.ok) { throw new Error(data.message || 'Registration failed'); } // Show success message and redirect to login alert('Registration successful! Please log in.'); window.location.href = 'login.html'; } catch (err) { alert(err.message || 'Registration failed. Please try again.'); console.error(err); } }); } // Form validation and submission for login const loginForm = document.getElementById('login-form'); if (loginForm) { loginForm.addEventListener('submit', async function(e) { e.preventDefault(); const email = document.getElementById('email').value; const password = document.getElementById('password').value; try { const response = await fetch('http://localhost:5000/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }) }); const data = await response.json(); if (!response.ok) { throw new Error(data.message || 'Login failed'); } // Save token and redirect localStorage.setItem('token', data.token); window.location.href = 'orders.html'; } catch (err) { alert(err.message || 'Login failed. Please try again.'); console.error(err); } }); } // Check authentication on protected pages if (window.location.pathname.includes('orders.html')) { const token = localStorage.getItem('token'); if (!token) { window.location.href = 'login.html'; return; } // Fetch user data or orders here feather.replace(); } }); // Helper function for authenticated requests async function authFetch(url, options = {}) { const token = localStorage.getItem('token'); if (!token) { window.location.href = 'login.html'; return; } options.headers = { ...options.headers, 'x-auth-token': token }; const response = await fetch(url, options); if (response.status === 401) { localStorage.removeItem('token'); window.location.href = 'login.html'; return; } return response; }