Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

954 B
Raw Blame
type CookieAttributeUrls = {
  apiUrl: string;
  clientUrl: string;
  cookieDomain?: string;
};

export function getDefaultCookieAttributes({
  apiUrl,
  clientUrl,
  cookieDomain,
}: CookieAttributeUrls) {
  const parsedApiUrl = (() => {
    try {
      return new URL(apiUrl);
    } catch {
      return undefined;
    }
  })();
  const isHttps = parsedApiUrl?.protocol === "https:";
  const isCrossSubdomain = (() => {
    try {
      const apiHost = parsedApiUrl?.hostname;
      const clientHost = new URL(clientUrl).hostname;
      return (
        apiHost !== undefined &&
        apiHost !== clientHost &&
        apiHost !== "localhost" &&
        clientHost !== "localhost"
      );
    } catch {
      return false;
    }
  })();

  return {
    sameSite:
      isCrossSubdomain && isHttps ? ("none" as const) : ("lax" as const),
    secure: isHttps,
    partitioned: isCrossSubdomain && isHttps,
    domain: cookieDomain || undefined,
  };
}