261102_01_ENH: KART 로그인 연동을 위한 공용 JWT 쿠키 설정 및 로그아웃 처리 통일
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import type { LoginRequest, LoginUser } from "@kri/common";
|
||||
import {serverEnv} from "@/lib/env.server";
|
||||
import {setAuthCookie} from "@/lib/auth-cookie";
|
||||
import {createContactApi} from "@/lib/http-server";
|
||||
import {errorToResponse} from "@kri/common/http";
|
||||
import {CookieKeys} from "@/store/cookie/cookie-keys";
|
||||
|
||||
//?ㅼ젣 Backend
|
||||
type LoginResponse = {
|
||||
@@ -19,13 +18,7 @@ export const POST = async (req: NextRequest) => {
|
||||
const result = await contactApi.post<LoginResponse>("/auth/login", body);
|
||||
|
||||
const res = NextResponse.json(result.user, { status: 200 });
|
||||
res.cookies.set(CookieKeys.AuthToken, result.accessToken, {
|
||||
httpOnly: true,
|
||||
secure: serverEnv.ENV === "prod",
|
||||
sameSite: "lax",
|
||||
path: "/",
|
||||
maxAge: 24 * 60 * 60, // 24?쒓컙
|
||||
});
|
||||
setAuthCookie(res, result.accessToken);
|
||||
|
||||
return res;
|
||||
} catch (error : unknown) {
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import {serverEnv} from "@/lib/env.server";
|
||||
import {setAuthCookie} from "@/lib/auth-cookie";
|
||||
import {createContactApi} from "@/lib/http-server";
|
||||
import {errorToResponse} from "@kri/common/http";
|
||||
import {RouteContext} from "@/types/common";
|
||||
import {CookieKeys} from "@/store/cookie/cookie-keys";
|
||||
|
||||
export async function POST(
|
||||
_req: NextRequest,
|
||||
@@ -22,13 +21,7 @@ export async function POST(
|
||||
await contactApi.post<boolean>(url);
|
||||
const res = NextResponse.json({ ok: true });
|
||||
|
||||
res.cookies.set(CookieKeys.AuthToken, "", {
|
||||
httpOnly: true,
|
||||
secure: serverEnv.ENV === "prod",
|
||||
sameSite: "lax",
|
||||
path: "/",
|
||||
maxAge: 0,
|
||||
});
|
||||
setAuthCookie(res, "");
|
||||
|
||||
return res;
|
||||
} catch (error: unknown) {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user