From 1bc27495d6a65f9c96c569dd85085c482d84b63a Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sat, 21 Aug 2021 22:29:21 +0100 Subject: [PATCH] :recycle: Slight refactor, before implementing Keycloak --- src/router.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/router.js b/src/router.js index 07c19273..1db8ceae 100644 --- a/src/router.js +++ b/src/router.js @@ -16,24 +16,18 @@ import Minimal from '@/views/Minimal.vue'; import DownloadConfig from '@/views/DownloadConfig.vue'; // Import helper functions, config data and defaults -import { isLoggedIn } from '@/utils/Auth'; +import { isAuthEnabled, isLoggedIn, isGuestAccessEnabled } from '@/utils/Auth'; import { config } from '@/utils/ConfigHelpers'; import { metaTagData, startingView, routePaths } from '@/utils/defaults'; Vue.use(Router); -/* Checks if guest mode is enabled in appConfig */ -const isGuestEnabled = () => { - if (!config || !config.appConfig) return false; - if (config.appConfig.enableGuestAccess) return true; - return config.appConfig.auth.enableGuestAccess || false; -}; - /* Returns true if user is already authenticated, or if auth is not enabled */ const isAuthenticated = () => { - const auth = config.appConfig.auth || {}; - const users = Array.isArray(auth) ? auth : auth.users || []; - return (!users || users.length === 0 || isLoggedIn() || isGuestEnabled()); + const authEnabled = isAuthEnabled(); + const userLoggedIn = isLoggedIn(); + const guestEnabled = isGuestAccessEnabled(); + return (!authEnabled || userLoggedIn || guestEnabled); }; /* Get the users chosen starting view from app config, or return default */ @@ -99,7 +93,7 @@ const router = new Router({ }, beforeEnter: (to, from, next) => { // If the user already logged in + guest mode not enabled, then redirect home - if (isAuthenticated() && !isGuestEnabled()) router.push({ path: '/' }); + if (isAuthenticated() && !isGuestAccessEnabled()) router.push({ path: '/' }); next(); }, },