diff --git a/src/router.js b/src/router.js index 9331226f..aaa52e66 100644 --- a/src/router.js +++ b/src/router.js @@ -22,14 +22,16 @@ import { metaTagData, startingView, routePaths } from '@/utils/defaults'; Vue.use(Router); -/** - * Checks if the current user is either authenticated, - * or if authentication is not enabled - * @returns true if user logged in, or user management not enabled - */ +/* Checks if guest mode is enabled in appConfig */ +const isGuestEnabled = () => { + if (!config || !config.appConfig) return false; + return config.appConfig.enableGuestAccess || false; +}; + +/* Returns true if user is already authenticated, or if auth is not enabled */ const isAuthenticated = () => { const users = config.appConfig.auth; - return (!users || users.length === 0 || isLoggedIn(users)); + return (!users || users.length === 0 || isLoggedIn(users) || isGuestEnabled()); }; /* Get the users chosen starting view from app config, or return default */ @@ -94,13 +96,14 @@ const router = new Router({ appConfig: config.appConfig, }, beforeEnter: (to, from, next) => { - if (isAuthenticated()) router.push({ path: '/' }); + // If the user already logged in + guest mode not enabled, then redirect home + if (isAuthenticated() && !isGuestEnabled()) router.push({ path: '/' }); next(); }, }, { // The about app page path: routePaths.about, - name: 'about', + name: 'about', // We lazy load the About page so as to not slow down the app component: () => import(/* webpackChunkName: "about" */ './views/About.vue'), meta: makeMetaTags('About Dashy'), }, @@ -115,9 +118,9 @@ const router = new Router({ }); /** - * Before loading a route, check if the user has authentication enabled * - * if so, then ensure that they are correctly logged in as a valid user * - * If not logged in, prevent access and redirect them to the login page * + * Before loading a route, check if the user has authentication enabled + * if so, then ensure that they are correctly logged in as a valid user + * If not logged in, prevent all access and redirect them to login page * */ router.beforeEach((to, from, next) => { if (to.name !== 'login' && !isAuthenticated()) next({ name: 'login' }); @@ -131,5 +134,5 @@ router.afterEach((to) => { }); }); -// Export the now configured router +// All done - export the now configured router export default router; diff --git a/src/utils/Auth.js b/src/utils/Auth.js index 9ccca77c..9086e1f1 100644 --- a/src/utils/Auth.js +++ b/src/utils/Auth.js @@ -1,5 +1,5 @@ import sha256 from 'crypto-js/sha256'; -import { cookieKeys, localStorageKeys } from './defaults'; +import { cookieKeys, localStorageKeys, userStateEnum } from './defaults'; /** * Generates a 1-way hash, in order to be stored in local storage for authentication @@ -34,6 +34,12 @@ export const isLoggedIn = (users) => { return userAuthenticated; }; +/* Returns true if authentication is enabled */ +export const isAuthEnabled = (users) => (users && users.length > 0); + +/* Returns true if guest access is enabled */ +export const isGuestAccessEnabled = (appConfig) => appConfig.enableGuestAccess || false; + /** * Checks credentials entered by the user against those in the config * Returns an object containing a boolean indicating success/ failure @@ -107,3 +113,20 @@ export const isUserAdmin = (users) => { }); return isAdmin; }; + +/** + * Determines which button should display, based on the user type + * 0 = Auth not configured (don't show anything) + * 1 = Auth configured, and user logged in (show logout button) + * 2 = Auth configured, guest access enabled, not logged in (show login) + * Note that if auth is enabled, but not guest access, and user not logged in, + * then they will never be able to view the homepage, so no button needed + */ +export const getUserState = (appConfig) => { + const { notConfigured, loggedIn, guestAccess } = userStateEnum; // Numeric enum options + const users = appConfig.auth || []; // Get auth object + if (!isAuthEnabled(users)) return notConfigured; // No auth enabled + if (isLoggedIn(users)) return loggedIn; // User is logged in + if (isGuestAccessEnabled(appConfig || {})) return guestAccess; // Guest is viewing + return notConfigured; +}; diff --git a/src/utils/defaults.js b/src/utils/defaults.js index a1896251..4d3d400e 100644 --- a/src/utils/defaults.js +++ b/src/utils/defaults.js @@ -166,4 +166,11 @@ module.exports = { ], /* Use your own self-hosted Sentry instance. Only used if error reporting is turned on */ sentryDsn: 'https://3138ea85f15a4fa883a5b27a4dc8ee28@o937511.ingest.sentry.io/5887934', + /* A JS enum for indicating the user state, when guest mode + authentication is enabled */ + userStateEnum: { + notConfigured: 0, + loggedIn: 1, + guestAccess: 2, + notLoggedIn: 3, + }, };