261102_01_ENH: KART 로그인 연동을 위한 공용 JWT 쿠키 설정 및 로그아웃 처리 통일
This commit is contained in:
31
src/lib/auth-cookie.ts
Normal file
31
src/lib/auth-cookie.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user