261102_01_ENH: KART 로그인 연동을 위한 공용 JWT 쿠키 설정 및 로그아웃 처리 통일

This commit is contained in:
2026-11-02 15:12:42 +09:00
parent 50a0cb1ef5
commit 273caf267d
6 changed files with 39 additions and 18 deletions

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

@@ -0,0 +1,31 @@
import "server-only";
import { NextResponse } from "next/server";
import { serverEnv } from "@/lib/env.server";
import { CookieKeys } from "@/store/cookie/cookie-keys";
export const setAuthCookie = (response: NextResponse, token: string) => {
const secure = serverEnv.ENV === "prod";
const options = {
httpOnly: true,
secure,
sameSite: "lax",
path: "/",
} as const;
const domain = secure ? serverEnv.AUTH_COOKIE_DOMAIN : undefined;
response.cookies.set(CookieKeys.AuthToken, token, {
...options,
...(domain ? { domain } : {}),
maxAge: token ? 24 * 60 * 60 : 0,
});
if (domain) {
// Expire the legacy host-only cookie without replacing the shared cookie header.
const legacy = new NextResponse();
legacy.cookies.set(CookieKeys.AuthToken, "", { ...options, maxAge: 0 });
for (const cookie of legacy.headers.getSetCookie()) {
response.headers.append("Set-Cookie", cookie);
}
}
};

View File

@@ -5,6 +5,8 @@ import { createEnv, env } from "@kri/common/config";
export const serverEnv = createEnv({
ENV: env.required("ENV", process.env.ENV),
AUTH_COOKIE_DOMAIN: env.optional("AUTH_COOKIE_DOMAIN", process.env.AUTH_COOKIE_DOMAIN),
CONTACT_SERVER: env.required("CONTACT_SERVER", process.env.CONTACT_SERVER),
API_CONTACT_SERVER: env.required("API_CONTACT_SERVER", process.env.API_CONTACT_SERVER),