fix(auth): avoid cookie domain on IP hosts
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled

This commit is contained in:
Your Name 2026-02-14 01:13:56 +08:00
parent fc063df878
commit 565e29c74f

View File

@ -1,10 +1,13 @@
import type { NextAuthConfig } from "next-auth";
import { isIP } from "node:net";
export function buildAuthConfig(authUrl?: string | null): NextAuthConfig {
const resolvedAuthUrl = authUrl || process.env.AUTH_URL || "";
const isSecure = resolvedAuthUrl.startsWith("https://");
const hostName = resolvedAuthUrl ? new URL(resolvedAuthUrl).hostname : undefined;
const cookieDomain = hostName && hostName !== "localhost" ? hostName : undefined;
// Browsers may reject `Domain` cookies for IP hosts; keep them host-only in that case.
const cookieDomain =
hostName && hostName !== "localhost" && isIP(hostName) === 0 ? hostName : undefined;
return {
trustHost: true,