diff --git a/README.md b/README.md index d5f6573..e939060 100644 --- a/README.md +++ b/README.md @@ -63,15 +63,18 @@ Deploy ke web server (Apache/Nginx) dengan konfigurasi: ### URL Rewrite (Opsional) **TIDAK PERLU URL rewrite** untuk aplikasi ini karena: + - Semua file HTML bisa diakses langsung (`dashboard.html`, `event.html`, `settings.html`) - Tidak ada clean URLs atau SPA routing - Struktur file sudah jelas dan mudah diakses **Jika ingin clean URLs** (misalnya `/dashboard` tanpa `.html`), bisa gunakan: + - **Apache**: File `.htaccess` sudah disediakan (uncomment bagian clean URLs) - **Nginx**: Gunakan `nginx.conf.example` sebagai referensi File konfigurasi: + - `.htaccess` - Apache URL rewrite (opsional, sudah include security headers & cache) - `nginx.conf.example` - Nginx configuration example diff --git a/index.php b/index.php index 6b1dd65..1bdf556 100644 --- a/index.php +++ b/index.php @@ -70,10 +70,26 @@ // Jika sudah login, langsung arahkan ke dashboard utama (public/dashboard) // Cek dulu apakah kita sudah di dashboard untuk menghindari redirect loop - const currentPath = window.location.pathname; - if (Auth.isAuthenticated() && !currentPath.includes('dashboard.html')) { - window.location.href = 'public/dashboard/dashboard.html'; - } + // Gunakan check yang lebih spesifik untuk mencegah 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 = 'public/dashboard/dashboard.html'; + return; + } + } + })(); document.getElementById('loginForm').addEventListener('submit', async (e) => { e.preventDefault(); diff --git a/public/dashboard/js/api.js b/public/dashboard/js/api.js index 072f65d..ef7ef9f 100644 --- a/public/dashboard/js/api.js +++ b/public/dashboard/js/api.js @@ -40,9 +40,11 @@ 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; - if (!currentPath.includes('index.php')) { + const isLoginPage = currentPath.includes('index.php'); + if (!isLoginPage) { window.location.href = '../index.php'; } throw new Error('Unauthorized'); diff --git a/public/dashboard/js/auth.js b/public/dashboard/js/auth.js index 7c632d8..b3d6f00 100644 --- a/public/dashboard/js/auth.js +++ b/public/dashboard/js/auth.js @@ -76,10 +76,18 @@ 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; - if (Auth.isAuthenticated() && !currentPath.includes('dashboard')) { - window.location.href = 'dashboard.html'; - return; + 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 346a8ae..fdd3174 100644 --- a/public/dashboard/js/dashboard.js +++ b/public/dashboard/js/dashboard.js @@ -695,9 +695,19 @@ function initCharts() { document.addEventListener('DOMContentLoaded', async () => { // Require auth if (!Auth.isAuthenticated()) { - window.location.href = '../index.php'; + // Cek apakah sudah di login page untuk mencegah redirect loop + const currentPath = window.location.pathname; + const isLoginPage = currentPath.includes('index.php'); + if (!isLoginPage) { + // Clear redirect flag jika logout + sessionStorage.removeItem('auth_redirect_done'); + 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];