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/api/index.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'opendc-web/opendc-web-ui/src/api') diff --git a/opendc-web/opendc-web-ui/src/api/index.js b/opendc-web/opendc-web-ui/src/api/index.js index 1a9877d0..75751658 100644 --- a/opendc-web/opendc-web-ui/src/api/index.js +++ b/opendc-web/opendc-web-ui/src/api/index.js @@ -20,7 +20,7 @@ * SOFTWARE. */ -const apiUrl = process.env.NEXT_PUBLIC_API_BASE_URL +import { apiUrl } from '../config' /** * Send the specified request to the OpenDC API. @@ -31,14 +31,19 @@ const apiUrl = process.env.NEXT_PUBLIC_API_BASE_URL * @param body The body of the request. */ export async function request(auth, path, method = 'GET', body) { + const headers = { + 'Content-Type': 'application/json', + } + const { getAccessTokenSilently } = auth - const token = await getAccessTokenSilently() + if (getAccessTokenSilently) { + const token = await getAccessTokenSilently() + headers['Authorization'] = `Bearer ${token}` + } + const response = await fetch(`${apiUrl}/${path}`, { method: method, - headers: { - Authorization: `Bearer ${token}`, - 'Content-Type': 'application/json', - }, + headers: headers, body: body && JSON.stringify(body), }) const json = await response.json() -- cgit v1.2.3