refactor routes and route guards

This commit is contained in:
Ulf Gebhardt 2021-11-19 19:56:37 +01:00
parent 2c25651db0
commit 5baeb0b0ef
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
2 changed files with 23 additions and 23 deletions

View File

@ -1,7 +1,23 @@
const addNavigationGuards = (router, store) => {
// store token on authenticate
router.beforeEach((to, from, next) => {
console.log('token', to.path, from.path, to.query)
// handle authentication
if (to.meta.requiresAuth && !store.state.token) {
if (to.path === '/authenticate' && to.query.token) {
console.log('token', to.query.token, to)
store.commit('token',to.query.token)
// to.query = {}
next({path: '/'})
} else {
next()
}
})
// protect all routes but not-found
router.beforeEach((to, from, next) => {
console.log('protect', to.path, from.path)
// handle authentication
if (!store.state.token && to.path !== '/not-found') {
next({ path: '/not-found' })
} else {
next()

View File

@ -1,38 +1,22 @@
const routes = [
{
path: '/',
component: () => import('@/views/Overview.vue'),
meta: {
requiresAuth: true,
},
path: '/authenticate',
},
{
path: '/overview',
component: () => import('@/views/Overview.vue'),
meta: {
requiresAuth: true,
},
path: '/',
component: () => import('@/pages/Overview.vue'),
},
{
path: '/user',
component: () => import('@/views/UserSearch.vue'),
meta: {
requiresAuth: true,
},
component: () => import('@/pages/UserSearch.vue'),
},
{
path: '/creation',
component: () => import('@/views/Creation.vue'),
meta: {
requiresAuth: true,
},
component: () => import('@/pages/Creation.vue'),
},
{
path: '/creation-confirm',
component: () => import('@/views/CreationConfirm.vue'),
meta: {
requiresAuth: true,
},
component: () => import('@/pages/CreationConfirm.vue'),
},
{
path: '*',