From da151681e1ca00ae9a0f16d1c41ecdbac4e9bf32 Mon Sep 17 00:00:00 2001 From: mwpn Date: Thu, 18 Dec 2025 11:41:47 +0700 Subject: [PATCH] Fix redirect loop: hapus auto-redirect di index.php, hanya redirect setelah login berhasil --- public/dashboard/js/api.js | 3 +-- public/dashboard/js/auth.js | 16 ++-------------- public/dashboard/js/dashboard.js | 8 ++------ public/index.php | 23 ++--------------------- 4 files changed, 7 insertions(+), 43 deletions(-) diff --git a/public/dashboard/js/api.js b/public/dashboard/js/api.js index ef7ef9f..3f409aa 100644 --- a/public/dashboard/js/api.js +++ b/public/dashboard/js/api.js @@ -40,10 +40,9 @@ async function apiRequest(path, options = {}) { // Unauthorized → clear token & redirect to login localStorage.removeItem('token'); localStorage.removeItem('user'); - sessionStorage.removeItem('auth_redirect_done'); // Cek apakah sudah di login page untuk menghindari redirect loop const currentPath = window.location.pathname; - const isLoginPage = currentPath.includes('index.php'); + const isLoginPage = currentPath.includes('index.php') || currentPath === '/' || currentPath.endsWith('/'); if (!isLoginPage) { window.location.href = '../index.php'; } diff --git a/public/dashboard/js/auth.js b/public/dashboard/js/auth.js index 3e9443e..79e0c72 100644 --- a/public/dashboard/js/auth.js +++ b/public/dashboard/js/auth.js @@ -73,23 +73,11 @@ async function handleLoginSubmit(event) { } // Attach events on login page only +// Hapus auto-redirect untuk mencegah redirect loop +// Redirect hanya setelah login berhasil (di handleLoginSubmit) document.addEventListener('DOMContentLoaded', () => { const form = document.getElementById('login-form'); if (form) { - // Cek apakah sudah authenticated dan belum di dashboard untuk menghindari redirect loop - // Hanya redirect jika benar-benar di login page (bukan dashboard) - const currentPath = window.location.pathname; - const isLoginPage = currentPath.includes('index.php') || (currentPath.endsWith('/') && !currentPath.includes('dashboard')); - const isDashboardPage = currentPath.includes('dashboard.html') || currentPath.includes('event.html') || currentPath.includes('settings.html'); - - if (Auth.isAuthenticated() && isLoginPage && !isDashboardPage) { - const redirectKey = 'auth_redirect_done'; - if (!sessionStorage.getItem(redirectKey)) { - sessionStorage.setItem(redirectKey, '1'); - window.location.href = 'dashboard.html'; - return; - } - } form.addEventListener('submit', handleLoginSubmit); } }); diff --git a/public/dashboard/js/dashboard.js b/public/dashboard/js/dashboard.js index fdd3174..7e4a0ff 100644 --- a/public/dashboard/js/dashboard.js +++ b/public/dashboard/js/dashboard.js @@ -697,17 +697,13 @@ document.addEventListener('DOMContentLoaded', async () => { if (!Auth.isAuthenticated()) { // Cek apakah sudah di login page untuk mencegah redirect loop const currentPath = window.location.pathname; - const isLoginPage = currentPath.includes('index.php'); + const isLoginPage = currentPath.includes('index.php') || currentPath === '/' || currentPath.endsWith('/'); if (!isLoginPage) { - // Clear redirect flag jika logout - sessionStorage.removeItem('auth_redirect_done'); + // Redirect ke login hanya jika belum di login page window.location.href = '../index.php'; } return; } - - // Clear redirect flag saat sudah di dashboard - sessionStorage.removeItem('auth_redirect_done'); // Set default date ke hari ini (jangan auto-detect ke tanggal lama) const today = new Date().toISOString().split('T')[0]; diff --git a/public/index.php b/public/index.php index 19bba72..3fa3a44 100644 --- a/public/index.php +++ b/public/index.php @@ -69,27 +69,8 @@ } from './dashboard/js/auth.js'; window.Auth = Auth; - // Jika sudah login, langsung arahkan ke dashboard - // Cek dulu apakah kita sudah di dashboard untuk menghindari redirect loop - (function() { - // Cek apakah ini benar-benar halaman index.php (bukan dashboard) - const currentPath = window.location.pathname; - const isIndexPage = currentPath.endsWith('index.php') || currentPath.endsWith('/') || currentPath === '/'; - const isDashboardPage = currentPath.includes('dashboard.html') || currentPath.includes('event.html') || currentPath.includes('settings.html'); - - // Hanya redirect jika: - // 1. User sudah authenticated - // 2. Kita di index page (bukan dashboard) - // 3. Belum pernah redirect (cek sessionStorage) - if (Auth.isAuthenticated() && isIndexPage && !isDashboardPage) { - const redirectKey = 'auth_redirect_done'; - if (!sessionStorage.getItem(redirectKey)) { - sessionStorage.setItem(redirectKey, '1'); - window.location.href = 'dashboard/dashboard.html'; - return; - } - } - })(); + // Hapus auto-redirect untuk mencegah redirect loop + // Biarkan user login dulu, redirect hanya setelah login berhasil document.getElementById('loginForm').addEventListener('submit', async (e) => { e.preventDefault();