자료관 앱을 원앤올 공통 패키지 기준으로 최신화
This commit is contained in:
@@ -1,29 +1,48 @@
|
||||
import {
|
||||
ACCESS_TOKEN,
|
||||
AUTH_USER_ID,
|
||||
CLAIM_NAME,
|
||||
REFRESH_TOKEN,
|
||||
SERVER_API_URL,
|
||||
SITE_ID,
|
||||
} from "@/shared/config";
|
||||
/**
|
||||
* 📄 Proxy Route
|
||||
* API 프록시 라우트
|
||||
*
|
||||
* - 서버 API로의 요청 프록시 처리
|
||||
* - 쿠키 기반 인증 토큰 및 리프레시 토큰 관리
|
||||
* - POST 및 PUT 요청 핸들링
|
||||
* - 사용자 인증 정보 검증 및 응답 처리
|
||||
* - 에러 핸들링 및 상태 코드 반환
|
||||
* - 사이트 ID 및 헤더 설정
|
||||
*/
|
||||
import { cookies } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
import { clientIpHeaders } from "../../../../client-ip";
|
||||
import { protectedRagProxyRoute } from "@/shared/api/rag-proxy-boundary";
|
||||
import { SERVER_API_URL, SITE_ID } from "@/shared/config/env";
|
||||
|
||||
const ACCESS_TOKEN = process.env.NEXT_PUBLIC_ACCESS_TOKEN || "access-token";
|
||||
const AUTH_USER_ID = process.env.NEXT_PUBLIC_AUTH_USER_ID || "token-id";
|
||||
const CLAIM_NAME = process.env.NEXT_PUBLIC_CLAIM_NAME || "Authorization";
|
||||
const REFRESH_TOKEN = process.env.NEXT_PUBLIC_REFRESH_TOKEN || "refresh-token";
|
||||
|
||||
async function handler(
|
||||
request: Request,
|
||||
params: { url: string[] },
|
||||
method: "GET" | "POST" | "PUT" | "DELETE",
|
||||
) {
|
||||
if (protectedRagProxyRoute(`/${params.url.join("/")}`)) {
|
||||
return NextResponse.json({ message: "RAG_FRONTEND_ROUTE_REQUIRED" }, { status: 403 });
|
||||
}
|
||||
const cookieStore = await cookies();
|
||||
const isRefreshHandler = params.url.includes("refresh");
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
"X-Site-Id": SITE_ID,
|
||||
// 브라우저 IP (로그인/접속 로그용)
|
||||
...clientIpHeaders(request),
|
||||
};
|
||||
const deviceId = request.headers.get("X-Device-Id");
|
||||
if (deviceId) {
|
||||
headers["X-Device-Id"] = deviceId;
|
||||
}
|
||||
let body = {};
|
||||
const isRefreshHandler = params.url.includes("refresh");
|
||||
|
||||
if (!isRefreshHandler) {
|
||||
headers["Content-Type"] = "application/json";
|
||||
}
|
||||
|
||||
// silent refresh
|
||||
if (isRefreshHandler) {
|
||||
@@ -43,13 +62,20 @@ async function handler(
|
||||
const result = await fetch(`${SERVER_API_URL}/${params.url.join("/")}`, {
|
||||
method,
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
...(isRefreshHandler ? {} : { body: JSON.stringify(body) }),
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
const contentType = result.headers.get("content-type") ?? "";
|
||||
const errorBody = contentType.includes("application/json")
|
||||
? await result.json().catch(() => null)
|
||||
: await result.text().catch(() => "");
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: "Invalid Credentials" },
|
||||
{ status: 401 },
|
||||
typeof errorBody === "object" && errorBody !== null
|
||||
? errorBody
|
||||
: { message: errorBody || result.statusText || "Server Error" },
|
||||
{ status: result.status },
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user