-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
67 lines (56 loc) · 2.08 KB
/
Copy pathauth.js
File metadata and controls
67 lines (56 loc) · 2.08 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* ══════════════════════════════════════════════════════════
* Next-Gen IT · auth.js
* Shared session-auth utility used by the portal.
*
* Include this script before protected portal/app JavaScript.
* It redirects unauthenticated users to the portal login page.
* ══════════════════════════════════════════════════════════
*/
(function () {
'use strict';
const SESSION_KEY = 'ngit_portal_auth';
const LEGACY_SESSION_KEY = 'ngit_auth';
function getLoginUrl() {
const path = window.location.pathname || '';
return path.includes('/portal/') ? './login.html' : './portal/login.html';
}
function markAuthenticated() {
sessionStorage.setItem(SESSION_KEY, '1');
sessionStorage.setItem(LEGACY_SESSION_KEY, '1');
}
/**
* Call at the top of protected pages to guard the portal.
* Redirects to login.html if not authenticated.
*/
function requireAuth() {
const current = sessionStorage.getItem(SESSION_KEY) === '1';
const legacy = sessionStorage.getItem(LEGACY_SESSION_KEY) === '1';
if (legacy && !current) {
sessionStorage.setItem(SESSION_KEY, '1');
return;
}
if (!current) {
window.location.replace(getLoginUrl());
}
}
/**
* Sign the current session out and redirect to login.
* Wire to a logout button: Auth.logout()
*/
function logout() {
sessionStorage.removeItem(SESSION_KEY);
sessionStorage.removeItem(LEGACY_SESSION_KEY);
window.location.replace(getLoginUrl());
}
/**
* Returns true if the session is authenticated.
*/
function isAuthenticated() {
return sessionStorage.getItem(SESSION_KEY) === '1' || sessionStorage.getItem(LEGACY_SESSION_KEY) === '1';
}
// Expose public API
window.Auth = { requireAuth, logout, isAuthenticated, markAuthenticated };
// Auto-guard: if this script is loaded, enforce auth immediately.
requireAuth();
})();