diff --git a/public/dashboard/dashboard.html b/public/dashboard/dashboard.html index 1a328ca..f3ca703 100644 --- a/public/dashboard/dashboard.html +++ b/public/dashboard/dashboard.html @@ -173,9 +173,27 @@ import './js/dashboard.js'; import './js/realtime.js'; - document.getElementById('logout-button')?.addEventListener('click', () => { - Auth.logout(); - }); + // Setup logout button - pastikan ter-attach dengan benar + function setupLogoutButton() { + const logoutBtn = document.getElementById('logout-button'); + if (logoutBtn) { + console.log('[Dashboard] Logout button found, attaching event listener'); + logoutBtn.addEventListener('click', (e) => { + e.preventDefault(); + console.log('[Dashboard] Logout button clicked'); + Auth.logout(); + }); + } else { + console.warn('[Dashboard] Logout button not found!'); + } + } + + // Setup saat DOM ready + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', setupLogoutButton); + } else { + setupLogoutButton(); + } diff --git a/public/dashboard/event.html b/public/dashboard/event.html index afb1d33..8fe4cf9 100644 --- a/public/dashboard/event.html +++ b/public/dashboard/event.html @@ -248,10 +248,27 @@ } } - // Logout handler - document.getElementById('logout-button')?.addEventListener('click', () => { - Auth.logout(); - }); + // Logout handler - pastikan ter-attach dengan benar + function setupLogoutButton() { + const logoutBtn = document.getElementById('logout-button'); + if (logoutBtn) { + console.log('[Events] Logout button found, attaching event listener'); + logoutBtn.addEventListener('click', (e) => { + e.preventDefault(); + console.log('[Events] Logout button clicked'); + Auth.logout(); + }); + } else { + console.warn('[Events] Logout button not found!'); + } + } + + // Setup saat DOM ready + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', setupLogoutButton); + } else { + setupLogoutButton(); + } // Video HLS setup - menggunakan URL kamera dari database let gatesCache = {}; // Cache untuk gates dengan camera URL diff --git a/public/dashboard/js/auth.js b/public/dashboard/js/auth.js index c0d6170..83ec570 100644 --- a/public/dashboard/js/auth.js +++ b/public/dashboard/js/auth.js @@ -20,10 +20,33 @@ export const Auth = { }, logout() { + console.log('[Auth] Logout called'); localStorage.removeItem(TOKEN_KEY); localStorage.removeItem(USER_KEY); sessionStorage.removeItem('auth_redirect_done'); - window.location.href = '../index.html'; + + // Deteksi path yang benar untuk redirect + const currentPath = window.location.pathname; + let loginPath = '../index.html'; + + // Jika di dashboard/, gunakan ../index.html + // Jika di root, gunakan index.html + if (currentPath.includes('/dashboard/')) { + loginPath = '../index.html'; + } else if (currentPath.endsWith('/') || currentPath === '/') { + loginPath = 'index.html'; + } else { + // Fallback: coba detect dari current path + const pathParts = currentPath.split('/').filter(p => p); + if (pathParts.length > 0 && pathParts[pathParts.length - 1].includes('dashboard')) { + loginPath = '../index.html'; + } else { + loginPath = 'index.html'; + } + } + + console.log('[Auth] Redirecting to:', loginPath, 'from:', currentPath); + window.location.href = loginPath; } };