Fix: perbaiki logout button handler dan path detection untuk konsistensi lokal dan produksi

This commit is contained in:
mwpn
2025-12-19 05:52:09 +07:00
parent de79ba0af2
commit 8447b05e90
3 changed files with 66 additions and 8 deletions

View File

@@ -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();
}
</script>
</body>
</html>

View File

@@ -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

View File

@@ -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;
}
};