flaschengeist-frontend/src/boot/login.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-10-14 16:44:03 +00:00
import { boot } from 'quasar/wrappers';
import { StateInterface } from 'src/store';
2020-10-16 11:07:31 +00:00
import { RouteRecord } from 'vue-router';
import { Store } from 'vuex';
2020-10-14 16:44:03 +00:00
export default boot<Store<StateInterface>>(({ router, store }) => {
2020-10-16 07:38:14 +00:00
router.beforeEach((to, from, next) => {
const session = store.state.session.currentSession;
if (to.name != 'login') {
if (!session || session.expires <= new Date()) {
store.dispatch('session/logout').catch(error => {
console.warn(error);
});
return next({ name: 'login', query: { redirect: to.fullPath } });
}
if (
to.matched.every((record: RouteRecord) => {
if (!('meta' in record) || !('permissions' in record.meta))
return true;
if (record.meta) {
if ((<{ permissions: FG.Permission[] }>record.meta).permissions) {
return (<{ permissions: FG.Permission[] }>(
record.meta
)).permissions.every((permission: string) => {
return store.state.user.currentPermissions.includes(permission);
});
}
}
})
) {
next();
} else {
next({ name: 'login', query: { redirect: to.fullPath } });
}
} else {
if (store.state.user.currentUser && !to.params['logout']) {
// Called login while already logged in
void next({ name: 'dashboard' });
} else {
// Not logged in or from logout
next();
}
}
});
2020-10-14 16:44:03 +00:00
});