/** * Admin Authentication Check Utility * Protects admin pages by redirecting unauthenticated users to login * * Usage: Include at top of every admin page HTML: * */ (function() { 'use strict'; // Skip auth check on login page itself if (window.location.pathname === '/admin/login.html') { return; } /** * Check if user has valid authentication token */ function checkAuthentication() { const token = localStorage.getItem('admin_token'); // No token found - redirect to login if (!token) { redirectToLogin('No authentication token found'); return false; } // Parse token to check expiration try { const payload = parseJWT(token); const now = Math.floor(Date.now() / 1000); // Token expired - redirect to login if (payload.exp && payload.exp < now) { localStorage.removeItem('admin_token'); redirectToLogin('Session expired'); return false; } // Check if admin role if (payload.role !== 'admin' && payload.role !== 'moderator') { redirectToLogin('Insufficient permissions'); return false; } // Token valid return true; } catch (error) { console.error('Token validation error:', error); localStorage.removeItem('admin_token'); redirectToLogin('Invalid authentication token'); return false; } } /** * Parse JWT token without verification (client-side validation only) */ function parseJWT(token) { const parts = token.split('.'); if (parts.length !== 3) { throw new Error('Invalid token format'); } const payload = parts[1]; const decoded = atob(payload.replace(/-/g, '+').replace(/_/g, '/')); return JSON.parse(decoded); } /** * Redirect to login page with reason */ function redirectToLogin(reason) { const currentPath = encodeURIComponent(window.location.pathname + window.location.search); const loginUrl = `/admin/login.html?redirect=${currentPath}&reason=${encodeURIComponent(reason)}`; // Show brief message before redirect document.body.innerHTML = `

Authentication Required

${reason}

Redirecting to login...

`; setTimeout(() => { window.location.href = loginUrl; }, 1500); } /** * Add authentication headers to fetch requests */ function getAuthHeaders() { const token = localStorage.getItem('admin_token'); return { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }; } /** * Handle API authentication errors */ function handleAuthError(response) { if (response.status === 401 || response.status === 403) { localStorage.removeItem('admin_token'); redirectToLogin('Session expired or invalid'); return true; } return false; } // Run authentication check immediately checkAuthentication(); // Export utilities for admin pages to use window.AdminAuth = { getAuthHeaders, handleAuthError, checkAuthentication, redirectToLogin }; // Periodically check token validity (every 5 minutes) setInterval(checkAuthentication, 5 * 60 * 1000); })();