Implements guest access checking functions

This commit is contained in:
Alicia Sykes
2021-08-18 21:23:39 +01:00
parent e4d7660712
commit 39ee63ebd3
3 changed files with 46 additions and 13 deletions

View File

@@ -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;
};