parent
8c153d9e8f
commit
cf1d99a72e
@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Vue Router
|
||||
*
|
||||
* @library
|
||||
*
|
||||
* https://router.vuejs.org/en/
|
||||
*/
|
||||
|
||||
// Lib imports
|
||||
import Vue from 'vue'
|
||||
import Router from 'vue-router'
|
||||
import Meta from 'vue-meta'
|
||||
|
||||
Vue.use(Router);
|
||||
Vue.use(Meta);
|
||||
|
||||
|
||||
import { TokenService } from "@Global/services/storage.services";
|
||||
import commonRoute from "@Global/utils/common/routes";
|
||||
|
||||
|
||||
export default class {
|
||||
|
||||
constructor(paths, callbackDynamicImport, redirect) {
|
||||
this.create(paths, callbackDynamicImport, redirect);
|
||||
this.beforeLoad();
|
||||
return this.router;
|
||||
}
|
||||
|
||||
route(options, callbackDynamicImport) {
|
||||
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: callbackDynamicImport(view)
|
||||
}
|
||||
}
|
||||
|
||||
create(paths, callbackDynamicImport, redirect) {
|
||||
const self = this;
|
||||
redirect = redirect ? redirect : paths[0].path;
|
||||
this.router = new Router({
|
||||
mode: 'history',
|
||||
routes: paths.map(path => self.route(path, callbackDynamicImport)).concat([
|
||||
{ path: '*', redirect: redirect }
|
||||
]),
|
||||
scrollBehavior(to, from, savedPosition) {
|
||||
if (savedPosition) {
|
||||
return savedPosition
|
||||
}
|
||||
if (to.hash) {
|
||||
return { selector: to.hash }
|
||||
}
|
||||
return { x: 0, y: 0 }
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
beforeLoad() {
|
||||
|
||||
this.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();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in new issue