diff --git a/client/src/main.js b/client/src/main.js index ce33c59b..341a5d02 100644 --- a/client/src/main.js +++ b/client/src/main.js @@ -16,6 +16,7 @@ import veeDe from 'vee-validate/dist/locale/de'; import {dateFilter} from './filters/date-filter'; import autoGrow from '@/directives/auto-grow' import clickOutside from '@/directives/click-outside' +import ME_QUERY from '@/graphql/gql/meQuery.gql'; Vue.config.productionTip = false; @@ -113,7 +114,7 @@ Vue.use(VeeValidate, { Vue.filter('date', dateFilter); -/* logged in guard */ +/* guards */ function getCookieValue(cookieName) { // https://stackoverflow.com/questions/5639346/what-is-the-shortest-function-for-reading-a-cookie-by-name-in-javascript @@ -121,18 +122,32 @@ function getCookieValue(cookieName) { return cookieValue ? cookieValue.pop() : ''; } -function redirectIfLoginRequird(to) { +function loginRequired(to) { // public pages have the meta.public property set to true - return (!to.hasOwnProperty('meta') || !to.meta.hasOwnProperty('public') || !to.meta.public) && getCookieValue('loginStatus') !== 'true'; + return !to.hasOwnProperty('meta') || !to.meta.hasOwnProperty('public') || !to.meta.public; } -router.beforeEach((to, from, next) => { - if (redirectIfLoginRequird(to)) { +function unauthorizedAccess(to) { + return loginRequired(to) && getCookieValue('loginStatus') !== 'true'; +} + +function redirectStudentsWithoutClass() { + return privateApolloClient.query({ + query: ME_QUERY, + }).then(({data}) => data.me.schoolClasses.edges.length === 0 && data.me.permissions.length === 0); +} + +router.beforeEach(async (to, from, next) => { + if (unauthorizedAccess(to)) { const redirectUrl = `/login?redirect=${to.path}`; next(redirectUrl); - } else { - next(); } + + if (to.name !== 'noClass' && loginRequired(to) && await redirectStudentsWithoutClass()) { + router.push({name: 'noClass'}) + } + + next(); }); /* eslint-disable no-new */ diff --git a/client/src/pages/waitforClass.vue b/client/src/pages/waitforClass.vue new file mode 100644 index 00000000..0dc8ed43 --- /dev/null +++ b/client/src/pages/waitforClass.vue @@ -0,0 +1,5 @@ + diff --git a/client/src/router/index.js b/client/src/router/index.js index 44ce2b7d..c7e7bc6c 100644 --- a/client/src/router/index.js +++ b/client/src/router/index.js @@ -29,6 +29,7 @@ import styleGuidePage from '@/pages/styleguide' import moduleRoom from '@/pages/moduleRoom' import login from '@/pages/login' import registration from '@/pages/registration' +import waitForClass from '@/pages/waitForClass' import store from '@/store/index'; @@ -127,6 +128,12 @@ const routes = [ layout: 'public', } }, + { + path: '/no-class', + component: waitForClass, + name: 'noClass', + meta: {layout: 'public'} + }, {path: '/styleguide', component: styleGuidePage}, {path: '*', component: p404} ];