-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionstorage.js
More file actions
40 lines (35 loc) · 1.28 KB
/
sessionstorage.js
File metadata and controls
40 lines (35 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
document.addEventListener('DOMContentLoaded', function () {
var userToken =
sessionStorage.getItem('token') || localStorage.getItem('token');
var currentPath = window.location.pathname;
// Normalize path by removing trailing slash
currentPath = currentPath.replace(/\/$/, '');
var loginPage = '/admin/index.html'; // Updated to correct login page path
var publicPage = '/';
var adminHomePage = '/admin/adminhome.html'; // Now under /admin
var isLoginPage = currentPath === loginPage;
var isProtectedAdminPage = currentPath.startsWith('/admin') && !isLoginPage;
if (userToken) {
console.log('✅ User is logged in.');
if (isLoginPage) {
console.log('🔄 Redirecting to admin home...');
window.location.href = adminHomePage;
}
} else {
console.log('❌ User not logged in.');
if (isProtectedAdminPage) {
console.log('🔄 Redirecting to login page...');
window.location.href = loginPage;
}
}
// Logout handler
var homeLink = document.getElementById('homelink');
if (homeLink) {
homeLink.addEventListener('click', function () {
sessionStorage.removeItem('token');
localStorage.removeItem('token');
console.log('🚪 Logged out: Token removed.');
window.location.href = publicPage;
});
}
});