/** * Seamless Login JavaScript * Handles logout strip and Disqus configuration updates without page reload */ (function () { // Fetch Disqus configuration from server function fetchDisqusConfig() { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open('POST', window.googleOneTapAjax.ajax_url, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); const data = new URLSearchParams({ action: 'get_disqus_config', nonce: window.googleOneTapAjax.nonces.disqus, }); xhr.onload = function () { if (xhr.status === 200) { try { const response = JSON.parse(xhr.responseText); if (response.success) { resolve(response.data); } else { reject(new Error(response.data || 'Failed to get Disqus config')); } } catch (e) { reject(new Error(`Error parsing Disqus config response: ${e.message}`)); } } else { reject(new Error(`AJAX request failed with status: ${xhr.status}`)); } }; xhr.onerror = function () { reject(new Error('AJAX request failed')); }; xhr.send(data); }); } // Set up Disqus SSO login configuration function setupDisqusSSOLogin(config) { if (typeof window.DISQUS !== 'undefined') { window.DISQUS.reset({ reload: true, config() { this.page.identifier = window.location.pathname; this.page.url = window.location.href; this.sso = { name: 'COMICBOOK', button: 'https://comicbook.com/wp-content/uploads/sites/4/2025/12/login-button-cb.png', url: config.login_url, logout: window.location.href, width: '500', height: '500', }; }, }); } else { // Set up configuration for when Disqus loads window.disqus_config = function () { this.page.identifier = window.location.pathname; this.page.url = window.location.href; this.sso = { name: 'COMICBOOK', button: 'https://comicbook.com/wp-content/uploads/sites/4/2025/12/login-button-cb.png', url: config.login_url, logout: window.location.href, width: '500', height: '500', }; }; } } // Check if Disqus is loaded function isDisqusLoaded() { return typeof window.DISQUS !== 'undefined'; } // Check if Disqus SSO is configured function isDisqusSSOConfigured() { return typeof window.DISQUS !== 'undefined' && window.DISQUS.config && window.DISQUS.config.sso; } // Check if Disqus config is set function isDisqusConfigSet() { return typeof window.disqus_config === 'function' || isDisqusSSOConfigured(); } // Wait for Disqus to load and configure it function waitForDisqusAndConfigure() { const checkInterval = setInterval(() => { if (isDisqusLoaded()) { clearInterval(checkInterval); if (!isDisqusConfigSet()) { // eslint-disable-next-line no-console console.log('Disqus loaded but SSO not configured, setting up now...'); fetchDisqusConfig().then((config) => { if (config && !config.user_logged_in) { setupDisqusSSOLogin(config); } }).catch((error) => { // eslint-disable-next-line no-console console.error('Failed to configure Disqus after load:', error); }); } } }, 1000); // Stop checking after 30 seconds setTimeout(() => { clearInterval(checkInterval); }, 30000); } // Get fresh nonces from server function getFreshNonces() { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open('POST', window.googleOneTapAjax.ajax_url, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); const data = new URLSearchParams({ action: 'get_fresh_nonces', }); xhr.onload = function () { if (xhr.status === 200) { try { const response = JSON.parse(xhr.responseText); if (response.success) { // Update the global nonces window.googleOneTapAjax.nonces = response.data; // eslint-disable-next-line no-console console.log('Fresh nonces received:', response.data); resolve(response.data); } else { reject(new Error(response.data || 'Failed to get fresh nonces')); } } catch (e) { reject(new Error(`Error parsing nonces response: ${e.message}`)); } } else { reject(new Error(`AJAX request failed with status: ${xhr.status}`)); } }; xhr.onerror = function () { reject(new Error('AJAX request failed')); }; xhr.send(data); }); } // Update logout strip with user information function updateLogoutStrip(user) { const strip = document.getElementById('unified-strip'); if (!strip) return; // eslint-disable-next-line no-console console.log('Updating unified strip for user:', user); // Update to logout mode strip.innerHTML = `
`; // Add logout nonce strip.setAttribute('data-logout-nonce', window.googleOneTapAjax.nonces.logout); // Add logged-in class to body to show the strip document.body.classList.add('logged-in'); document.body.classList.remove('logged-out'); // eslint-disable-next-line no-console console.log('Unified strip updated to logout mode'); } // Update Disqus configuration for logged-in user function updateDisqusConfiguration(user) { // eslint-disable-next-line no-console console.log('Updating Disqus configuration for user:', user); // Get fresh Disqus config fetchDisqusConfig().then((config) => { // eslint-disable-next-line no-console console.log('Disqus config received:', config); if (config && config.user_logged_in) { // User is logged in, set up SSO authentication if (typeof window.DISQUS !== 'undefined') { // Disqus is already loaded, update it directly // eslint-disable-next-line no-console console.log('Reloading existing Disqus...'); window.DISQUS.reset({ reload: true, config() { this.page.identifier = window.location.pathname; this.page.url = window.location.href; this.page.remote_auth_s3 = config.remote_auth_s3; this.page.api_key = config.api_key; }, }); } else { // Disqus not loaded yet, set up configuration // eslint-disable-next-line no-console console.log('Disqus loaded but SSO not configured, setting up now...'); window.disqus_config = function () { this.page.identifier = window.location.pathname; this.page.url = window.location.href; this.page.remote_auth_s3 = config.remote_auth_s3; this.page.api_key = config.api_key; }; } } else { // User is not logged in, set up login configuration setupDisqusSSOLogin(config); } }).catch((error) => { // eslint-disable-next-line no-console console.error('Failed to update Disqus configuration:', error); }); } // Remove login overlays function removeLoginOverlays() { // Remove any existing login overlays const overlays = document.querySelectorAll('.disqus-login-overlay'); overlays.forEach((overlay) => overlay.remove()); } // Handle logout function handleLogout(event) { event.preventDefault(); // eslint-disable-next-line no-console console.log('Logout initiated'); const xhr = new XMLHttpRequest(); xhr.open('POST', window.googleOneTapAjax.ajax_url, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); const data = new URLSearchParams({ action: 'google_one_tap_logout', nonce: window.googleOneTapAjax.nonces.logout, }); xhr.onload = function () { if (xhr.status === 200) { try { const response = JSON.parse(xhr.responseText); if (response.success) { // eslint-disable-next-line no-console console.log('Logout successful'); // Trigger custom event for seamless logout const logoutEvent = new CustomEvent('userLoggedOut'); document.dispatchEvent(logoutEvent); } else { // eslint-disable-next-line no-console console.error('Logout failed:', response.data); // eslint-disable-next-line no-alert alert(`Logout failed: ${response.data || 'Unknown error'}`); } } catch (e) { // eslint-disable-next-line no-console console.error('Error parsing logout response:', e); // eslint-disable-next-line no-alert alert('Logout failed: Error parsing response'); } } else { // eslint-disable-next-line no-console console.error('Logout request failed with status:', xhr.status); // eslint-disable-next-line no-alert alert('Logout failed: Request failed'); } }; xhr.onerror = function () { // eslint-disable-next-line no-console console.error('Logout request failed'); // eslint-disable-next-line no-alert alert('Logout failed: Network error'); }; xhr.send(data); } // Remove Disqus configuration (for logout) function removeDisqusConfiguration() { if (typeof window.DISQUS !== 'undefined') { // Clear SSO configuration and force reload window.DISQUS.reset({ reload: true, config() { this.page.identifier = window.location.pathname; this.page.url = window.location.href; this.sso = undefined; this.page.remote_auth_s3 = undefined; this.page.api_key = undefined; }, }); } else { // Set up configuration for when Disqus loads window.disqus_config = function () { this.page.identifier = window.location.pathname; this.page.url = window.location.href; this.sso = undefined; this.page.remote_auth_s3 = undefined; this.page.api_key = undefined; }; } } // Make functions globally accessible window.handleLogout = handleLogout; window.removeDisqusConfiguration = removeDisqusConfiguration; // Listen for successful login events document.addEventListener('userLoggedIn', (loginEvent) => { // eslint-disable-next-line no-console console.log('Seamless login detected, updating UI...'); // eslint-disable-next-line no-console console.log('User data received:', loginEvent.detail.user); // Get fresh nonces first getFreshNonces().then(() => { // Update logout strip updateLogoutStrip(loginEvent.detail.user); // Update Disqus configuration updateDisqusConfiguration(loginEvent.detail.user); // Remove login overlays removeLoginOverlays(); }).catch((error) => { // eslint-disable-next-line no-console console.error('Failed to get fresh nonces:', error); // Continue with updates even if nonce refresh fails updateLogoutStrip(loginEvent.detail.user); updateDisqusConfiguration(loginEvent.detail.user); removeLoginOverlays(); }); }); // Listen for logout events document.addEventListener('userLoggedOut', () => { // eslint-disable-next-line no-console console.log('User logged out, updating Disqus configuration...'); // Force Disqus to reload after logout to show login state if (window.DISQUS) { // eslint-disable-next-line no-console console.log('Forcing Disqus to reload after logout event...'); try { // First remove any existing Disqus configuration const existingScripts = document.querySelectorAll('script'); existingScripts.forEach((script) => { if (script.textContent && script.textContent.includes('disqus_config')) { script.remove(); } }); // Small delay to ensure configuration is properly cleared setTimeout(() => { // Force Disqus to reload without SSO window.DISQUS.reset({ reload: true, config() { // Clear all authentication - this is crucial for logout this.page.remote_auth_s3 = undefined; this.page.api_key = undefined; this.sso = undefined; // Force Disqus to treat this as a non-authenticated session this.page.identifier = undefined; this.page.url = undefined; }, }); // eslint-disable-next-line no-console console.log('Disqus reloaded after logout event with cleared authentication'); }, 100); } catch (error) { // eslint-disable-next-line no-console console.error('Failed to reload Disqus after logout event:', error); } } else { // Fallback: remove configuration manually removeDisqusConfiguration(); } // Update strip back to login mode (but hide it) const strip = document.getElementById('unified-strip'); if (strip) { // Get login URL fetch(`${window.googleOneTapAjax.ajax_url}?action=get_login_url`) .then((response) => response.json()) .then((data) => { if (data.success) { strip.innerHTML = ` `; strip.removeAttribute('data-logout-nonce'); // eslint-disable-next-line no-console console.log('Unified strip updated back to login mode'); } }) .catch((error) => { // eslint-disable-next-line no-console console.error('Failed to get login URL:', error); }); } // Remove logged-in class to hide the strip document.body.classList.remove('logged-in'); document.body.classList.add('logged-out'); // Reinitialize Google One Tap // eslint-disable-next-line no-console console.log('Reinitializing Google One Tap after logout...'); if (typeof window.initGoogleOneTap === 'function') { // Get fresh nonces first getFreshNonces().then(() => { window.initGoogleOneTap(); }).catch((error) => { // eslint-disable-next-line no-console console.error('Failed to get fresh nonces for reinit:', error); window.initGoogleOneTap(); }); } }); // Initialize Disqus configuration when page loads document.addEventListener('DOMContentLoaded', () => { // Set up initial Disqus configuration for non-logged-in users if (!document.body.classList.contains('logged-in')) { // eslint-disable-next-line no-console console.log('Initializing Disqus configuration for non-logged-in user...'); // Set up configuration immediately (in case Disqus loads before our script) fetchDisqusConfig().then((config) => { if (config && !config.user_logged_in) { // eslint-disable-next-line no-console console.log('Setting up initial Disqus SSO login configuration...'); setupDisqusSSOLogin(config); } }).catch((error) => { // eslint-disable-next-line no-console console.error('Failed to set up initial Disqus config:', error); }); // Also use the monitoring approach for when Disqus loads waitForDisqusAndConfigure(); } }); }());