Fix redirect loop: hapus auto-redirect di index.php, hanya redirect setelah login berhasil

This commit is contained in:
mwpn
2025-12-18 11:41:47 +07:00
parent ef51cf96a2
commit da151681e1
4 changed files with 7 additions and 43 deletions

View File

@@ -40,10 +40,9 @@ async function apiRequest(path, options = {}) {
// Unauthorized → clear token & redirect to login // Unauthorized → clear token & redirect to login
localStorage.removeItem('token'); localStorage.removeItem('token');
localStorage.removeItem('user'); localStorage.removeItem('user');
sessionStorage.removeItem('auth_redirect_done');
// Cek apakah sudah di login page untuk menghindari redirect loop // Cek apakah sudah di login page untuk menghindari redirect loop
const currentPath = window.location.pathname; const currentPath = window.location.pathname;
const isLoginPage = currentPath.includes('index.php'); const isLoginPage = currentPath.includes('index.php') || currentPath === '/' || currentPath.endsWith('/');
if (!isLoginPage) { if (!isLoginPage) {
window.location.href = '../index.php'; window.location.href = '../index.php';
} }

View File

@@ -73,23 +73,11 @@ async function handleLoginSubmit(event) {
} }
// Attach events on login page only // Attach events on login page only
// Hapus auto-redirect untuk mencegah redirect loop
// Redirect hanya setelah login berhasil (di handleLoginSubmit)
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('login-form'); const form = document.getElementById('login-form');
if (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); form.addEventListener('submit', handleLoginSubmit);
} }
}); });

View File

@@ -697,17 +697,13 @@ document.addEventListener('DOMContentLoaded', async () => {
if (!Auth.isAuthenticated()) { if (!Auth.isAuthenticated()) {
// Cek apakah sudah di login page untuk mencegah redirect loop // Cek apakah sudah di login page untuk mencegah redirect loop
const currentPath = window.location.pathname; const currentPath = window.location.pathname;
const isLoginPage = currentPath.includes('index.php'); const isLoginPage = currentPath.includes('index.php') || currentPath === '/' || currentPath.endsWith('/');
if (!isLoginPage) { if (!isLoginPage) {
// Clear redirect flag jika logout // Redirect ke login hanya jika belum di login page
sessionStorage.removeItem('auth_redirect_done');
window.location.href = '../index.php'; window.location.href = '../index.php';
} }
return; 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) // Set default date ke hari ini (jangan auto-detect ke tanggal lama)
const today = new Date().toISOString().split('T')[0]; const today = new Date().toISOString().split('T')[0];

View File

@@ -69,27 +69,8 @@
} from './dashboard/js/auth.js'; } from './dashboard/js/auth.js';
window.Auth = Auth; window.Auth = Auth;
// Jika sudah login, langsung arahkan ke dashboard // Hapus auto-redirect untuk mencegah redirect loop
// Cek dulu apakah kita sudah di dashboard untuk menghindari redirect loop // Biarkan user login dulu, redirect hanya setelah login berhasil
(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;
}
}
})();
document.getElementById('loginForm').addEventListener('submit', async (e) => { document.getElementById('loginForm').addEventListener('submit', async (e) => {
e.preventDefault(); e.preventDefault();