From 3eb7b8b84a0b14964a290dc7307842a2dcf91323 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Sat, 7 Nov 2020 16:44:11 -0800 Subject: [PATCH] Support API calls without auth and without cors --- web/.env.production | 2 -- web/utils/apis.ts | 19 +++++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/web/.env.production b/web/.env.production index d307f0c91..1fad60181 100644 --- a/web/.env.production +++ b/web/.env.production @@ -1,3 +1 @@ -NEXT_PUBLIC_ADMIN_USERNAME=admin -NEXT_PUBLIC_ADMIN_STREAMKEY=abc123 NEXT_PUBLIC_API_HOST=/ \ No newline at end of file diff --git a/web/utils/apis.ts b/web/utils/apis.ts index 4085631ba..d9dc1009b 100644 --- a/web/utils/apis.ts +++ b/web/utils/apis.ts @@ -37,16 +37,19 @@ export const LOGS_WARN = `${API_LOCATION}logs/warnings`; const GITHUB_RELEASE_URL = "https://api.github.com/repos/owncast/owncast/releases/latest"; export async function fetchData(url) { - const encoded = btoa(`${ADMIN_USERNAME}:${ADMIN_STREAMKEY}`); + let options: RequestInit = {}; + + if (ADMIN_USERNAME && ADMIN_STREAMKEY) { + const encoded = btoa(`${ADMIN_USERNAME}:${ADMIN_STREAMKEY}`); + options.headers = { + 'Authorization': `Basic ${encoded}` + } + options.mode = 'cors'; + options.credentials = 'include' + } try { - const response = await fetch(url, { - headers: { - 'Authorization': `Basic ${encoded}`, - }, - mode: 'cors', - credentials: 'include', - }); + const response = await fetch(url, options); if (!response.ok) { const message = `An error has occured: ${response.status}`; throw new Error(message);