From bb26d9a80dd03deefe14097406271c142cb87c80 Mon Sep 17 00:00:00 2001 From: overcuriousity Date: Fri, 25 Jul 2025 13:37:52 +0200 Subject: [PATCH] consolidation --- src/components/ToolFilters.astro | 8 -- src/components/ToolMatrix.astro | 1 - src/env.d.ts | 6 ++ src/layouts/BaseLayout.astro | 180 +++++++++++++++++++++++++++++-- src/scripts/client-auth.js | 86 --------------- src/scripts/theme.js | 64 ----------- 6 files changed, 176 insertions(+), 169 deletions(-) delete mode 100644 src/scripts/client-auth.js delete mode 100644 src/scripts/theme.js diff --git a/src/components/ToolFilters.astro b/src/components/ToolFilters.astro index 043da05..4095eff 100644 --- a/src/components/ToolFilters.astro +++ b/src/components/ToolFilters.astro @@ -145,14 +145,6 @@ const sortedTags = Object.entries(tagFrequency) let selectedPhase = ''; let isTagCloudExpanded = false; - // Check authentication status and show/hide AI button - async function initAIButton() { - await showIfAuthenticated('#ai-view-toggle'); - } - - // Call auth check on page load - initAIButton(); - // Initialize tag cloud state function initTagCloud() { const visibleCount = 22; diff --git a/src/components/ToolMatrix.astro b/src/components/ToolMatrix.astro index 79faed3..17c10ce 100644 --- a/src/components/ToolMatrix.astro +++ b/src/components/ToolMatrix.astro @@ -832,5 +832,4 @@ domains.forEach((domain: any) => { } } }); - setupContributionButtonAuth(); \ No newline at end of file diff --git a/src/env.d.ts b/src/env.d.ts index ac7b42e..8a3c74c 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -23,6 +23,12 @@ declare global { createToolSlug: (toolName: string) => string; findToolByIdentifier: (tools: any[], identifier: string) => any | undefined; isToolHosted: (tool: any) => boolean; + + // CONSOLIDATED: Auth utility functions (now in BaseLayout) + checkClientAuth: () => Promise<{authenticated: boolean; authRequired: boolean; expires?: string}>; + requireClientAuth: (callback?: () => void, returnUrl?: string) => Promise; + showIfAuthenticated: (selector: string) => Promise; + setupAuthButtons: (selector?: string) => void; } } diff --git a/src/layouts/BaseLayout.astro b/src/layouts/BaseLayout.astro index 66956bb..99cfe0d 100644 --- a/src/layouts/BaseLayout.astro +++ b/src/layouts/BaseLayout.astro @@ -20,18 +20,178 @@ const { title, description = 'CC24-Guide - A comprehensive directory of digital {title} - CC24-Guide - - - - - - - - - diff --git a/src/scripts/client-auth.js b/src/scripts/client-auth.js deleted file mode 100644 index f4942ef..0000000 --- a/src/scripts/client-auth.js +++ /dev/null @@ -1,86 +0,0 @@ -// src/scripts/client-auth.js - CONSOLIDATED client-side auth utilities -// This file REPLACES auth-utils.js and any client-side auth functions - -/** - * Check authentication status - */ -async function checkClientAuth() { - try { - const response = await fetch('/api/auth/status'); - const data = await response.json(); - return { - authenticated: data.authenticated, - authRequired: data.authRequired, - expires: data.expires - }; - } catch (error) { - console.error('Auth check failed:', error); - return { - authenticated: false, - authRequired: true - }; - } -} - -/** - * Redirect to login if not authenticated, otherwise execute callback - */ -async function requireClientAuth(callback, returnUrl) { - const authStatus = await checkClientAuth(); - - if (authStatus.authRequired && !authStatus.authenticated) { - const targetUrl = returnUrl || window.location.href; - window.location.href = `/api/auth/login?returnTo=${encodeURIComponent(targetUrl)}`; - return false; - } else { - if (typeof callback === 'function') { - callback(); - } - return true; - } -} - -/** - * Show/hide element based on authentication - */ -async function showIfAuthenticated(selector) { - const authStatus = await checkClientAuth(); - const element = document.querySelector(selector); - - if (element) { - element.style.display = (!authStatus.authRequired || authStatus.authenticated) - ? 'inline-flex' - : 'none'; - } -} -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 for dynamic content -window.checkClientAuth = checkClientAuth; -window.requireClientAuth = requireClientAuth; -window.showIfAuthenticated = showIfAuthenticated; -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]'); -}); - -console.log('Client auth utilities loaded'); \ No newline at end of file diff --git a/src/scripts/theme.js b/src/scripts/theme.js deleted file mode 100644 index b7184cd..0000000 --- a/src/scripts/theme.js +++ /dev/null @@ -1,64 +0,0 @@ -// Theme management -const THEME_KEY = 'dfir-theme'; - -// Get system preference -function getSystemTheme() { - return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; -} - -// Get stored theme or default to auto -function getStoredTheme() { - return localStorage.getItem(THEME_KEY) || 'auto'; -} - -// Apply theme to document -function applyTheme(theme) { - const effectiveTheme = theme === 'auto' ? getSystemTheme() : theme; - document.documentElement.setAttribute('data-theme', effectiveTheme); -} - -// Update theme toggle button state -function updateThemeToggle(theme) { - document.querySelectorAll('[data-theme-toggle]').forEach(button => { - button.setAttribute('data-current-theme', theme); - }); -} - -// Initialize theme on page load -function initTheme() { - const storedTheme = getStoredTheme(); - applyTheme(storedTheme); - - // Update theme toggle buttons immediately - updateThemeToggle(storedTheme); -} - -// Handle theme toggle -function toggleTheme() { - const current = getStoredTheme(); - const themes = ['light', 'dark', 'auto']; - const currentIndex = themes.indexOf(current); - const nextIndex = (currentIndex + 1) % themes.length; - const nextTheme = themes[nextIndex]; - - localStorage.setItem(THEME_KEY, nextTheme); - applyTheme(nextTheme); - updateThemeToggle(nextTheme); -} - -// Listen for system theme changes -window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { - if (getStoredTheme() === 'auto') { - applyTheme('auto'); - } -}); - -// Initialize when DOM is ready (for safety) -document.addEventListener('DOMContentLoaded', initTheme); - -// Export functions for use in Astro components -window.themeUtils = { - initTheme, - toggleTheme, - getStoredTheme -}; \ No newline at end of file