You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.7 KiB
81 lines
1.7 KiB
/**
|
|
* Vue Router
|
|
*
|
|
* @library
|
|
*
|
|
* https://router.vuejs.org/en/
|
|
*/
|
|
|
|
// Lib imports
|
|
import Vue from 'vue'
|
|
// import VueAnalytics from 'vue-analytics'
|
|
import Router from 'vue-router'
|
|
import Meta from 'vue-meta'
|
|
|
|
// Routes
|
|
import paths from './paths'
|
|
|
|
|
|
function route (options) {
|
|
let path = options.path;
|
|
let view = options.view;
|
|
let name = options.name;
|
|
let meta = (options.meta) ? options.meta : '';
|
|
return {
|
|
name: name || view,
|
|
path,
|
|
meta,
|
|
component: (resovle) => import(
|
|
`@Home/views/${view}.vue`
|
|
).then(resovle)
|
|
}
|
|
}
|
|
|
|
Vue.use(Router)
|
|
|
|
|
|
|
|
// Create a new router
|
|
const router = new Router({
|
|
mode: 'history',
|
|
routes: paths.map(path => route(path)).concat([
|
|
{ path: '*', redirect: '/main/home' }
|
|
]),
|
|
scrollBehavior (to, from, savedPosition) {
|
|
if (savedPosition) {
|
|
return savedPosition
|
|
}
|
|
if (to.hash) {
|
|
return { selector: to.hash }
|
|
}
|
|
return { x: 0, y: 0 }
|
|
}
|
|
})
|
|
|
|
Vue.use(Meta)
|
|
|
|
|
|
import { TokenService } from "@Global/services/storage.services";
|
|
import commonRoute from "@Global/utils/common/routes";
|
|
router.beforeEach((to, from, next) => {
|
|
const isPublic = to.matched.some(record => record.meta.public);
|
|
const onlyWhenLoggedOut = to.matched.some(
|
|
record => record.meta.onlyWhenLoggedOut
|
|
);
|
|
const roles = to.meta && to.meta.roles ? to.meta.roles : null;
|
|
const loggedIn = !!TokenService.getToken();
|
|
|
|
if (!isPublic && !loggedIn) {
|
|
return window.location.href = commonRoute.login() + '?redirect='+to.fullPath;
|
|
}
|
|
|
|
// Do not allow user to visit login page or register page if they are logged in
|
|
if (loggedIn && onlyWhenLoggedOut && policy) {
|
|
return window.location.href = commonRoute.main();
|
|
}
|
|
|
|
next();
|
|
});
|
|
|
|
export default router
|