217 lines
4.9 KiB
TypeScript
217 lines
4.9 KiB
TypeScript
import type { RouteLocation } from "vue-router";
|
|
import { 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";
|
|
|
|
const Settings = () => import("@/views/Settings.vue");
|
|
const GlobalSettings = () => import("@/views/settings/Global.vue");
|
|
const ProfileSettings = () => import("@/views/settings/Profile.vue");
|
|
const Shares = () => import("@/views/settings/Shares.vue");
|
|
import Errors from "@/views/Errors.vue";
|
|
import { useAuthStore } from "@/stores/auth";
|
|
import { baseURL, name } from "@/utils/constants";
|
|
import i18n from "@/i18n";
|
|
import { validateLogin } from "@/utils/auth";
|
|
|
|
const titles = {
|
|
CraftEditor: "addons.title",
|
|
Login: "sidebar.login",
|
|
Share: "buttons.share",
|
|
Files: "files.files",
|
|
Settings: "sidebar.settings",
|
|
ProfileSettings: "settings.profileSettings",
|
|
Shares: "settings.shareManagement",
|
|
GlobalSettings: "explorer.adminSettings",
|
|
Users: "settings.users",
|
|
User: "settings.user",
|
|
Forbidden: "errors.forbidden",
|
|
NotFound: "errors.notFound",
|
|
InternalServerError: "errors.internal",
|
|
};
|
|
|
|
const routes = [
|
|
{
|
|
path: "/editor/:addon(gridcraft|pdfcraft)",
|
|
name: "CraftEditor",
|
|
component: () => import("@/views/files/CraftEditor.vue"),
|
|
meta: { requiresAuth: true },
|
|
},
|
|
{
|
|
path: "/login",
|
|
name: "Login",
|
|
component: Login,
|
|
},
|
|
{
|
|
path: "/s",
|
|
alias: "/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,
|
|
redirect: {
|
|
path: "/settings/profile",
|
|
},
|
|
children: [
|
|
{
|
|
path: "profile",
|
|
name: "ProfileSettings",
|
|
component: ProfileSettings,
|
|
},
|
|
{
|
|
path: "shares",
|
|
name: "Shares",
|
|
component: Shares,
|
|
},
|
|
{
|
|
path: "global",
|
|
name: "GlobalSettings",
|
|
component: GlobalSettings,
|
|
meta: {
|
|
requiresAdmin: true,
|
|
},
|
|
},
|
|
{
|
|
path: "users",
|
|
name: "Users",
|
|
redirect: { path: "/settings/global", query: { users: "all" } },
|
|
meta: {
|
|
requiresAdmin: true,
|
|
},
|
|
},
|
|
{
|
|
path: "users/:id",
|
|
name: "User",
|
|
redirect: (to: RouteLocation) => ({
|
|
path: "/settings/global",
|
|
query: { users: String(to.params.id) },
|
|
}),
|
|
meta: {
|
|
requiresAdmin: true,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
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(.*)*",
|
|
redirect: (to: RouteLocation) => {
|
|
const catchAll = to.params.catchAll;
|
|
if (!catchAll) return "/files/";
|
|
return `/files/${Array.isArray(catchAll) ? catchAll.join("/") : catchAll}`;
|
|
},
|
|
},
|
|
];
|
|
|
|
async function initAuth() {
|
|
await validateLogin();
|
|
}
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(baseURL),
|
|
routes,
|
|
scrollBehavior(to, from, savedPosition) {
|
|
if (savedPosition) return savedPosition;
|
|
if (to.path !== from.path && to.path.startsWith("/settings"))
|
|
return { top: 0 };
|
|
},
|
|
});
|
|
|
|
router.beforeResolve(async (to, from) => {
|
|
const authStore = useAuthStore();
|
|
|
|
// this will only be null on first route
|
|
if (from.name == null) {
|
|
try {
|
|
await initAuth();
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
}
|
|
|
|
const title = i18n.global.t(titles[to.name as keyof typeof titles]);
|
|
document.title = title + " - " + name;
|
|
|
|
if (to.path.endsWith("/login") && authStore.isLoggedIn) {
|
|
return { path: "/files/" };
|
|
}
|
|
|
|
if (to.matched.some((record) => record.meta.requiresAuth)) {
|
|
if (!authStore.isLoggedIn) {
|
|
return {
|
|
path: "/login",
|
|
query: { redirect: to.fullPath },
|
|
};
|
|
}
|
|
|
|
if (to.matched.some((record) => record.meta.requiresAdmin)) {
|
|
if (authStore.user === null || !authStore.user.perm.admin) {
|
|
return { path: "/403" };
|
|
}
|
|
}
|
|
}
|
|
|
|
return;
|
|
});
|
|
|
|
export { router, router as default };
|