Fix redirect loop: hapus auto-redirect di index.php, hanya redirect setelah login berhasil
This commit is contained in:
@@ -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';
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -697,18 +697,14 @@ 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];
|
||||
state.date = today;
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user