2024-07-20 22:23:58 +02:00
|
|
|
import type { ReadonlyHeaders } from "next/dist/server/web/spec-extension/adapters/headers";
|
|
|
|
|
|
2024-08-06 21:43:12 +02:00
|
|
|
import { extractBaseUrlFromHeaders } from "@homarr/common";
|
|
|
|
|
|
2024-07-20 22:23:58 +02:00
|
|
|
/**
|
|
|
|
|
* The redirect_uri is constructed to work behind a reverse proxy. It is constructed from the headers x-forwarded-proto and x-forwarded-host.
|
|
|
|
|
* @param headers
|
|
|
|
|
* @param pathname
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
2024-12-24 14:15:34 +01:00
|
|
|
export const createRedirectUri = (
|
|
|
|
|
headers: ReadonlyHeaders | null,
|
|
|
|
|
pathname: string,
|
|
|
|
|
fallbackProtocol: "http" | "https" = "http",
|
|
|
|
|
) => {
|
2024-07-20 22:23:58 +02:00
|
|
|
if (!headers) {
|
|
|
|
|
return pathname;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 14:15:34 +01:00
|
|
|
const baseUrl = extractBaseUrlFromHeaders(headers, fallbackProtocol);
|
2024-07-20 22:23:58 +02:00
|
|
|
|
|
|
|
|
const path = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
|
|
|
|
2024-08-06 21:43:12 +02:00
|
|
|
return `${baseUrl}${path}`;
|
2024-07-20 22:23:58 +02:00
|
|
|
};
|