From 0da821d97733ab01289f6ac11d3962effe918849 Mon Sep 17 00:00:00 2001 From: Anurag chavan <118217089+anuragchvn-blip@users.noreply.github.com> Date: Thu, 6 Mar 2025 02:06:34 +0530 Subject: [PATCH] fix: Improve authentication handling in fetchWithAuth. --- src/lib/fetch.ts | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/src/lib/fetch.ts b/src/lib/fetch.ts index c3ec5f42..471ae50f 100644 --- a/src/lib/fetch.ts +++ b/src/lib/fetch.ts @@ -1,28 +1,3 @@ -// @ts-ignore -import nodeFetch, { Headers as NodeFetchHeaders } from '@supabase/node-fetch' - -type Fetch = typeof fetch - -export const resolveFetch = (customFetch?: Fetch): Fetch => { - let _fetch: Fetch - if (customFetch) { - _fetch = customFetch - } else if (typeof fetch === 'undefined') { - _fetch = nodeFetch as unknown as Fetch - } else { - _fetch = fetch - } - return (...args: Parameters) => _fetch(...args) -} - -export const resolveHeadersConstructor = () => { - if (typeof Headers === 'undefined') { - return NodeFetchHeaders - } - - return Headers -} - export const fetchWithAuth = ( supabaseKey: string, getAccessToken: () => Promise, @@ -32,7 +7,14 @@ export const fetchWithAuth = ( const HeadersConstructor = resolveHeadersConstructor() return async (input, init) => { - const accessToken = (await getAccessToken()) ?? supabaseKey + let accessToken: string | null + try { + accessToken = (await getAccessToken()) ?? supabaseKey + } catch (error) { + console.error("Error retrieving access token:", error) + accessToken = supabaseKey + } + let headers = new HeadersConstructor(init?.headers) if (!headers.has('apikey')) {