UNPKG

cheetah-framework

Version:

Cheetah Framework JS used in all our applications

76 lines (61 loc) 2.03 kB
/** * Autodiscovery of modules routes. */ /* global cheetahApp */ import Vue from 'vue' import VueRouter from 'vue-router' import layout404 from '@cheetah/layouts/404' import config from '@cheetah/config' const authConfig = config.auth const routes = [] Vue.use(VueRouter) function addRoutes (routesToAdd) { routesToAdd.forEach(route => { const foundIndex = routes.findIndex(r => r.name === route.name) if (foundIndex !== -1) { routes.splice(foundIndex, 1, route) } else { routes.push(route) } }) } function loadRoutes (context) { context.keys().forEach(key => addRoutes(context(key).default)) } // Cheetah-Framework routes loadRoutes(require.context('@cheetah/modules', true, /\.\/.*\/routes\/index\.js$/)) // Other modules routes (@imarcom/monitored-tasks, @imarcom/cms-core, @imarcom/cheetah-attributes, ...) loadRoutes(require.context('@@/cheetahModules', true, /\.\/@[^/]+\/routes\/index\.js$/)) loadRoutes(require.context('@@/cheetahModules', true, /\.\/@[^/]+\/modules\/[^/]+\/routes\/index\.js$/)) // App modules routes (usually in /resources/js/modules/*/routes) loadRoutes(require.context('@@/modules', true, /\.\/.*\/routes\/index\.js$/)) routes.push({ path: '/*', component: layout404, name: '404' }) const router = new VueRouter({ mode: config.router.mode, linkActiveClass: 'active', base: config.router.base, routes }) router.beforeEach(async (to, from, next) => { /** * Await next tick since on the first load, the app is not * yet loaded and the user is undefined. This is the most elegant * and fastest way to ensure the app is loaded. * * @see https://github.com/vuejs/vue-router/issues/2672 */ await Vue.nextTick() if (_.get(to, 'meta.auth', true)) { if (!cheetahApp.user) { return next({ name: _.get(authConfig, 'routes.login.name', 'auth'), query: { redirectPath: to.fullPath }}) } } else if (cheetahApp.user) { return next(authConfig.defaultRedirectRoute) } return next() }) export default router