summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/auth.js
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2022-04-05 21:15:57 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2022-04-06 13:39:32 +0200
commit68d9003f8d8d2adcba43cad6366eca5365110e48 (patch)
tree8e9287ae4c738229e82ace3e9b39d33a2953f490 /opendc-web/opendc-web-ui/src/auth.js
parentf2ff40b5170260289e99e0506525f0905f380907 (diff)
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.
Diffstat (limited to 'opendc-web/opendc-web-ui/src/auth.js')
-rw-r--r--opendc-web/opendc-web-ui/src/auth.js42
1 files changed, 29 insertions, 13 deletions
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,15 +23,27 @@
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.
*/
export function useRequireAuth() {
@@ -51,16 +63,20 @@ export function useRequireAuth() {
* AuthProvider which provides an authentication context.
*/
export function AuthProvider({ children }) {
- return (
- <Auth0Provider
- domain={process.env.NEXT_PUBLIC_AUTH0_DOMAIN}
- clientId={process.env.NEXT_PUBLIC_AUTH0_CLIENT_ID}
- redirectUri={global.window && global.window.location.origin}
- audience={process.env.NEXT_PUBLIC_AUTH0_AUDIENCE}
- >
- {children}
- </Auth0Provider>
- )
+ if (auth.domain) {
+ return (
+ <Auth0Provider
+ domain={auth.domain}
+ clientId={auth.clientId}
+ redirectUri={auth.redirectUri}
+ audience={auth.audience}
+ >
+ {children}
+ </Auth0Provider>
+ )
+ }
+
+ return children
}
AuthProvider.propTypes = {