's'
This commit is contained in:
@@ -1,10 +1,15 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { getAuthCookieDomain } from "@/app/lib/auth-cookie-domain";
|
||||||
|
|
||||||
const COOKIE_NAME = "pb_session";
|
const COOKIE_NAME = "pb_session";
|
||||||
const COOKIE_DOMAIN = process.env.AUTH_COOKIE_DOMAIN || ".hackrobot.cn";
|
|
||||||
|
|
||||||
export async function POST() {
|
export async function POST(request: NextRequest) {
|
||||||
|
const domain = getAuthCookieDomain(request);
|
||||||
const res = NextResponse.json({ ok: true });
|
const res = NextResponse.json({ ok: true });
|
||||||
res.cookies.set(COOKIE_NAME, "", { domain: COOKIE_DOMAIN, path: "/", maxAge: 0 });
|
res.cookies.set(COOKIE_NAME, "", {
|
||||||
|
...(domain && { domain }),
|
||||||
|
path: "/",
|
||||||
|
maxAge: 0,
|
||||||
|
});
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { getPocketBaseConfig } from "@/config/services.config";
|
import { getPocketBaseConfig } from "@/config/services.config";
|
||||||
|
import { getAuthCookieDomain } from "@/app/lib/auth-cookie-domain";
|
||||||
|
|
||||||
const COOKIE_NAME = "pb_session";
|
const COOKIE_NAME = "pb_session";
|
||||||
const COOKIE_DOMAIN = process.env.AUTH_COOKIE_DOMAIN || ".hackrobot.cn";
|
|
||||||
const COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
|
const COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
const cookie = request.cookies.get(COOKIE_NAME)?.value;
|
const cookie = request.cookies.get(COOKIE_NAME)?.value;
|
||||||
|
const domain = getAuthCookieDomain(request);
|
||||||
if (!cookie) {
|
if (!cookie) {
|
||||||
return NextResponse.json({ ok: true, user: null });
|
return NextResponse.json({ ok: true, user: null });
|
||||||
}
|
}
|
||||||
@@ -21,7 +22,7 @@ export async function GET(request: NextRequest) {
|
|||||||
});
|
});
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const r = NextResponse.json({ ok: true, user: null });
|
const r = NextResponse.json({ ok: true, user: null });
|
||||||
r.cookies.set(COOKIE_NAME, "", { domain: COOKIE_DOMAIN, path: "/", maxAge: 0 });
|
r.cookies.set(COOKIE_NAME, "", { ...(domain && { domain }), path: "/", maxAge: 0 });
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
@@ -40,7 +41,7 @@ export async function GET(request: NextRequest) {
|
|||||||
token: newToken,
|
token: newToken,
|
||||||
});
|
});
|
||||||
r.cookies.set(COOKIE_NAME, encoded, {
|
r.cookies.set(COOKIE_NAME, encoded, {
|
||||||
domain: COOKIE_DOMAIN,
|
...(domain && { domain }),
|
||||||
path: "/",
|
path: "/",
|
||||||
maxAge: COOKIE_MAX_AGE,
|
maxAge: COOKIE_MAX_AGE,
|
||||||
secure: process.env.NODE_ENV === "production",
|
secure: process.env.NODE_ENV === "production",
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { getAuthCookieDomain } from "@/app/lib/auth-cookie-domain";
|
||||||
|
|
||||||
const COOKIE_NAME = "pb_session";
|
const COOKIE_NAME = "pb_session";
|
||||||
const COOKIE_MAX_AGE = 60 * 60 * 24 * 365; // 365 天,登录态持续有效直至用户主动退出
|
const COOKIE_MAX_AGE = 60 * 60 * 24 * 365; // 365 天,登录态持续有效直至用户主动退出
|
||||||
const COOKIE_DOMAIN = process.env.AUTH_COOKIE_DOMAIN || ".hackrobot.cn";
|
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
const body = await request.json().catch(() => ({}));
|
const body = await request.json().catch(() => ({}));
|
||||||
@@ -19,9 +19,10 @@ export async function POST(request: NextRequest) {
|
|||||||
});
|
});
|
||||||
const encoded = Buffer.from(payload, "utf-8").toString("base64url");
|
const encoded = Buffer.from(payload, "utf-8").toString("base64url");
|
||||||
|
|
||||||
|
const domain = getAuthCookieDomain(request);
|
||||||
const res = NextResponse.json({ ok: true });
|
const res = NextResponse.json({ ok: true });
|
||||||
res.cookies.set(COOKIE_NAME, encoded, {
|
res.cookies.set(COOKIE_NAME, encoded, {
|
||||||
domain: COOKIE_DOMAIN,
|
...(domain && { domain }),
|
||||||
path: "/",
|
path: "/",
|
||||||
maxAge: COOKIE_MAX_AGE,
|
maxAge: COOKIE_MAX_AGE,
|
||||||
secure: process.env.NODE_ENV === "production",
|
secure: process.env.NODE_ENV === "production",
|
||||||
|
|||||||
11
app/lib/auth-cookie-domain.ts
Normal file
11
app/lib/auth-cookie-domain.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* 根据请求 host 返回 Cookie domain。
|
||||||
|
* localhost 时不设 domain,生产环境用根域实现跨站 SSO。
|
||||||
|
*/
|
||||||
|
export function getAuthCookieDomain(request: { headers: { get: (name: string) => string | null } }): string | undefined {
|
||||||
|
const envDomain = process.env.AUTH_COOKIE_DOMAIN;
|
||||||
|
if (envDomain) return envDomain;
|
||||||
|
const host = request.headers.get("host") ?? "";
|
||||||
|
if (host.includes("localhost") || host.startsWith("127.0.0.1")) return undefined;
|
||||||
|
return process.env.AUTH_COOKIE_DOMAIN || ".hackrobot.cn";
|
||||||
|
}
|
||||||
@@ -37,11 +37,7 @@ export function VipHeader({ isLoggedIn, userName, onLogout }: VipHeaderProps) {
|
|||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : null}
|
||||||
<Link href="/#login" className={styles.loginLink}>
|
|
||||||
登录
|
|
||||||
</Link>
|
|
||||||
)}
|
|
||||||
</nav>
|
</nav>
|
||||||
</header>
|
</header>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user