Add no-class page & guard

This commit is contained in:
Christian Cueni 2019-10-24 08:37:45 +02:00
parent b20917d29a
commit 06dbf20094
3 changed files with 34 additions and 7 deletions

View File

@ -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 */

View File

@ -0,0 +1,5 @@
<template>
<div>
<p>Sie sind keiner Klasse zugeteilt. Bitte kontaktieren Sie die zuständige Lehrperson um Zugriff zu erhalten.</p>
</div>
</template>

View File

@ -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}
];