261102_03_ENH: KART와 JWT 쿠키 처리 방식 통일 및 만료 시간 상수화
This commit is contained in:
@@ -5,3 +5,5 @@ export const FONT_SIZE = {
|
|||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export const ALLOW_REDIRECT = new Set(["complete", "screen", "quota"]);
|
export const ALLOW_REDIRECT = new Set(["complete", "screen", "quota"]);
|
||||||
|
|
||||||
|
export const AUTH_COOKIE_MAX_AGE = 24 * 60 * 60;
|
||||||
|
|||||||
@@ -1,31 +1,33 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
|
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
import { AUTH_COOKIE_MAX_AGE } from "@/const/commonConst";
|
||||||
import { serverEnv } from "@/lib/env.server";
|
import { serverEnv } from "@/lib/env.server";
|
||||||
import { CookieKeys } from "@/store/cookie/cookie-keys";
|
import { CookieKeys } from "@/store/cookie/cookie-keys";
|
||||||
|
|
||||||
export const setAuthCookie = (response: NextResponse, token: string) => {
|
/** Issue/renew the normal-login cookie, or expire it when the token is empty. */
|
||||||
const secure = serverEnv.ENV === "prod";
|
export function setAuthCookie(response: NextResponse, token: string): void {
|
||||||
const options = {
|
const production = serverEnv.ENV === "prod";
|
||||||
httpOnly: true,
|
|
||||||
secure,
|
|
||||||
sameSite: "lax",
|
|
||||||
path: "/",
|
|
||||||
} as const;
|
|
||||||
const domain = serverEnv.AUTH_COOKIE_DOMAIN;
|
const domain = serverEnv.AUTH_COOKIE_DOMAIN;
|
||||||
|
|
||||||
response.cookies.set(CookieKeys.AuthToken, token, {
|
const options = {
|
||||||
...options,
|
httpOnly: true,
|
||||||
...(domain ? { domain } : {}),
|
secure: production,
|
||||||
maxAge: token ? 24 * 60 * 60 : 0,
|
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 (domain) {
|
if (domain) append("", undefined, 0);
|
||||||
// Expire the legacy host-only cookie without replacing the shared cookie header.
|
append(token, domain, token ? AUTH_COOKIE_MAX_AGE : 0);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user