2025-12-30 17:00:18 +09:00
|
|
|
|
/**
|
|
|
|
|
|
* 전역 Fetch Wrapper
|
|
|
|
|
|
*
|
|
|
|
|
|
* 모든 Server Actions에서 사용할 공통 fetch 함수
|
2026-01-30 14:16:17 +09:00
|
|
|
|
* - 401 에러 자동 감지 및 토큰 자동 갱신 (authenticatedFetch 게이트웨이 위임)
|
2025-12-30 17:00:18 +09:00
|
|
|
|
* - 일관된 에러 처리
|
|
|
|
|
|
* - 헤더 자동 설정
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2026-01-30 14:16:17 +09:00
|
|
|
|
import { cookies } from 'next/headers';
|
2025-12-30 17:00:18 +09:00
|
|
|
|
import { redirect } from 'next/navigation';
|
2026-01-08 18:41:15 +09:00
|
|
|
|
import { isNextRedirectError } from '@/lib/utils/redirect-error';
|
2025-12-30 17:00:18 +09:00
|
|
|
|
import { createErrorResponse, type ApiErrorResponse } from './errors';
|
2026-01-30 14:16:17 +09:00
|
|
|
|
import { authenticatedFetch } from './authenticated-fetch';
|
2025-12-30 17:00:18 +09:00
|
|
|
|
|
2026-01-09 11:00:39 +09:00
|
|
|
|
/**
|
|
|
|
|
|
* 토큰 쿠키 삭제 (리프레시 실패 또는 인증 만료 시)
|
|
|
|
|
|
*
|
|
|
|
|
|
* ⚠️ 중요: redirect('/login') 호출 전에 반드시 실행해야 함
|
|
|
|
|
|
* 쿠키가 남아있으면 미들웨어가 "인증됨"으로 판단하여 무한 루프 발생
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function clearTokenCookies() {
|
|
|
|
|
|
const cookieStore = await cookies();
|
|
|
|
|
|
|
|
|
|
|
|
cookieStore.delete('access_token');
|
|
|
|
|
|
cookieStore.delete('refresh_token');
|
|
|
|
|
|
cookieStore.delete('token_refreshed_at');
|
|
|
|
|
|
cookieStore.delete('is_authenticated');
|
|
|
|
|
|
|
2026-01-30 14:16:17 +09:00
|
|
|
|
console.log('🗑️ [serverFetch] 토큰 쿠키 삭제 완료');
|
2026-01-09 11:00:39 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 17:00:18 +09:00
|
|
|
|
/**
|
|
|
|
|
|
* 새 토큰을 쿠키에 저장
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function setNewTokenCookies(tokens: {
|
|
|
|
|
|
accessToken?: string;
|
|
|
|
|
|
refreshToken?: string;
|
|
|
|
|
|
expiresIn?: number;
|
|
|
|
|
|
}) {
|
|
|
|
|
|
const cookieStore = await cookies();
|
|
|
|
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
|
|
|
|
|
|
|
|
|
|
|
if (tokens.accessToken) {
|
|
|
|
|
|
cookieStore.set('access_token', tokens.accessToken, {
|
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
|
secure: isProduction,
|
|
|
|
|
|
sameSite: 'lax',
|
|
|
|
|
|
path: '/',
|
|
|
|
|
|
maxAge: tokens.expiresIn || 7200,
|
|
|
|
|
|
});
|
2025-12-31 18:40:50 +09:00
|
|
|
|
|
2026-01-30 14:16:17 +09:00
|
|
|
|
// 토큰 갱신 신호 쿠키 (클라이언트 useMenuPolling 감지용)
|
2025-12-31 18:40:50 +09:00
|
|
|
|
cookieStore.set('token_refreshed_at', Date.now().toString(), {
|
|
|
|
|
|
httpOnly: false,
|
|
|
|
|
|
secure: isProduction,
|
|
|
|
|
|
sameSite: 'lax',
|
|
|
|
|
|
path: '/',
|
|
|
|
|
|
maxAge: 60, // 1분 후 자동 삭제
|
|
|
|
|
|
});
|
|
|
|
|
|
console.log('🔔 [setNewTokenCookies] token_refreshed_at 신호 쿠키 설정');
|
2025-12-30 17:00:18 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (tokens.refreshToken) {
|
|
|
|
|
|
cookieStore.set('refresh_token', tokens.refreshToken, {
|
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
|
secure: isProduction,
|
|
|
|
|
|
sameSite: 'lax',
|
|
|
|
|
|
path: '/',
|
|
|
|
|
|
maxAge: 604800, // 7 days
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* API 헤더 생성 (Server Side)
|
|
|
|
|
|
*/
|
|
|
|
|
|
export async function getServerApiHeaders(token?: string): Promise<HeadersInit> {
|
|
|
|
|
|
const cookieStore = await cookies();
|
2026-01-30 14:16:17 +09:00
|
|
|
|
const accessToken = token || cookieStore.get('access_token')?.value;
|
2025-12-30 17:00:18 +09:00
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
'Accept': 'application/json',
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
'Authorization': accessToken ? `Bearer ${accessToken}` : '',
|
|
|
|
|
|
'X-API-KEY': process.env.API_KEY || '',
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Server Action용 Fetch Wrapper
|
|
|
|
|
|
*
|
2026-01-30 14:16:17 +09:00
|
|
|
|
* 401 감지 → refresh → retry 는 authenticatedFetch 게이트웨이에 위임.
|
|
|
|
|
|
* 이 함수는 쿠키 읽기/설정/삭제, 리다이렉트만 담당.
|
2025-12-30 17:00:18 +09:00
|
|
|
|
*
|
|
|
|
|
|
* @example
|
|
|
|
|
|
* ```typescript
|
|
|
|
|
|
* const { response, error } = await serverFetch(url, { method: 'GET' });
|
2026-01-30 14:16:17 +09:00
|
|
|
|
* if (error) return error;
|
2025-12-30 17:00:18 +09:00
|
|
|
|
* // response 사용...
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*/
|
|
|
|
|
|
export async function serverFetch(
|
|
|
|
|
|
url: string,
|
refactor(WEB): Server Component → Client Component 전면 마이그레이션
- 53개 페이지를 Server Component에서 Client Component로 변환
- Next.js 15에서 Server Component 렌더링 중 쿠키 수정 불가 이슈 해결
- 폐쇄형 ERP 시스템 특성상 SEO 불필요, Client Component 사용이 적합
주요 변경사항:
- 모든 페이지에 'use client' 지시어 추가
- use(params) 훅으로 async params 처리
- useState + useEffect로 데이터 페칭 패턴 적용
- skipTokenRefresh 옵션 및 관련 코드 제거 (더 이상 필요 없음)
변환된 페이지:
- Settings: 4개 (account-info, notification-settings, permissions, popup-management)
- Accounting: 9개 (vendors, sales, deposits, bills, withdrawals, expected-expenses, bad-debt-collection)
- Sales: 4개 (quote-management, pricing-management)
- Production/Quality/Master-data: 6개
- Material/Outbound: 4개
- Construction: 22개
- Other: 4개 (payment-history, subscription, dev/test-urls)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-09 19:19:37 +09:00
|
|
|
|
options?: RequestInit & {
|
|
|
|
|
|
skipAuthCheck?: boolean;
|
|
|
|
|
|
}
|
2025-12-30 17:00:18 +09:00
|
|
|
|
): Promise<{ response: Response | null; error: ApiErrorResponse | null }> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const cookieStore = await cookies();
|
2026-01-30 14:16:17 +09:00
|
|
|
|
const refreshToken = cookieStore.get('refresh_token')?.value;
|
2025-12-30 17:00:18 +09:00
|
|
|
|
|
2025-12-30 22:54:24 +09:00
|
|
|
|
const baseHeaders = await getServerApiHeaders() as Record<string, string>;
|
|
|
|
|
|
|
|
|
|
|
|
// FormData일 경우 Content-Type을 제외 (브라우저가 자동 설정)
|
|
|
|
|
|
const isFormData = options?.body instanceof FormData;
|
2026-01-30 14:16:17 +09:00
|
|
|
|
const requestHeaders: Record<string, string> = isFormData
|
2025-12-30 22:54:24 +09:00
|
|
|
|
? {
|
|
|
|
|
|
Accept: baseHeaders.Accept,
|
|
|
|
|
|
Authorization: baseHeaders.Authorization,
|
|
|
|
|
|
'X-API-KEY': baseHeaders['X-API-KEY'],
|
|
|
|
|
|
}
|
|
|
|
|
|
: baseHeaders;
|
2025-12-30 17:00:18 +09:00
|
|
|
|
|
2026-01-30 14:16:17 +09:00
|
|
|
|
// authenticatedFetch 게이트웨이로 요청 실행
|
|
|
|
|
|
// skipAuthCheck=true면 refreshToken을 넘기지 않아 401 시 갱신 안 함
|
|
|
|
|
|
const { response, newTokens, authFailed } = await authenticatedFetch(
|
|
|
|
|
|
url,
|
|
|
|
|
|
{
|
|
|
|
|
|
...options,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
...requestHeaders,
|
|
|
|
|
|
...options?.headers,
|
|
|
|
|
|
},
|
|
|
|
|
|
cache: options?.cache || 'no-store',
|
2025-12-30 17:00:18 +09:00
|
|
|
|
},
|
2026-01-30 14:16:17 +09:00
|
|
|
|
options?.skipAuthCheck ? undefined : refreshToken,
|
|
|
|
|
|
'serverFetch'
|
|
|
|
|
|
);
|
2025-12-30 17:00:18 +09:00
|
|
|
|
|
2026-01-30 14:16:17 +09:00
|
|
|
|
// 새 토큰 → 쿠키 저장
|
|
|
|
|
|
if (newTokens) {
|
|
|
|
|
|
await setNewTokenCookies(newTokens);
|
|
|
|
|
|
}
|
2025-12-30 17:00:18 +09:00
|
|
|
|
|
2026-01-30 14:16:17 +09:00
|
|
|
|
// 인증 실패 → 쿠키 삭제 + 로그인 리다이렉트
|
|
|
|
|
|
if (authFailed) {
|
|
|
|
|
|
await clearTokenCookies();
|
2025-12-30 17:00:18 +09:00
|
|
|
|
redirect('/login');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 403 Forbidden
|
|
|
|
|
|
if (response.status === 403) {
|
|
|
|
|
|
console.warn(`[serverFetch] 403 Forbidden: ${url}`);
|
|
|
|
|
|
return {
|
|
|
|
|
|
response: null,
|
|
|
|
|
|
error: createErrorResponse(403, '접근 권한이 없습니다.', 'FORBIDDEN'),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return { response, error: null };
|
|
|
|
|
|
} catch (error) {
|
2026-01-30 14:16:17 +09:00
|
|
|
|
// Next.js 15: redirect()는 특수한 에러를 throw하므로 다시 throw
|
2026-01-08 18:41:15 +09:00
|
|
|
|
if (isNextRedirectError(error)) throw error;
|
2025-12-30 17:00:18 +09:00
|
|
|
|
console.error(`[serverFetch] Network error: ${url}`, error);
|
|
|
|
|
|
return {
|
|
|
|
|
|
response: null,
|
|
|
|
|
|
error: createErrorResponse(500, '네트워크 오류가 발생했습니다.', 'NETWORK_ERROR'),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* JSON 응답 파싱 헬퍼
|
|
|
|
|
|
*/
|
|
|
|
|
|
export async function parseJsonResponse<T>(response: Response): Promise<T | null> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
return await response.json();
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
console.error('[parseJsonResponse] JSON 파싱 실패');
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 전체 API 호출 헬퍼 (fetch + JSON 파싱)
|
|
|
|
|
|
*
|
|
|
|
|
|
* @example
|
|
|
|
|
|
* ```typescript
|
|
|
|
|
|
* const { data, error } = await serverApiCall<UserResponse>(url, { method: 'GET' });
|
|
|
|
|
|
* if (error) return error;
|
|
|
|
|
|
* return data;
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*/
|
|
|
|
|
|
export async function serverApiCall<T>(
|
|
|
|
|
|
url: string,
|
|
|
|
|
|
options?: RequestInit
|
|
|
|
|
|
): Promise<{ data: T | null; error: ApiErrorResponse | null }> {
|
|
|
|
|
|
const { response, error } = await serverFetch(url, options);
|
|
|
|
|
|
|
|
|
|
|
|
if (error || !response) {
|
|
|
|
|
|
return { data: null, error };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
const errorData = await parseJsonResponse<{ message?: string; code?: string }>(response);
|
|
|
|
|
|
return {
|
|
|
|
|
|
data: null,
|
|
|
|
|
|
error: createErrorResponse(
|
|
|
|
|
|
response.status,
|
|
|
|
|
|
errorData?.message || '요청 처리 중 오류가 발생했습니다.',
|
|
|
|
|
|
errorData?.code
|
|
|
|
|
|
),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 204 No Content
|
|
|
|
|
|
if (response.status === 204) {
|
|
|
|
|
|
return { data: null, error: null };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const data = await parseJsonResponse<T>(response);
|
|
|
|
|
|
return { data, error: null };
|
2026-01-30 14:16:17 +09:00
|
|
|
|
}
|