let loggedInUser = false; const checkIntervalDuration = 1000; function getRoleFromCookie() { const name = "role="; const decodedCookie = decodeURIComponent(document.cookie); const cookieArray = decodedCookie.split(';'); for(let i = 0; i { const lastCheck = localStorage.getItem('lastCheck'); const timeSinceLastCheck = Date.now() - (lastCheck ? parseInt(lastCheck) : 0); if (timeSinceLastCheck >= checkIntervalDuration) { checkCookieAndUpdateUI(); } }, 10); } } function deleteCookie(name) { document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; } function resetAuthState() { loggedInUser = false; const authButton = document.getElementById('authButton'); authButton.style.display = 'none'; localStorage.removeItem('lastCheck'); clearInterval(window.cookieCheckInterval); window.cookieCheckInterval = null; } window.addEventListener('load',function() { const wasRefreshed=sessionStorage.getItem("refreshed"); if(wasRefreshed){ //deleteCookie("username"); sessionStorage.removeItem("refreshed") if(getRoleFromCookie()==="ADMIN"){ window.location.href="/admin.html"; } else{ window.location.href="/"; } } else{ sessionStorage.setItem("refreshed","true") } checkCookieAndUpdateUI(); startCookieCheckInterval(); let authButton=document.getElementById("authButton"); authButton.addEventListener('click', () => { if (authButton.innerText === 'Logout') { deleteCookie('username'); deleteCookie('role'); resetAuthState(); window.location.href="/"; } }); }); window.addEventListener("beforeunload", function() { sessionStorage.removeItem("refreshed"); });