mirror of
https://github.com/different-ai/openwork
synced 2026-04-26 09:35:17 +02:00
fix(web): preserve auth cookies in den proxy
This commit is contained in:
@@ -67,6 +67,24 @@ function buildUpstreamErrorResponse(status: number, error: string): Response {
|
||||
});
|
||||
}
|
||||
|
||||
function copySetCookieHeaders(upstreamHeaders: Headers, responseHeaders: Headers): void {
|
||||
const getSetCookie = (upstreamHeaders as Headers & { getSetCookie?: () => string[] }).getSetCookie;
|
||||
if (typeof getSetCookie === "function") {
|
||||
const cookies = getSetCookie.call(upstreamHeaders);
|
||||
for (const cookie of cookies) {
|
||||
if (cookie) {
|
||||
responseHeaders.append("set-cookie", cookie);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const cookie = upstreamHeaders.get("set-cookie");
|
||||
if (cookie) {
|
||||
responseHeaders.append("set-cookie", cookie);
|
||||
}
|
||||
}
|
||||
|
||||
function buildHeaders(request: NextRequest, contentType: string | null): Headers {
|
||||
const headers = new Headers();
|
||||
const copyHeaders = ["accept", "authorization", "cookie", "user-agent", "x-requested-with", "origin"];
|
||||
@@ -156,7 +174,7 @@ async function proxy(request: NextRequest, segments: string[] = []) {
|
||||
}
|
||||
|
||||
const responseHeaders = new Headers();
|
||||
const passThroughHeaders = ["content-type", "set-cookie", "location", "cache-control"];
|
||||
const passThroughHeaders = ["content-type", "location", "cache-control"];
|
||||
|
||||
for (const key of passThroughHeaders) {
|
||||
const value = upstream.headers.get(key);
|
||||
@@ -165,6 +183,8 @@ async function proxy(request: NextRequest, segments: string[] = []) {
|
||||
}
|
||||
}
|
||||
|
||||
copySetCookieHeaders(upstream.headers, responseHeaders);
|
||||
|
||||
const shouldDropBody = request.method === "HEAD" || NO_BODY_STATUS.has(upstream.status);
|
||||
|
||||
return new Response(shouldDropBody ? null : body, {
|
||||
|
||||
Reference in New Issue
Block a user