Skip to content

Commit 9262151

Browse files
committed
Re-auth upon 401 when POSTing
1 parent 03e583b commit 9262151

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/client/sse.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,17 @@ export class SSEClientTransport implements Transport {
195195
};
196196

197197
const response = await fetch(this._endpoint, init);
198-
199198
if (!response.ok) {
199+
if (response.status === 401 && this._authProvider) {
200+
const result = await auth(this._authProvider, { serverUrl: this._url });
201+
if (result !== "AUTHORIZED") {
202+
throw new Error("Unauthorized");
203+
}
204+
205+
// Purposely _not_ awaited, so we don't call onerror twice
206+
return this.send(message);
207+
}
208+
200209
const text = await response.text().catch(() => null);
201210
throw new Error(
202211
`Error POSTing to endpoint (HTTP ${response.status}): ${text}`,

0 commit comments

Comments
 (0)