import { boot } from 'quasar/wrappers'; import { RouteRecord } from 'vue-router'; import { Store } from 'vuex'; import { StateInterface } from 'src/store'; import { UserStateInterface } from 'src/plugins/user/store/user'; export default boot>(({ router, store }) => { router.beforeEach((to, from, next) => { store .dispatch('user/loadFromLocalStorage') .then(() => { const user: FG.User = store.getters['user/user']; const session: FG.Session = store.getters['user/session']; let permissions: string[] = []; user.roles.forEach(role => { permissions = permissions.concat(role.permissions); }); console.log('route to', to); console.log('route from', from); if (to.name != 'login') { console.log(new Date(session.expires), new Date()); console.log(new Date(session.expires) >= new Date()); if ( new Date(session.expires) >= new Date() && to.matched.every((record: RouteRecord) => { const checkedPerimssions: | boolean | undefined = record.meta?.permissions?.every( (permission: FG.Permission) => { return permissions.includes(permission); } ); return checkedPerimssions === undefined ? true : checkedPerimssions; }) ) { next(); } else { next({ name: 'login', query: { redirect: to.fullPath } }); } } else { next(); } // if ( // to.matched.some((record: RouteRecord) => { // // permissions is set AND has NO matching permission // return ( // 'permissions' in record.meta && // record.meta.permissions.filter((value: string) => // permissions.includes(value) // ).length == 0 // ); // }) // ) { // next({ // path: '/login', // query: { redirect: to.fullPath } // }); // } else { // next(); // } }) .catch(error => { console.exception(error); }); }); });