-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth-guard.js
More file actions
37 lines (32 loc) · 1.01 KB
/
Copy pathauth-guard.js
File metadata and controls
37 lines (32 loc) · 1.01 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
/**
* Redirect guests away from app pages to the homepage intro (index.html).
*/
(function () {
const PUBLIC_PAGES = new Set(["index.html", "login.html", ""]);
function currentPage() {
const parts = window.location.pathname.split("/");
return parts[parts.length - 1] || "index.html";
}
function redirectGuest() {
if (!window.AuthManager || !window.AuthManager.isAuthenticated()) {
const page = currentPage();
if (!PUBLIC_PAGES.has(page)) {
window.location.replace("index.html");
}
}
}
function run() {
if (!window.AuthManager) return;
window.AuthManager.init();
if (PUBLIC_PAGES.has(currentPage())) return;
redirectGuest();
window.addEventListener("apush:session-restored", redirectGuest, { once: true });
window.addEventListener("apush:login", redirectGuest, { once: true });
setTimeout(redirectGuest, 500);
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", run);
} else {
run();
}
})();