1 | let loggedInUser = false;
|
---|
2 | const checkIntervalDuration = 1000;
|
---|
3 | function getRoleFromCookie() {
|
---|
4 | const name = "role=";
|
---|
5 | const decodedCookie = decodeURIComponent(document.cookie);
|
---|
6 | const cookieArray = decodedCookie.split(';');
|
---|
7 | for(let i = 0; i <cookieArray.length; i++) {
|
---|
8 | let cookie = cookieArray[i];
|
---|
9 | while (cookie.charAt(0) === ' ') {
|
---|
10 | cookie = cookie.substring(1);
|
---|
11 | }
|
---|
12 | if (cookie.indexOf(name) === 0) {
|
---|
13 | return cookie.substring(name.length, cookie.length);
|
---|
14 | }
|
---|
15 | }
|
---|
16 | return "";
|
---|
17 | }
|
---|
18 | function getCookie(name) {
|
---|
19 | const value = `; ${document.cookie}`;
|
---|
20 | const parts = value.split(`; ${name}=`);
|
---|
21 | if (parts.length === 2) return parts.pop().split(';').shift();
|
---|
22 | return undefined;
|
---|
23 | }
|
---|
24 | function checkCookieAndUpdateUI() {
|
---|
25 | const username = getCookie('username');
|
---|
26 | const userRole=getRoleFromCookie();
|
---|
27 | console.log(userRole);
|
---|
28 | const authButton = document.getElementById('authButton');
|
---|
29 |
|
---|
30 | if (username) {
|
---|
31 | if (!loggedInUser) {
|
---|
32 | loggedInUser = true;
|
---|
33 | authButton.innerText = 'Logout';
|
---|
34 | authButton.style.display = 'block';
|
---|
35 | localStorage.setItem('lastCheck', Date.now());
|
---|
36 | }
|
---|
37 | else{
|
---|
38 | if(window.location.pathname === '/'){
|
---|
39 | if(userRole==="ADMIN"){
|
---|
40 | window.location.href = '/admin.html';
|
---|
41 | }
|
---|
42 | else{
|
---|
43 | const personalisedSection=document.getElementById("personalised");
|
---|
44 | personalisedSection.innerHTML=`Добредојде, ${username}!`;
|
---|
45 | document.getElementsByClassName("logged-in")[0].style.display='block';
|
---|
46 | document.getElementById("adminSection").style.display='none';
|
---|
47 | }
|
---|
48 |
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | }
|
---|
53 | else {
|
---|
54 | if (loggedInUser) {
|
---|
55 | loggedInUser = false;
|
---|
56 | authButton.style.display = 'none';
|
---|
57 | // deleteCookie('username')
|
---|
58 | window.location.href = '/';
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 |
|
---|
65 | function startCookieCheckInterval() {
|
---|
66 | if (!window.cookieCheckInterval) {
|
---|
67 | window.cookieCheckInterval = setInterval(() => {
|
---|
68 | const lastCheck = localStorage.getItem('lastCheck');
|
---|
69 | const timeSinceLastCheck = Date.now() - (lastCheck ? parseInt(lastCheck) : 0);
|
---|
70 | if (timeSinceLastCheck >= checkIntervalDuration) {
|
---|
71 | checkCookieAndUpdateUI();
|
---|
72 | }
|
---|
73 | }, 10);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | function deleteCookie(name) {
|
---|
77 | document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
|
---|
78 | }
|
---|
79 | function resetAuthState() {
|
---|
80 | loggedInUser = false;
|
---|
81 | const authButton = document.getElementById('authButton');
|
---|
82 | authButton.style.display = 'none';
|
---|
83 | localStorage.removeItem('lastCheck');
|
---|
84 | clearInterval(window.cookieCheckInterval);
|
---|
85 | window.cookieCheckInterval = null;
|
---|
86 | }
|
---|
87 |
|
---|
88 | window.addEventListener('load',function() {
|
---|
89 |
|
---|
90 | const wasRefreshed=sessionStorage.getItem("refreshed");
|
---|
91 | if(wasRefreshed){
|
---|
92 | //deleteCookie("username");
|
---|
93 | sessionStorage.removeItem("refreshed")
|
---|
94 | if(getRoleFromCookie()==="ADMIN"){
|
---|
95 | window.location.href="/admin.html";
|
---|
96 | }
|
---|
97 | else{
|
---|
98 | window.location.href="/";
|
---|
99 | }
|
---|
100 | }
|
---|
101 | else{
|
---|
102 | sessionStorage.setItem("refreshed","true")
|
---|
103 | }
|
---|
104 | checkCookieAndUpdateUI();
|
---|
105 | startCookieCheckInterval();
|
---|
106 | let authButton=document.getElementById("authButton");
|
---|
107 | authButton.addEventListener('click', () => {
|
---|
108 | if (authButton.innerText === 'Logout') {
|
---|
109 | deleteCookie('username');
|
---|
110 | deleteCookie('role');
|
---|
111 | resetAuthState();
|
---|
112 | window.location.href="/";
|
---|
113 | }
|
---|
114 | });
|
---|
115 | });
|
---|
116 | window.addEventListener("beforeunload", function() {
|
---|
117 | sessionStorage.removeItem("refreshed");
|
---|
118 | }); |
---|