-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
34 lines (28 loc) · 1.15 KB
/
auth.js
File metadata and controls
34 lines (28 loc) · 1.15 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
console.log("Current page:", window.location.pathname.split('/').pop());
document.addEventListener('DOMContentLoaded', function() {
const publicPages = ['Login.html', 'Register.html'];
const currentPage = window.location.pathname.split('/').pop();
// Check both sessionStorage and localStorage
const isAuthenticated = sessionStorage.getItem('isAuthenticated') === 'true' ||
localStorage.getItem('isAuthenticated') === 'true';
if (publicPages.includes(currentPage)) {
// If on login page but already authenticated, redirect to dashboard
if (isAuthenticated) {
window.location.href = 'Index.html';
}
} else {
// For protected pages, require authentication
if (!isAuthenticated) {
window.location.href = 'Login.html';
}
}
});
function logout() {
// Clear both storage mechanisms
sessionStorage.removeItem('isAuthenticated');
localStorage.removeItem('isAuthenticated');
sessionStorage.removeItem('userRole');
localStorage.removeItem('userRole');
// Redirect to login page
window.location.href = 'Login.html';
}