diff --git a/webapp/layouts/default.vue b/webapp/layouts/default.vue index 1094acf21..8a6cdf704 100644 --- a/webapp/layouts/default.vue +++ b/webapp/layouts/default.vue @@ -224,7 +224,7 @@ export default { }, showFilterPostsDropdown() { const [firstRoute] = this.$route.matched - return firstRoute.name === 'index' + return firstRoute && firstRoute.name === 'index' }, }, watch: { diff --git a/webapp/middleware/authenticated.js b/webapp/middleware/authenticated.js index 04717e51c..f2273df58 100644 --- a/webapp/middleware/authenticated.js +++ b/webapp/middleware/authenticated.js @@ -1,5 +1,4 @@ import isEmpty from 'lodash/isEmpty' -import { VERSION } from '~/pages/terms-and-conditions' export default async ({ store, env, route, redirect }) => { let publicPages = env.publicPages @@ -10,14 +9,7 @@ export default async ({ store, env, route, redirect }) => { // await store.dispatch('auth/refreshJWT', 'authenticated middleware') const isAuthenticated = await store.dispatch('auth/check') - - // TODO: find a better solution to **reliably** get the user - // having the encrypted JWT does not mean we have access to the user object - const user = await store.getters['auth/user'] - - const upToDate = user.termsAndConditionsAgreedVersion === VERSION - - if (isAuthenticated === true && upToDate) { + if (isAuthenticated === true) { return true } @@ -26,14 +18,9 @@ export default async ({ store, env, route, redirect }) => { // set the redirect path for after the login let params = {} - if (!isEmpty(route.path) && route.path !== '/' && route.path !== 'terms-and-conditions-confirm') { + if (!isEmpty(route.path) && route.path !== '/') { params.path = route.path } - console.log("ÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖÖö") - if (!upToDate) { - console.log("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") - return redirect('/terms-and-conditions-confirm', params) - } else { - return redirect('/login', params) - } + + return redirect('/login', params) } diff --git a/webapp/middleware/termsAndConditions.js b/webapp/middleware/termsAndConditions.js new file mode 100644 index 000000000..c5a27fa21 --- /dev/null +++ b/webapp/middleware/termsAndConditions.js @@ -0,0 +1,19 @@ +import isEmpty from 'lodash/isEmpty' + +export default async ({ store, env, route, redirect }) => { + let publicPages = env.publicPages + // only affect non public pages + if (publicPages.indexOf(route.name) >= 0) { + return true + } + if (route.name === 'terms-and-conditions-confirm') return true // avoid endless loop + + if(store.getters['auth/termsAndConditionsAgreed']) return true + + let params = {} + if (!isEmpty(route.path) && route.path !== '/') { + params.path = route.path + } + + return redirect('/terms-and-conditions-confirm', params) +} diff --git a/webapp/nuxt.config.js b/webapp/nuxt.config.js index a95cac136..9a0839058 100644 --- a/webapp/nuxt.config.js +++ b/webapp/nuxt.config.js @@ -122,7 +122,7 @@ module.exports = { ], router: { - middleware: ['authenticated'], + middleware: ['authenticated', 'termsAndConditions'], linkActiveClass: 'router-link-active', linkExactActiveClass: 'router-link-exact-active', scrollBehavior: (to, _from, savedPosition) => { diff --git a/webapp/pages/terms-and-conditions-confirm.vue b/webapp/pages/terms-and-conditions-confirm.vue index 88f82a806..c28346587 100644 --- a/webapp/pages/terms-and-conditions-confirm.vue +++ b/webapp/pages/terms-and-conditions-confirm.vue @@ -54,9 +54,6 @@ export default { ...mapGetters({ currentUser: 'auth/user', }), - ...mapMutations({ - setCurrentUser: 'auth/SET_USER', - }), }, data() { return { @@ -74,15 +71,14 @@ export default { } }, asyncData({ store, redirect }) { - console.log('store') - console.log(store) - console.log('redirect') - console.log(redirect) - if (store.getters['auth/isLoggedIn']) { + if (store.getters['auth/termsAndConditionsAgreed']) { redirect('/') } }, methods: { + ...mapMutations({ + setCurrentUser: 'auth/SET_USER', + }), async submit() { try { await this.$apollo.mutate({ @@ -100,7 +96,7 @@ export default { }, }) this.$toast.success(this.$t('DANKE')) - this.$router.push('/') + this.$router.replace(this.$route.query.path || '/') } catch (err) { this.$toast.error(err.message) } diff --git a/webapp/store/auth.js b/webapp/store/auth.js index c7fe202f5..0f93a1d56 100644 --- a/webapp/store/auth.js +++ b/webapp/store/auth.js @@ -1,4 +1,5 @@ import gql from 'graphql-tag' +import { VERSION } from '~/pages/terms-and-conditions' export const state = () => { return { @@ -42,6 +43,9 @@ export const getters = { token(state) { return state.token }, + termsAndConditionsAgreed(state) { + return state.user && state.user.termsAndConditionsAgreedVersion === VERSION + } } export const actions = {