diff --git a/src/lib/utils.ts b/src/lib/utils.ts index c58c03a..110f05e 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -85,16 +85,20 @@ export async function connectToRemoteServer( // Create transport with eventSourceInit to pass Authorization header if present const eventSourceInit = { - fetch: (url: string | URL, init: RequestInit | undefined) => { - return fetch(url, { - ...init, - headers: { - ...init?.headers, - ...headers, - }, - }) + fetch: (url: string | URL, init?: RequestInit) => { + return Promise.resolve(authProvider?.tokens?.()).then((tokens) => + fetch(url, { + ...init, + headers: { + ...(init?.headers as Record | undefined), + ...headers, + ...(tokens?.access_token ? { Authorization: `Bearer ${tokens.access_token}` } : {}), + Accept: "text/event-stream", + } as Record, + }) + ); }, - } + }; const transport = new SSEClientTransport(url, { authProvider,