diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2022-10-27 15:03:45 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2022-10-27 16:24:39 +0200 |
| commit | 5e2a42a58b2cf4d1b94d5568e7c74373a4759bc3 (patch) | |
| tree | 0ae9f9489a55b398365c0dcb6f152fc32496ec05 /opendc-web | |
| parent | 11d14bee5c1ef79b0122ceed5a6cfa6ad62b81e7 (diff) | |
fix(web/ui): Default to anonymous auth domain
This change updates the auth code in the OpenDC web UI to default to
the anonymous auth domain if no configuration is provided.
Diffstat (limited to 'opendc-web')
| -rw-r--r-- | opendc-web/opendc-web-ui/src/auth.js | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/opendc-web/opendc-web-ui/src/auth.js b/opendc-web/opendc-web-ui/src/auth.js index 3d6cf87c..8c88f526 100644 --- a/opendc-web/opendc-web-ui/src/auth.js +++ b/opendc-web/opendc-web-ui/src/auth.js @@ -27,10 +27,11 @@ import { auth } from './config' /** * Helper function to provide the authentication context in case Auth0 is not - * configured. + * configured and the user is anonymous. */ -function useAuthDev() { +function useAnonymousAuth() { return { + isAnonymous: true, isAuthenticated: false, isLoading: false, logout: () => {}, @@ -39,15 +40,17 @@ function useAuthDev() { } /** - * Obtain the authentication context. + * Determine whether the auth domain is anonymous. */ -export const useAuth = auth.domain ? useAuth0 : useAuthDev +function isAnonymousDomain(config) { + return !config.domain || config.domain === '%%NEXT_PUBLIC_AUTH0_DOMAIN%%' +} /** * Force the user to be authenticated or redirect to the homepage. */ -export function useRequireAuth() { - const auth = useAuth() +function useRequireAuth0() { + const auth = useAuth0() const { loginWithRedirect, isLoading, isAuthenticated } = auth useEffect(() => { @@ -55,21 +58,31 @@ export function useRequireAuth() { loginWithRedirect() } }, [loginWithRedirect, isLoading, isAuthenticated]) - - return auth } /** + * Obtain the authentication context. + */ +export const useAuth = isAnonymousDomain(auth) ? useAnonymousAuth : useAuth0 + +/** + * Force the user to be authenticated or redirect to the homepage. + */ +export const useRequireAuth = isAnonymousDomain(auth) ? () => {} : useRequireAuth0 + +/** * AuthProvider which provides an authentication context. */ export function AuthProvider({ children }) { - if (auth.domain) { + const authConfig = auth + + if (!isAnonymousDomain(authConfig)) { return ( <Auth0Provider - domain={auth.domain} - clientId={auth.clientId} - redirectUri={auth.redirectUri} - audience={auth.audience} + domain={authConfig.domain} + clientId={authConfig.clientId} + redirectUri={authConfig.redirectUri} + audience={authConfig.audience} > {children} </Auth0Provider> |
