2020-10-14 16:44:03 +00:00
|
|
|
import { boot } from 'quasar/wrappers';
|
2020-10-29 00:39:06 +00:00
|
|
|
import { StateInterface } from 'src/store';
|
2020-10-16 11:07:31 +00:00
|
|
|
import { RouteRecord } from 'vue-router';
|
2020-11-04 22:53:10 +00:00
|
|
|
import { Store } from 'vuex';
|
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-11-04 22:53:10 +00:00
|
|
|
const session = store.state.session.currentSession;
|
2020-10-28 23:19:39 +00:00
|
|
|
|
2020-11-14 13:41:46 +00:00
|
|
|
if (to.path == from.path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-13 03:30:05 +00:00
|
|
|
if (to.path.startsWith('/main')) {
|
|
|
|
// Secured area (LOGIN REQUIRED)
|
|
|
|
// Check login is ok
|
|
|
|
if (!session || session.expires <= new Date()) {
|
2021-03-18 16:23:57 +00:00
|
|
|
store.dispatch('session/logout').catch((error) => {
|
2020-11-13 03:30:05 +00:00
|
|
|
console.warn(error);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if special permissions are required
|
2020-11-04 22:53:10 +00:00
|
|
|
if (
|
|
|
|
to.matched.every((record: RouteRecord) => {
|
2021-01-21 20:07:49 +00:00
|
|
|
if (!('meta' in record) || !('permissions' in record.meta)) return true;
|
2020-11-04 22:53:10 +00:00
|
|
|
if (record.meta) {
|
|
|
|
if ((<{ permissions: FG.Permission[] }>record.meta).permissions) {
|
2021-01-21 20:07:49 +00:00
|
|
|
return (<{ permissions: FG.Permission[] }>record.meta).permissions.every(
|
|
|
|
(permission: string) => {
|
|
|
|
return store.state.user.currentPermissions.includes(permission);
|
|
|
|
}
|
|
|
|
);
|
2020-11-04 22:53:10 +00:00
|
|
|
}
|
2020-10-30 12:27:33 +00:00
|
|
|
}
|
2020-11-04 22:53:10 +00:00
|
|
|
})
|
|
|
|
) {
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
next({ name: 'login', query: { redirect: to.fullPath } });
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-21 20:07:49 +00:00
|
|
|
if (to.name == 'login' && store.state.user.currentUser && !to.params['logout']) {
|
2020-11-09 02:40:51 +00:00
|
|
|
// Called login while already logged in
|
2020-11-10 00:33:55 +00:00
|
|
|
void next({ name: 'dashboard' });
|
2020-11-09 02:40:51 +00:00
|
|
|
} else {
|
2020-11-13 03:30:05 +00:00
|
|
|
// We are on the non secured area
|
2020-11-09 02:40:51 +00:00
|
|
|
next();
|
|
|
|
}
|
2020-11-04 22:53:10 +00:00
|
|
|
}
|
2020-10-27 10:51:53 +00:00
|
|
|
});
|
2020-10-14 16:44:03 +00:00
|
|
|
});
|