From 68d9003f8d8d2adcba43cad6366eca5365110e48 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Tue, 5 Apr 2022 21:15:57 +0200 Subject: feat(web/ui): Add support for unauthenticated user access This change updates the web UI and API to support unauthenticated user access. Such functionality is helpful when there is just a single user that wants to try OpenDC. --- opendc-web/opendc-web-ui/src/auth.js | 42 +++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 13 deletions(-) (limited to 'opendc-web/opendc-web-ui/src/auth.js') diff --git a/opendc-web/opendc-web-ui/src/auth.js b/opendc-web/opendc-web-ui/src/auth.js index e670476c..3d6cf87c 100644 --- a/opendc-web/opendc-web-ui/src/auth.js +++ b/opendc-web/opendc-web-ui/src/auth.js @@ -23,14 +23,26 @@ import PropTypes from 'prop-types' import { Auth0Provider, useAuth0 } from '@auth0/auth0-react' import { useEffect } from 'react' +import { auth } from './config' /** - * Obtain the authentication context. + * Helper function to provide the authentication context in case Auth0 is not + * configured. */ -export function useAuth() { - return useAuth0() +function useAuthDev() { + return { + isAuthenticated: false, + isLoading: false, + logout: () => {}, + loginWithRedirect: () => {}, + } } +/** + * Obtain the authentication context. + */ +export const useAuth = auth.domain ? useAuth0 : useAuthDev + /** * Force the user to be authenticated or redirect to the homepage. */ @@ -51,16 +63,20 @@ export function useRequireAuth() { * AuthProvider which provides an authentication context. */ export function AuthProvider({ children }) { - return ( - - {children} - - ) + if (auth.domain) { + return ( + + {children} + + ) + } + + return children } AuthProvider.propTypes = { -- cgit v1.2.3