From 0c7df1a927549170d761cbaef515e88d28d97a34 Mon Sep 17 00:00:00 2001 From: Dominik Pschenitschni Date: Mon, 1 Nov 2021 18:40:48 +0100 Subject: [PATCH] feat: add auth meta check --- src/router/index.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/router/index.js b/src/router/index.js index 7ff3d34ba..852652eb3 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -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) => {