feat: 대시보드에 유저의 정보를 불러오기

This commit is contained in:
2026-02-26 20:49:12 +09:00
parent d56303cec4
commit c92e270716
11 changed files with 213 additions and 22 deletions

View File

@@ -6,20 +6,31 @@
* - 빌드 후 운영 환경: https://api.viberoom.io (from .env.production)
*/
import Cookies from "js-cookie";
const API_BASE_URL =
process.env.NEXT_PUBLIC_API_BASE_URL || "http://localhost:8080";
const TOKEN_COOKIE_KEY = "vr_access_token";
export const apiClient = async <T>(
endpoint: string,
options: RequestInit = {},
): Promise<T> => {
// 엔드포인트 앞의 슬래시(/) 중복 방지
const url = `${API_BASE_URL}${endpoint.startsWith("/") ? endpoint : `/${endpoint}`}`;
const defaultHeaders = {
// 쿠키에서 토큰 가져오기
const token = Cookies.get(TOKEN_COOKIE_KEY);
const defaultHeaders: Record<string, string> = {
"Content-Type": "application/json",
};
// 토큰이 있으면 Authorization 헤더 추가
if (token) {
defaultHeaders["Authorization"] = `Bearer ${token}`;
}
const response = await fetch(url, {
...options,
headers: {
@@ -34,5 +45,6 @@ export const apiClient = async <T>(
throw new Error(`API 요청 실패: ${response.status}`);
}
return response.json() as Promise<T>;
const result = await response.json();
return result.data as T;
};