2022-12-20 11:00:36 +00:00
|
|
|
import { route } from 'quasar/wrappers';
|
2023-03-15 10:28:18 +00:00
|
|
|
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 { useSession } from 'src/composables/useSession';
|
|
|
|
import { useRole } from 'src/composables/useRole';
|
2023-05-12 07:15:45 +00:00
|
|
|
import { useUserConfig } from 'src/composables/useUserConfig';
|
2022-12-20 11:30:25 +00:00
|
|
|
import { toLowerCamel } from 'src/filters';
|
|
|
|
|
|
|
|
const state = useState();
|
|
|
|
const session = useSession();
|
|
|
|
const { t } = i18n.global;
|
2022-12-20 11:00:36 +00:00
|
|
|
|
2023-03-15 10:28:18 +00:00
|
|
|
const createHistory = process.env.SERVER
|
|
|
|
? createMemoryHistory
|
|
|
|
: process.env.VUE_ROUTER_MODE === 'history'
|
|
|
|
? 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.
|
|
|
|
*/
|
2023-03-15 10:28:18 +00:00
|
|
|
export { Router };
|
2022-12-20 11:00:36 +00:00
|
|
|
export default route(function (/* { store, ssrContext } */) {
|
2022-12-20 11:30:25 +00:00
|
|
|
Router.beforeEach(async (to, from, next) => {
|
|
|
|
const { isLoggedIn } = session;
|
2023-08-03 13:56:55 +00:00
|
|
|
const outLayout = ['Login', 'TwoFactor'];
|
|
|
|
if (!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 useUserConfig().fetch();
|
2022-12-20 11:30:25 +00:00
|
|
|
}
|
|
|
|
const matches = to.matched;
|
|
|
|
const hasRequiredRoles = matches.every((route) => {
|
|
|
|
const meta = route.meta;
|
2023-05-12 07:15:45 +00:00
|
|
|
if (meta && meta.roles) return useRole().hasAny(meta.roles);
|
2022-12-20 11:30:25 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!hasRequiredRoles) {
|
|
|
|
return next({ path: '/' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
Router.afterEach((to) => {
|
|
|
|
let title = t(`login.title`);
|
|
|
|
|
|
|
|
const matches = to.matched;
|
|
|
|
let moduleName;
|
|
|
|
if (matches && matches.length > 1) {
|
|
|
|
const module = matches[1];
|
|
|
|
const moduleTitle = module.meta && module.meta.title;
|
|
|
|
moduleName = toLowerCamel(module.name);
|
|
|
|
if (moduleTitle) {
|
|
|
|
title = t(`${moduleName}.pageTitles.${moduleTitle}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const childPage = to.meta;
|
|
|
|
const childPageTitle = childPage && childPage.title;
|
|
|
|
if (childPageTitle && matches.length > 2) {
|
|
|
|
if (title != '') title += ': ';
|
|
|
|
|
|
|
|
const pageTitle = t(`${moduleName}.pageTitles.${childPageTitle}`);
|
|
|
|
const idParam = to.params && to.params.id;
|
|
|
|
const idPageTitle = `${idParam} - ${pageTitle}`;
|
|
|
|
const builtTitle = idParam ? idPageTitle : pageTitle;
|
|
|
|
|
|
|
|
title += builtTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
document.title = title;
|
|
|
|
});
|
|
|
|
|
2022-12-20 11:00:36 +00:00
|
|
|
return Router;
|
|
|
|
});
|