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,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];
|
||||
|
||||
Reference in New Issue
Block a user