diff --git a/src/app/(protected)/dashboard/[id]/edit/layout.tsx b/src/app/(protected)/dashboard/[id]/edit/layout.tsx new file mode 100644 index 0000000..08c145a --- /dev/null +++ b/src/app/(protected)/dashboard/[id]/edit/layout.tsx @@ -0,0 +1,29 @@ +import { cookies } from "next/headers"; +import { redirect } from "next/navigation"; +import { apiRequest } from "@/lib/apiRequest"; +import { Dashboard } from "@/features/dashboard/types"; + +export default async function Layout({ + children, + params, +}: { + children: React.ReactNode; + params: any; +}) { + const cookieStore = await cookies(); + const cookie = cookieStore.get("accessToken")?.value; + const cookieHeader = cookie ? `accessToken=${cookie}` : ""; + + const { id } = params as { id: string }; + + const dashboard = await apiRequest(`/dashboards/${id}`, { + method: "GET", + cookieHeader, + }); + + if (!dashboard.createdByMe) { + redirect(`/dashboard/${id}`); + } + + return <>{children}; +} diff --git a/src/app/(protected)/dashboard/[id]/edit/page.tsx b/src/app/(protected)/dashboard/[id]/edit/page.tsx index 6fe326f..58ec974 100644 --- a/src/app/(protected)/dashboard/[id]/edit/page.tsx +++ b/src/app/(protected)/dashboard/[id]/edit/page.tsx @@ -116,6 +116,9 @@ export default function DashboardIdEdit() { setDisplayName(dashboardName); alert("대시보드가 성공적으로 수정되었습니다."); + + queryClient.invalidateQueries({ queryKey: ["dashboards"] }); + queryClient.invalidateQueries({ queryKey: ["dashboard", dashboardId] }); } catch (e) { console.error("대시보드 수정 실패", e); alert("대시보드 수정에 실패했습니다."); diff --git a/src/lib/apiRequest.ts b/src/lib/apiRequest.ts index c3b612b..ad239a7 100644 --- a/src/lib/apiRequest.ts +++ b/src/lib/apiRequest.ts @@ -1,15 +1,18 @@ -const BASE_URL = "/api/proxy"; +const isServer = typeof window === "undefined"; + +const BASE_URL = isServer ? `${process.env.NEXT_PUBLIC_FRONT_URL}/api/proxy` : "/api/proxy"; interface FetchOptions extends Omit { isFormData?: boolean; data?: unknown; + cookieHeader?: string; } export async function apiRequest( endpoint: string, options: FetchOptions = {}, ): Promise { - const { isFormData, headers, data, ...rest } = options; + const { isFormData, headers, data, cookieHeader, ...rest } = options; const fetchOptions: RequestInit = { ...rest, @@ -19,6 +22,13 @@ export async function apiRequest( }, }; + if (cookieHeader) { + fetchOptions.headers = { + ...fetchOptions.headers, + Cookie: cookieHeader, + }; + } + if (!isFormData && data !== undefined) { fetchOptions.headers = { ...fetchOptions.headers,