forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.ts
More file actions
103 lines (94 loc) · 2.27 KB
/
router.ts
File metadata and controls
103 lines (94 loc) · 2.27 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { createResource } from "frappe-ui";
import { ref } from "vue";
import { NavigationGuardNext, RouteLocationNormalized, createRouter, createWebHistory } from "vue-router";
let hasPermission: null | boolean = null;
let sessionUser = ref("Guest");
function validatePermission(next: NavigationGuardNext) {
if (hasPermission) {
next();
} else {
alert("You do not have permission to access this page");
if (isUserLoggedIn()) {
window.location.href = "/app";
} else {
window.location.href = "/login?redirect-to=/builder";
}
}
}
const validateVisit = async function (
to: RouteLocationNormalized,
from: RouteLocationNormalized,
next: NavigationGuardNext,
) {
if (isUserLoggedIn()) {
sessionUser.value = getSessionUser();
if (hasPermission === null) {
try {
const response = await createResource({
url: "frappe.client.has_permission",
}).submit({
doctype: "Builder Page",
docname: "",
perm_type: "write",
});
hasPermission = response.has_permission;
return validatePermission(next);
} catch (e) {
hasPermission = false;
return validatePermission(next);
}
}
}
return validatePermission(next);
};
function isUserLoggedIn() {
return document.cookie.includes("user_id") && !document.cookie.includes("user_id=Guest");
}
function getSessionUser() {
return decodeURIComponent(document.cookie.split("user_id=")[1].split(";")[0]) || "Guest";
}
const routes = [
{
path: "/home",
beforeEnter: validateVisit,
redirect: "/",
},
{
path: "/page",
beforeEnter: validateVisit,
redirect: "/home",
},
{
path: "/",
name: "home",
beforeEnter: validateVisit,
component: () => import("@/pages/PageBuilderDashboard.vue"),
},
{
path: "/page/:pageId",
name: "builder",
beforeEnter: validateVisit,
component: () => import("@/pages/PageBuilder.vue"),
},
{
path: "/page/:pageId/preview",
name: "preview",
beforeEnter: validateVisit,
component: () => import("@/pages/PagePreview.vue"),
},
];
declare global {
interface Window {
builder_path: string;
}
}
let builder_path = window.builder_path || "/builder";
if (builder_path.startsWith("{{")) {
builder_path = "/builder";
}
const router = createRouter({
history: createWebHistory(builder_path),
routes,
});
export { sessionUser };
export default router;