diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2022-04-05 21:15:57 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2022-04-06 13:39:32 +0200 |
| commit | 68d9003f8d8d2adcba43cad6366eca5365110e48 (patch) | |
| tree | 8e9287ae4c738229e82ace3e9b39d33a2953f490 /opendc-web/opendc-web-ui/src/api/index.js | |
| parent | f2ff40b5170260289e99e0506525f0905f380907 (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/api/index.js')
| -rw-r--r-- | opendc-web/opendc-web-ui/src/api/index.js | 17 |
1 files changed, 11 insertions, 6 deletions
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() |
