25 lines
678 B
TypeScript
25 lines
678 B
TypeScript
import { headers } from "next/headers";
|
|
|
|
import { getEnv } from "@/lib/env";
|
|
|
|
export async function getRequestOrigin() {
|
|
const headerList = await headers();
|
|
const forwardedProto = headerList.get("x-forwarded-proto");
|
|
const forwardedHost = headerList.get("x-forwarded-host");
|
|
const host = forwardedHost || headerList.get("host");
|
|
|
|
if (!host) {
|
|
return getEnv().APP_URL;
|
|
}
|
|
|
|
const proto =
|
|
forwardedProto || (host.startsWith("localhost") || host.startsWith("127.0.0.1") ? "http" : "http");
|
|
|
|
return `${proto}://${host}`;
|
|
}
|
|
|
|
export function getOriginFromRequest(request: Request) {
|
|
const url = new URL(request.url);
|
|
return `${url.protocol}//${url.host}`;
|
|
}
|