Agile / project 5 /auth-ui.js
thatsdevvvv's picture
Upload 2050 files
390cffd verified
// Authentication UI management
let pendingAction = null;
// Initialize auth UI when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
initAuthUI();
updateNavbar();
});
// Initialize authentication UI
function initAuthUI() {
// Modal functionality
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);
}
// Close modal on backdrop click
if (modal) {
modal.addEventListener('click', (e) => {
if (e.target === modal) {
closeModal();
}
});
}
// Close modal on Escape key
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && isModalOpen()) {
closeModal();
}
});
}
// Show login modal
function showLoginModal(action = null) {
pendingAction = action;
const modal = document.getElementById('loginModal');
if (modal) {
modal.style.display = 'flex';
document.body.style.overflow = 'hidden';
// Focus management
const focusableElements = modal.querySelectorAll('button, a');
if (focusableElements.length > 0) {
focusableElements[0].focus();
}
}
}
// Close login modal
function closeModal() {
const modal = document.getElementById('loginModal');
if (modal) {
modal.style.display = 'none';
document.body.style.overflow = '';
pendingAction = null;
}
}
// Check if modal is open
function isModalOpen() {
const modal = document.getElementById('loginModal');
return modal && modal.style.display === 'flex';
}
// Handle demo login from modal
async function handleDemoLogin() {
try {
const result = await auth.loginAsDemo();
if (result.success) {
auth.showToast(`Welcome, ${result.user.name}!`, 'success');
closeModal();
updateNavbar();
// Execute pending action if any
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');
}
}
// Update navbar based on auth state
function updateNavbar() {
const navActions = document.querySelector('.nav-actions');
if (!navActions) return;
if (auth.isAuthenticated()) {
const user = auth.getUser();
// Create user dropdown
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>
`;
// Add event listeners
const userAvatar = document.getElementById('userAvatar');
const userDropdown = document.getElementById('userDropdown');
const logoutBtn = document.getElementById('logoutBtn');
// Toggle dropdown
if (userAvatar) {
userAvatar.addEventListener('click', (e) => {
e.stopPropagation();
userDropdown.classList.toggle('show');
});
}
// Close dropdown when clicking outside
document.addEventListener('click', (e) => {
if (!userAvatar?.contains(e.target) && !userDropdown?.contains(e.target)) {
userDropdown?.classList.remove('show');
}
});
// Handle logout
if (logoutBtn) {
logoutBtn.addEventListener('click', () => {
auth.logout();
updateNavbar();
location.reload(); // Refresh to update navbar
});
}
// Handle other dropdown items (placeholder functionality)
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 {
// User not authenticated, show login button
navActions.innerHTML = `
<button class="btn btn-outline" id="loginBtn">Log in</button>
`;
// Add login button event listener
document.getElementById('loginBtn')?.addEventListener('click', () => {
window.location.href = 'login.html';
});
}
}
// Auth guard function
function requireAuth(action) {
if (auth.isAuthenticated()) {
// User is authenticated, execute action
if (action && typeof action === 'function') {
action();
}
} else {
// User not authenticated, show login modal
showLoginModal(action);
}
}
// Make functions globally available
window.requireAuth = requireAuth;
window.showLoginModal = showLoginModal;
window.updateNavbar = updateNavbar;