2020-10-14 16:44:03 +00:00
|
|
|
import { boot } from 'quasar/wrappers';
|
2020-10-16 11:07:31 +00:00
|
|
|
import { RouteRecord } from 'vue-router';
|
2020-10-27 10:51:53 +00:00
|
|
|
import { Store } from 'vuex';
|
2020-10-18 23:45:06 +00:00
|
|
|
import { StateInterface } from 'src/store';
|
2020-10-27 10:51:53 +00:00
|
|
|
import { UserStateInterface } from 'src/plugins/user/store/user';
|
2020-10-14 16:44:03 +00:00
|
|
|
|
2020-10-18 23:45:06 +00:00
|
|
|
export default boot<Store<StateInterface>>(({ router, store }) => {
|
2020-10-16 07:38:14 +00:00
|
|
|
router.beforeEach((to, from, next) => {
|
2020-10-27 10:51:53 +00:00
|
|
|
store
|
|
|
|
.dispatch('user/loadFromLocalStorage')
|
|
|
|
.then(() => {
|
|
|
|
const user: FG.User = <FG.User>store.getters['user/user'];
|
|
|
|
const session: FG.Session = <FG.Session>store.getters['user/session'];
|
|
|
|
let permissions: string[] = [];
|
|
|
|
user.roles.forEach(role => {
|
|
|
|
permissions = permissions.concat(role.permissions);
|
2020-10-18 23:45:06 +00:00
|
|
|
});
|
2020-10-27 10:51:53 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2020-10-14 16:44:03 +00:00
|
|
|
});
|