feat: add auth meta check
continuous-integration/drone/pr Build is failing Details

This commit is contained in:
Dominik Pschenitschni 2021-11-01 18:40:48 +01:00
parent 89ff5ebee1
commit 0c7df1a927
Signed by: dpschen
GPG Key ID: B257AC0149F43A77
1 changed files with 21 additions and 1 deletions

View File

@ -97,6 +97,9 @@ const router = createRouter({
path: '/login',
name: 'user.login',
component: LoginComponent,
meta: {
accessibleWithoutAuth: true,
},
},
{
path: '/get-password-reset',
@ -522,9 +525,27 @@ router.resolve({
params: { pathMatch: ['not', 'found'] },
}).href // '/not/found'
router.beforeEach((to) => {
if (
to.matched.some(record => record.meta.requiresAuth) &&
// this route requires auth, check if logged in
// if not, redirect to login page.
!auth.loggedIn()
) {
return {
path: '/login',
query: { redirect: to.fullPath },
}
}
})
function canUserAccess(to) {
// Check if the user is already logged in and redirect them to the home page if not
if (
(to.matched.some(record => record.meta.accessibleWithoutAuth))
to.name !== 'user.login' &&
to.name !== 'user.password-reset.request' &&
to.name !== 'user.password-reset.reset' &&
@ -537,7 +558,6 @@ function canUserAccess(to) {
saveLastVisited(to.name, to.params)
return false
}
return true
}
router.beforeEach(async (to, from) => {