-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
36 lines (29 loc) · 1.01 KB
/
router.js
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
import Error404 from "./views/pages/Error404";
import Home from "./views/pages/Home";
import Expenses from "./views/pages/Expenses";
import Navbar from "./views/components/Navbar";
const routes = {
"/": Home,
expenses: Expenses,
};
const navbar = document.getElementById("navbar");
const content = document.getElementById("content");
const router = async () => {
if (new URLSearchParams(window.location.search).has("code")) {
await window.auth0Client.handleRedirectCallback();
window.history.replaceState({}, document.title, "/");
}
if (await window.auth0Client.isAuthenticated())
window.user = await window.auth0Client.getUser();
const request = location.hash.slice(1).toLowerCase() || "/";
const page = routes[request] || Error404;
if (await page.allowAccess()) {
content.innerHTML = await page.render();
await page.postRender();
} else {
window.history.replaceState({}, document.title, "/");
}
navbar.innerHTML = await Navbar.render();
await Navbar.postRender();
};
export default router;