Skip to content

Commit c2b36cd

Browse files
committed
feat: prevent browser to invoke basic auth popup
1 parent 600b181 commit c2b36cd

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

app/api/common.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,30 @@ export async function requestOpenai(req: NextRequest) {
3030
controller.abort();
3131
}, 10 * 60 * 1000);
3232

33+
const fetchUrl = `${baseUrl}/${openaiPath}`;
34+
const fetchOptions: RequestInit = {
35+
headers: {
36+
"Content-Type": "application/json",
37+
Authorization: authValue,
38+
...(process.env.OPENAI_ORG_ID && {
39+
"OpenAI-Organization": process.env.OPENAI_ORG_ID,
40+
}),
41+
},
42+
cache: "no-store",
43+
method: req.method,
44+
body: req.body,
45+
signal: controller.signal,
46+
};
47+
3348
try {
34-
return await fetch(`${baseUrl}/${openaiPath}`, {
35-
headers: {
36-
"Content-Type": "application/json",
37-
Authorization: authValue,
38-
...(process.env.OPENAI_ORG_ID && {
39-
"OpenAI-Organization": process.env.OPENAI_ORG_ID,
40-
}),
41-
},
42-
cache: "no-store",
43-
method: req.method,
44-
body: req.body,
45-
signal: controller.signal,
46-
});
47-
} catch (err: unknown) {
48-
if (err instanceof Error && err.name === "AbortError") {
49-
console.log("Fetch aborted");
50-
} else {
51-
throw err;
49+
const res = await fetch(fetchUrl, fetchOptions);
50+
51+
if (res.status === 401) {
52+
// to prevent browser prompt for credentials
53+
res.headers.delete("www-authenticate");
5254
}
55+
56+
return res;
5357
} finally {
5458
clearTimeout(timeoutId);
5559
}

0 commit comments

Comments
 (0)