flaschengeist-frontend/src/boot/login.ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

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';
import { Store } from 'vuex'
import { StateInterface } from 'src/store';
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) => {
store.dispatch('user/loadFromLocalStorage').then(() => {
const user: User = store.getters['user/user'];
let permissions: string[] = [];
user.roles.forEach(role => {
permissions = permissions.concat(role.permissions)
});
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
2020-10-16 11:07:31 +00:00
)
);
})
) {
next({
path: '/login',
query: { redirect: to.fullPath }
});
} else {
next();
}
}).catch(error => {
console.exception(error);
});
})
2020-10-14 16:44:03 +00:00
});