| |
| let pendingAction = null; |
|
|
| |
| document.addEventListener('DOMContentLoaded', function() { |
| initAuthUI(); |
| updateNavbar(); |
| }); |
|
|
| |
| function initAuthUI() { |
| |
| const modal = document.getElementById('loginModal'); |
| const closeBtn = document.getElementById('modalClose'); |
| const demoBtn = document.getElementById('demoLoginBtn'); |
|
|
| if (closeBtn) { |
| closeBtn.addEventListener('click', closeModal); |
| } |
|
|
| if (demoBtn) { |
| demoBtn.addEventListener('click', handleDemoLogin); |
| } |
|
|
| |
| if (modal) { |
| modal.addEventListener('click', (e) => { |
| if (e.target === modal) { |
| closeModal(); |
| } |
| }); |
| } |
|
|
| |
| document.addEventListener('keydown', (e) => { |
| if (e.key === 'Escape' && isModalOpen()) { |
| closeModal(); |
| } |
| }); |
| } |
|
|
| |
| function showLoginModal(action = null) { |
| pendingAction = action; |
| const modal = document.getElementById('loginModal'); |
| if (modal) { |
| modal.style.display = 'flex'; |
| document.body.style.overflow = 'hidden'; |
| |
| |
| const focusableElements = modal.querySelectorAll('button, a'); |
| if (focusableElements.length > 0) { |
| focusableElements[0].focus(); |
| } |
| } |
| } |
|
|
| |
| function closeModal() { |
| const modal = document.getElementById('loginModal'); |
| if (modal) { |
| modal.style.display = 'none'; |
| document.body.style.overflow = ''; |
| pendingAction = null; |
| } |
| } |
|
|
| |
| function isModalOpen() { |
| const modal = document.getElementById('loginModal'); |
| return modal && modal.style.display === 'flex'; |
| } |
|
|
| |
| async function handleDemoLogin() { |
| try { |
| const result = await auth.loginAsDemo(); |
| |
| if (result.success) { |
| auth.showToast(`Welcome, ${result.user.name}!`, 'success'); |
| closeModal(); |
| updateNavbar(); |
| |
| |
| if (pendingAction && typeof pendingAction === 'function') { |
| pendingAction(); |
| } |
| } else { |
| auth.showToast(result.error || 'Demo login failed', 'error'); |
| } |
| } catch (error) { |
| auth.showToast('An error occurred. Please try again.', 'error'); |
| } |
| } |
|
|
| |
| function updateNavbar() { |
| const navActions = document.querySelector('.nav-actions'); |
| if (!navActions) return; |
|
|
| if (auth.isAuthenticated()) { |
| const user = auth.getUser(); |
| |
| |
| navActions.innerHTML = ` |
| <div class="user-dropdown"> |
| <button class="user-avatar" id="userAvatar" aria-label="User menu"> |
| ${user.avatar || user.name.charAt(0).toUpperCase()} |
| </button> |
| <div class="dropdown-menu" id="userDropdown"> |
| <div class="dropdown-header"> |
| <div class="user-info"> |
| <div class="user-name">${user.name}</div> |
| <div class="user-email">${user.email}</div> |
| </div> |
| </div> |
| <div class="dropdown-divider"></div> |
| <a href="#" class="dropdown-item" id="profileLink"> |
| <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> |
| <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/> |
| <circle cx="12" cy="7" r="4"/> |
| </svg> |
| Profile |
| </a> |
| <a href="#" class="dropdown-item" id="savedCasesLink"> |
| <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> |
| <path d="M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z"/> |
| </svg> |
| Saved Cases |
| </a> |
| <a href="#" class="dropdown-item" id="settingsLink"> |
| <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> |
| <circle cx="12" cy="12" r="3"/> |
| <path d="M12 1v6m0 6v6m11-7h-6m-6 0H1"/> |
| </svg> |
| Settings |
| </a> |
| <div class="dropdown-divider"></div> |
| <button class="dropdown-item logout-btn" id="logoutBtn"> |
| <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> |
| <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/> |
| <polyline points="16,17 21,12 16,7"/> |
| <line x1="21" y1="12" x2="9" y2="12"/> |
| </svg> |
| Log out |
| </button> |
| </div> |
| </div> |
| `; |
|
|
| |
| const userAvatar = document.getElementById('userAvatar'); |
| const userDropdown = document.getElementById('userDropdown'); |
| const logoutBtn = document.getElementById('logoutBtn'); |
|
|
| |
| if (userAvatar) { |
| userAvatar.addEventListener('click', (e) => { |
| e.stopPropagation(); |
| userDropdown.classList.toggle('show'); |
| }); |
| } |
|
|
| |
| document.addEventListener('click', (e) => { |
| if (!userAvatar?.contains(e.target) && !userDropdown?.contains(e.target)) { |
| userDropdown?.classList.remove('show'); |
| } |
| }); |
|
|
| |
| if (logoutBtn) { |
| logoutBtn.addEventListener('click', () => { |
| auth.logout(); |
| updateNavbar(); |
| location.reload(); |
| }); |
| } |
|
|
| |
| document.getElementById('profileLink')?.addEventListener('click', (e) => { |
| e.preventDefault(); |
| auth.showToast('Profile page coming soon!', 'info'); |
| userDropdown.classList.remove('show'); |
| }); |
|
|
| document.getElementById('savedCasesLink')?.addEventListener('click', (e) => { |
| e.preventDefault(); |
| auth.showToast('Saved cases feature coming soon!', 'info'); |
| userDropdown.classList.remove('show'); |
| }); |
|
|
| document.getElementById('settingsLink')?.addEventListener('click', (e) => { |
| e.preventDefault(); |
| auth.showToast('Settings page coming soon!', 'info'); |
| userDropdown.classList.remove('show'); |
| }); |
| } else { |
| |
| navActions.innerHTML = ` |
| <button class="btn btn-outline" id="loginBtn">Log in</button> |
| `; |
|
|
| |
| document.getElementById('loginBtn')?.addEventListener('click', () => { |
| window.location.href = 'login.html'; |
| }); |
| } |
| } |
|
|
| |
| function requireAuth(action) { |
| if (auth.isAuthenticated()) { |
| |
| if (action && typeof action === 'function') { |
| action(); |
| } |
| } else { |
| |
| showLoginModal(action); |
| } |
| } |
|
|
| |
| window.requireAuth = requireAuth; |
| window.showLoginModal = showLoginModal; |
| window.updateNavbar = updateNavbar; |
|
|