cheetah-framework
Version:
Cheetah Framework JS used in all our applications
61 lines (50 loc) • 1.52 kB
JavaScript
/**
* 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 loadRoutes (r) {
r.keys().forEach(key => routes.push(...r(key).default))
}
// framework routes
loadRoutes(require.context('@cheetah/modules', true, /routes\/index\.js$/))
// app routes (Cheetah Commerce / Cheetah Reach)
loadRoutes(require.context('@@/cheetahModules', true, /routes\/index\.js$/))
// app project 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