// Shared utility functions const API_BASE_URL = 'http://localhost:3000/api'; async function fetchWithAuth(url, options = {}) { const token = localStorage.getItem('token'); if (token) { options.headers = { ...options.headers, 'Authorization': `Bearer ${token}` }; } const response = await fetch(`${API_BASE_URL}${url}`, options); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.json(); } function redirectTo(path) { window.location.href = path; } function showToast(message, type = 'success') { const toast = document.createElement('div'); toast.className = `fixed top-4 right-4 px-4 py-2 rounded-md shadow-lg text-white ${ type === 'success' ? 'bg-green-500' : 'bg-red-500' }`; toast.textContent = message; document.body.appendChild(toast); setTimeout(() => { toast.classList.add('opacity-0', 'transition-opacity', 'duration-300'); setTimeout(() => toast.remove(), 300); }, 3000); } // Auth state management function checkAuth() { const token = localStorage.getItem('token'); const user = localStorage.getItem('user'); if (token && user) { const parsedUser = JSON.parse(user); if (parsedUser.role === 'admin') { redirectTo('/admin-dashboard.html'); } else { redirectTo('/dashboard.html'); } } } // Initialize auth check on pages that require it if (document.body.classList.contains('auth-required')) { document.addEventListener('DOMContentLoaded', checkAuth); }