261102_01_ENH: 프로젝트 접근 권한 확인 및 탭별 선택 상태 분리, 공용 인증 쿠키 설정

This commit is contained in:
2026-11-02 15:14:08 +09:00
parent 4bfa2af213
commit 69939f8d2b
18 changed files with 336 additions and 53 deletions

35
src/lib/auth-cookie.ts Normal file
View File

@@ -0,0 +1,35 @@
import "server-only";
import { NextResponse } from "next/server";
import { AUTH_COOKIE_MAX_AGE } from "@/const/common.const";
import { serverEnv } from "@/lib/env.server";
import { CookieKeys } from "@/store/cookie/cookie-keys";
/** Issue/renew the normal-login cookie, or expire it when the token is empty. */
export function setAuthCookie(response: NextResponse, token: string): void {
const production = serverEnv.ENV === "prod";
const domain = production ? serverEnv.AUTH_COOKIE_DOMAIN : undefined;
if (production && !domain)
throw new Error("AUTH_COOKIE_DOMAIN must be configured when ENV is prod.");
const options = {
httpOnly: true,
secure: production,
sameSite: "lax" as const,
path: "/",
};
// Serialize each cookie separately: ResponseCookies indexes cookies by name,
// so setting the shared and host-only variants on one instance loses a header.
const append = (value: string, cookieDomain: string | undefined, maxAge: number) => {
const cookieResponse = new NextResponse(null);
cookieResponse.cookies.set(CookieKeys.AuthToken, value, {
...options,
...(cookieDomain ? { domain: cookieDomain } : {}),
maxAge,
});
for (const header of cookieResponse.headers.getSetCookie())
response.headers.append("Set-Cookie", header);
};
if (production) append("", undefined, 0);
append(token, domain, token ? AUTH_COOKIE_MAX_AGE : 0);
}