filebrowser/frontend/src/router/index.ts

169 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-07-30 17:45:27 +00:00
import { RouteLocation, createRouter, createWebHistory } from "vue-router";
import Login from "@/views/Login.vue";
import Layout from "@/views/Layout.vue";
import Files from "@/views/Files.vue";
import Share from "@/views/Share.vue";
import Settings from "@/views/Settings.vue";
import Errors from "@/views/Errors.vue";
import { baseURL, name } from "@/utils/constants";
import { getters, state } from "@/store";
import { mutations } from "@/store";
import i18n from "@/i18n";
const titles = {
Login: "sidebar.login",
Share: "buttons.share",
Files: "files.files",
Settings: "sidebar.settings",
ProfileSettings: "settings.profileSettings",
Shares: "settings.shareManagement",
GlobalSettings: "settings.globalSettings",
Users: "settings.users",
User: "settings.user",
Forbidden: "errors.forbidden",
NotFound: "errors.notFound",
InternalServerError: "errors.internal",
};
const routes = [
{
path: "/login",
name: "Login",
component: Login,
},
{
path: "/share",
component: Layout,
children: [
{
path: ":path*",
name: "Share",
component: Share,
},
],
},
{
path: "/files",
component: Layout,
meta: {
requiresAuth: true,
},
children: [
{
path: ":path*",
name: "Files",
component: Files,
},
],
},
{
path: "/settings",
component: Layout,
meta: {
requiresAuth: true,
},
children: [
{
path: "",
name: "Settings",
component: Settings,
},
2024-09-16 21:01:16 +00:00
{
path: "users/:id",
name: "User",
component: Settings,
},
2024-07-30 17:45:27 +00:00
],
},
{
path: "/403",
name: "Forbidden",
component: Errors,
props: {
errorCode: 403,
showHeader: true,
},
},
{
path: "/404",
name: "NotFound",
component: Errors,
props: {
errorCode: 404,
showHeader: true,
},
},
{
path: "/500",
name: "InternalServerError",
component: Errors,
props: {
errorCode: 500,
showHeader: true,
},
},
{
path: "/:catchAll(.*)*",
2024-11-26 17:21:41 +00:00
redirect: (to: RouteLocation) => {
const path = Array.isArray(to.params.catchAll)
? to.params.catchAll.join("/")
: to.params.catchAll || "";
return `/files/${path}`;
},
2024-07-30 17:45:27 +00:00
},
];
const router = createRouter({
history: createWebHistory(baseURL),
routes,
});
2024-11-26 17:21:41 +00:00
// Helper function to check if a route resolves to itself
function isSameRoute(to: RouteLocation, from: RouteLocation) {
return to.path === from.path && JSON.stringify(to.params) === JSON.stringify(from.params);
2024-07-30 17:45:27 +00:00
}
router.beforeResolve(async (to, from, next) => {
2024-11-26 17:21:41 +00:00
console.log("Navigating to", to.path,from.path);
if (isSameRoute(to, from)) {
console.warn("Avoiding recursive navigation to the same route.");
return next(false);
}
// Set the page title using i18n
2024-07-30 17:45:27 +00:00
const title = i18n.global.t(titles[to.name as keyof typeof titles]);
document.title = title + " - " + name;
2024-11-26 17:21:41 +00:00
// Update store with the current route
mutations.setRoute(to);
// Handle auth requirements
2024-07-30 17:45:27 +00:00
if (to.matched.some((record) => record.meta.requiresAuth)) {
if (!getters.isLoggedIn()) {
next({
path: "/login",
query: { redirect: to.fullPath },
});
return;
}
2024-11-26 17:21:41 +00:00
// Handle admin-only routes
2024-07-30 17:45:27 +00:00
if (to.matched.some((record) => record.meta.requiresAdmin)) {
2024-11-26 17:21:41 +00:00
if (!state.user || !getters.isAdmin()) {
2024-07-30 17:45:27 +00:00
next({ path: "/403" });
return;
}
}
}
2024-11-26 17:21:41 +00:00
// Redirect logged-in users from login page
if (to.path.endsWith("/login") && getters.isLoggedIn()) {
next({ path: "/files/" });
return;
}
2024-07-30 17:45:27 +00:00
next();
});
2024-11-26 17:21:41 +00:00
export { router, router as default };