// frontend/static/js/common.js // Check authentication status and update the auth section (async function() { console.log('Checking authentication status...'); try { const response = await fetch('/api/auth/whoami'); const data = await response.json(); const authSection = document.getElementById("authSection"); const mobileAuthSection = document.getElementById("mobileAuthSection"); // If we have an email, user is authenticated (the whoami endpoint would have thrown 401 otherwise) if (data.email) { // Get the display name (prefer name, fall back to preferred_username, then email) const displayName = data.name || data.preferred_username || data.email; // User is logged in let authHTML = `
Avatar ${displayName}
`; if (authSection) { authSection.innerHTML = authHTML; } if (mobileAuthSection) { mobileAuthSection.innerHTML = `
Avatar ${displayName}
Logout
`; } } else { // User is not logged in (this shouldn't happen with current setup, but keeping as fallback) if (authSection) { authSection.innerHTML = `Login`; } if (mobileAuthSection) { mobileAuthSection.innerHTML = `Login`; } } } catch (error) { console.error('Authentication check failed:', error); // Fallback if whoami endpoint fails const authSection = document.getElementById("authSection"); const mobileAuthSection = document.getElementById("mobileAuthSection"); if (authSection) { authSection.innerHTML = `Login`; } if (mobileAuthSection) { mobileAuthSection.innerHTML = `Login`; } } })(); // Other common functionality can be added here