♻️ Slight refactor, before implementing Keycloak

This commit is contained in:
Alicia Sykes 2021-08-21 22:29:21 +01:00
parent 7608fba2f5
commit 1bc27495d6

View File

@ -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();
},
},