Files
boc/quixzoom-sso-auth/site-auth-snippet.js
T

60 lines
1.7 KiB
JavaScript
Raw Normal View History

/**
* quiXzoom Site Auth Snippet
* Include this on every quixzoom page for automatic auth
*
* Usage: Add to <head> of each HTML page:
* <script type="module" src="/auth-check.js"></script>
*/
import { quixzoomAuth } from 'https://auth.quixzoom.com/sso-client.js';
// Auto-check auth on page load
(async function() {
const user = await quixzoomAuth.init();
// Update UI based on auth state
updateAuthUI(user);
// Listen for auth state changes
document.addEventListener('quixzoom:auth', (e) => {
updateAuthUI(e.detail.user);
});
})();
function updateAuthUI(user) {
// Find auth elements
const authElements = document.querySelectorAll('[data-auth]');
authElements.forEach(el => {
const showWhen = el.dataset.auth; // 'logged-in' or 'logged-out'
const isLoggedIn = user !== null;
if (showWhen === 'logged-in') {
el.style.display = isLoggedIn ? '' : 'none';
// Update user info
if (isLoggedIn && el.dataset.authName) {
const nameEl = el.querySelector('[data-auth-user-name]');
if (nameEl) {
nameEl.textContent = `${user.first_name || ''} ${user.last_name || ''}`.trim() || user.email;
}
}
} else if (showWhen === 'logged-out') {
el.style.display = isLoggedIn ? 'none' : '';
}
});
// Update login links to include redirect
document.querySelectorAll('a[href*="auth.quixzoom.com/login"]').forEach(link => {
const url = new URL(link.href);
url.searchParams.set('redirect', window.location.href);
link.href = url.toString();
});
}
// Global logout function
window.quixzoomLogout = async () => {
await quixzoomAuth.logout();
window.location.reload();
};