salix-front/src/router/index.js

117 lines
3.8 KiB
JavaScript
Raw Normal View History

2024-12-10 14:36:25 +00:00
import { route as defineRouter } from 'quasar/wrappers';
import {
createRouter,
createMemoryHistory,
createWebHistory,
createWebHashHistory,
} from 'vue-router';
2022-12-20 11:00:36 +00:00
import routes from './routes';
2022-12-20 11:30:25 +00:00
import { i18n } from 'src/boot/i18n';
import { useState } from 'src/composables/useState';
import { useRole } from 'src/composables/useRole';
2023-05-12 07:15:45 +00:00
import { useUserConfig } from 'src/composables/useUserConfig';
2024-04-22 05:37:35 +00:00
import { useTokenConfig } from 'src/composables/useTokenConfig';
import { useAcl } from 'src/composables/useAcl';
2024-12-10 14:36:25 +00:00
import { isLoggedIn } from 'src/utils/session';
import { useSession } from 'src/composables/useSession';
2022-12-20 11:30:25 +00:00
let session = null;
2024-03-14 14:16:50 +00:00
const { t, te } = i18n.global;
2022-12-20 11:00:36 +00:00
const createHistory = process.env.SERVER
? createMemoryHistory
: process.env.VUE_ROUTER_MODE === 'history'
2025-01-23 12:49:21 +00:00
? createWebHistory
: createWebHashHistory;
const Router = createRouter({
scrollBehavior: () => ({ left: 0, top: 0 }),
routes,
// Leave this as is and make changes in quasar.conf.js instead!
// quasar.conf.js -> build -> vueRouterMode
// quasar.conf.js -> build -> publicPath
history: createHistory(process.env.VUE_ROUTER_BASE),
});
2022-12-20 11:00:36 +00:00
/*
* If not building with SSR mode, you can
* directly export the Router instantiation;
*
* The function below can be async too; either use
* async/await or return a Promise which resolves
* with the Router instance.
*/
export { Router };
2024-12-10 14:36:25 +00:00
export default defineRouter(function (/* { store, ssrContext } */) {
const state = useState();
2022-12-20 11:30:25 +00:00
Router.beforeEach(async (to, from, next) => {
if (!session) session = useSession();
const outLayout = Router.options.routes[0].children.map((r) => r.name);
2024-12-10 14:36:25 +00:00
if (!session.isLoggedIn() && !outLayout.includes(to.name)) {
2022-12-20 11:30:25 +00:00
return next({ name: 'Login', query: { redirect: to.fullPath } });
}
if (isLoggedIn()) {
const stateRoles = state.getRoles().value;
if (stateRoles.length === 0) {
2023-05-12 07:15:45 +00:00
await useRole().fetch();
await useAcl().fetch();
2023-05-12 07:15:45 +00:00
await useUserConfig().fetch();
2024-04-22 05:37:35 +00:00
await useTokenConfig().fetch();
2022-12-20 11:30:25 +00:00
}
const matches = to.matched;
const hasRequiredAcls = matches.every((route) => {
2022-12-20 11:30:25 +00:00
const meta = route.meta;
if (!meta?.acls) return true;
return useAcl().hasAny(meta.acls);
2022-12-20 11:30:25 +00:00
});
if (!hasRequiredAcls) return next({ path: '/' });
2022-12-20 11:30:25 +00:00
}
next();
});
Router.afterEach((to) => {
let title = t(`login.title`);
const matches = to.matched;
if (matches && matches.length > 1) {
const module = matches[1];
const moduleTitle = module.meta && module.meta.title;
if (moduleTitle) {
2024-06-19 06:44:40 +00:00
title = t(`globals.pageTitles.${moduleTitle}`);
2022-12-20 11:30:25 +00:00
}
}
const childPage = to.meta;
const childPageTitle = childPage && childPage.title;
if (childPageTitle && matches.length > 2) {
if (title != '') title += ': ';
2024-06-19 06:44:40 +00:00
const moduleLocale = `globals.pageTitles.${childPageTitle}`;
2024-03-14 14:16:50 +00:00
const pageTitle = te(moduleLocale)
? t(moduleLocale)
: t(`globals.pageTitles.${childPageTitle}`);
2022-12-20 11:30:25 +00:00
const idParam = to.params && to.params.id;
const idPageTitle = `${idParam} - ${pageTitle}`;
const builtTitle = idParam ? idPageTitle : pageTitle;
title += builtTitle;
}
document.title = title;
});
2025-01-26 22:49:40 +00:00
Router.onError(({ message }) => {
2025-01-26 02:16:25 +00:00
const errorMessages = [
'Failed to fetch dynamically imported module',
'Importing a module script failed',
];
2025-02-07 13:38:06 +00:00
state.set(
'error',
errorMessages.some((error) => message.startsWith(error)),
);
2025-01-23 12:49:21 +00:00
});
2022-12-20 11:00:36 +00:00
return Router;
});