This commit is contained in:
root
2026-06-07 19:18:24 +08:00
parent a1daa040ef
commit 32b900547a
16 changed files with 127 additions and 427 deletions

View File

@@ -4,6 +4,8 @@
* 本地开发不设置 domain避免 IP 访问时 cookie 失效
*/
import { IS_DEVELOPMENT, IS_PRODUCTION } from "@/config/runtime-env";
const COOKIE_NAME = "pb_session";
const COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
@@ -15,18 +17,17 @@ export function getHostFromRequest(request: { headers: { get: (k: string) => str
}
export function getCookieOptions(clear = false, requestHost?: string) {
const isProd = process.env.NODE_ENV === "production";
const domain = process.env.AUTH_COOKIE_DOMAIN?.trim();
const opts: Record<string, unknown> = {
path: "/",
maxAge: clear ? 0 : COOKIE_MAX_AGE,
secure: isProd,
secure: IS_PRODUCTION,
sameSite: "lax" as const,
httpOnly: true,
};
if (domain && isProd) {
if (domain && IS_PRODUCTION) {
opts.domain = domain;
} else if (!isProd && requestHost && /^(\d{1,3}\.){3}\d{1,3}$/.test(requestHost)) {
} else if (IS_DEVELOPMENT && requestHost && /^(\d{1,3}\.){3}\d{1,3}$/.test(requestHost)) {
opts.domain = requestHost;
}
return opts;

View File

@@ -1,5 +1,5 @@
const isDev = process.env.NODE_ENV === "development";
import { IS_DEVELOPMENT } from "@/config/runtime-env";
export const SITE_URL =
process.env.NEXT_PUBLIC_SITE_URL ||
(isDev ? "http://localhost:3001" : "https://nomadcna.cn");
(IS_DEVELOPMENT ? "http://localhost:3001" : "https://nomadcna.cn");