Skip to content

Commit

Permalink
refactor: validateVisit edge cases
Browse files Browse the repository at this point in the history
- If user is logged in and does not have access to builder, redirct to app
- if user is not logged in redirect to login. Once user logs in redirect back to builder page.
  • Loading branch information
surajshetty3416 committed Dec 27, 2024
1 parent fdad369 commit 7127b0f
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions frontend/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,49 @@ function validatePermission(next: NavigationGuardNext) {
next();
} else {
alert("You do not have permission to access this page");
window.location.href = "/login";
if (isUserLoggedIn()) {
window.location.href = "/app";
} else {
window.location.href = "/login?redirect-to=/builder";
}
}
}

const validateVisit = function (
const validateVisit = async function (
to: RouteLocationNormalized,
from: RouteLocationNormalized,
next: NavigationGuardNext,
) {
if (document.cookie.includes("user_id") && !document.cookie.includes("user_id=Guest")) {
sessionUser.value = decodeURIComponent(document.cookie.split("user_id=")[1].split(";")[0]);
if (isUserLoggedIn()) {
sessionUser.value = getSessionUser();
if (hasPermission === null) {
createResource({
url: "frappe.client.has_permission",
caches: "has_permission",
})
.submit({
try {
const response = await createResource({
url: "frappe.client.has_permission",
}).submit({
doctype: "Builder Page",
docname: null,
perm_type: "write",
})
.then((res: { has_permission: boolean }) => {
hasPermission = res.has_permission;
validatePermission(next);
})
.catch(() => {
hasPermission = false;
validatePermission(next);
});
} else {
validatePermission(next);
hasPermission = response.has_permission;
return validatePermission(next);
} catch (e) {
hasPermission = false;
return validatePermission(next);
}
}
} else {
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",
Expand Down

0 comments on commit 7127b0f

Please sign in to comment.