From 1f33ad1b16cba455705aa1cf82795929e8f87dc6 Mon Sep 17 00:00:00 2001 From: overcuriousity Date: Thu, 24 Jul 2025 20:40:17 +0200 Subject: [PATCH] auth system consolidation --- src/components/ContributionButton.astro | 33 ++----------------------- src/components/ToolMatrix.astro | 27 -------------------- src/pages/index.astro | 11 ++++----- src/scripts/client-auth.js | 13 ++++++---- 4 files changed, 15 insertions(+), 69 deletions(-) diff --git a/src/components/ContributionButton.astro b/src/components/ContributionButton.astro index ea8a041..79a0218 100644 --- a/src/components/ContributionButton.astro +++ b/src/components/ContributionButton.astro @@ -1,5 +1,5 @@ --- -// src/components/ContributionButton.astro +// src/components/ContributionButton.astro - CLEANED: Removed duplicate auth script export interface Props { type: 'edit' | 'new' | 'write'; toolName?: string; @@ -69,33 +69,4 @@ const iconSize = variant === 'small' ? '14' : '16'; {displayText} - - - \ No newline at end of file + \ No newline at end of file diff --git a/src/components/ToolMatrix.astro b/src/components/ToolMatrix.astro index c4be5fc..d9bc440 100644 --- a/src/components/ToolMatrix.astro +++ b/src/components/ToolMatrix.astro @@ -852,32 +852,5 @@ domains.forEach((domain: any) => { } } }); - document.addEventListener('DOMContentLoaded', () => { - // Auth check for contribution buttons (similar to existing auth checking pattern) - function setupContributionButtonAuth() { - document.addEventListener('click', async (e) => { - const contributeButton = e.target.closest('[data-contribute-button]'); - if (!contributeButton) return; - - e.preventDefault(); - - try { - const response = await fetch('/api/auth/status'); - const data = await response.json(); - - if (data.authRequired && !data.authenticated) { - const returnUrl = contributeButton.href; - window.location.href = `/api/auth/login?returnTo=${encodeURIComponent(returnUrl)}`; - } else { - window.location.href = contributeButton.href; - } - } catch (error) { - console.error('Auth check failed:', error); - window.location.href = contributeButton.href; - } - }); - } - setupContributionButtonAuth(); - }); \ No newline at end of file diff --git a/src/pages/index.astro b/src/pages/index.astro index 7f364de..6984fd5 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -53,7 +53,7 @@ const tools = data.tools; - + @@ -143,16 +143,15 @@ const tools = data.tools; return sorted; } } - - // FIXED: AI Query Button Handler using global client-side auth function + // OPTIMIZED: AI Query Button Handler using consolidated auth system if (aiQueryBtn) { aiQueryBtn.addEventListener('click', async () => { - // Wait for client-side auth functions to be available + // Use the global auth system consistently if (typeof window.requireClientAuth === 'function') { await window.requireClientAuth(() => switchToView('ai'), `${window.location.pathname}?view=ai`); } else { - console.error('requireClientAuth not available - client-auth.js may not be loaded'); - // Fallback - try switching anyway + // Better fallback logging + console.warn('[AUTH] requireClientAuth not available - client-auth.js may not be loaded properly'); switchToView('ai'); } }); diff --git a/src/scripts/client-auth.js b/src/scripts/client-auth.js index 2f900eb..f4942ef 100644 --- a/src/scripts/client-auth.js +++ b/src/scripts/client-auth.js @@ -53,23 +53,25 @@ async function showIfAuthenticated(selector) { : 'none'; } } - -/** - * Handle contribute button clicks with auth check - */ function setupAuthButtons(selector = '[data-contribute-button]') { + // Use event delegation on document for dynamic content support document.addEventListener('click', async (e) => { const button = e.target.closest(selector); if (!button) return; e.preventDefault(); + + // Enhanced error handling and debugging + console.log('[AUTH] Contribute button clicked:', button.getAttribute('data-contribute-button')); + await requireClientAuth(() => { + console.log('[AUTH] Navigation approved, redirecting to:', button.href); window.location.href = button.href; }, button.href); }); } -// Make functions available globally +// Make functions available globally for dynamic content window.checkClientAuth = checkClientAuth; window.requireClientAuth = requireClientAuth; window.showIfAuthenticated = showIfAuthenticated; @@ -77,6 +79,7 @@ window.setupAuthButtons = setupAuthButtons; // Auto-setup contribute buttons when DOM is ready document.addEventListener('DOMContentLoaded', () => { + console.log('[AUTH] Setting up global auth handlers for contribute buttons'); setupAuthButtons('[data-contribute-button]'); });