44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
import { boot } from 'quasar/wrappers';
|
|
import { useMainStore } from 'src/stores';
|
|
import { hasPermissions } from 'src/utils/permission';
|
|
import { RouteRecord } from 'vue-router';
|
|
|
|
export default boot(({ router }) => {
|
|
router.beforeResolve((to, from, next) => {
|
|
const store = useMainStore();
|
|
|
|
if (to.path == from.path) return next();
|
|
|
|
if (to.path.startsWith('/main')) {
|
|
// Secured area (LOGIN REQUIRED)
|
|
// Check login is ok
|
|
if (!store.session || store.session.expires <= new Date()) {
|
|
void store.logout();
|
|
return next({ name: 'login', query: { redirect: to.fullPath } });
|
|
}
|
|
|
|
// Check if special permissions are required
|
|
if (
|
|
to.matched.every((record: RouteRecord) => {
|
|
if (!('meta' in record) || !('permissions' in record.meta)) return true;
|
|
if ((<{ permissions: FG.Permission[] }>record.meta).permissions) {
|
|
return hasPermissions((<{ permissions: FG.Permission[] }>record.meta).permissions);
|
|
}
|
|
})
|
|
) {
|
|
return next();
|
|
} else {
|
|
return next({ name: 'login', query: { redirect: to.fullPath } });
|
|
}
|
|
} else {
|
|
if (to.name == 'login' && store.user && !to.params['logout']) {
|
|
// Called login while already logged in
|
|
return next({ name: 'dashboard' });
|
|
} else {
|
|
// We are on the non secured area
|
|
return next();
|
|
}
|
|
}
|
|
});
|
|
});
|