diff --git a/src/lib/axios.ts b/src/lib/axios.ts index ccd7c11..2dfffb1 100644 --- a/src/lib/axios.ts +++ b/src/lib/axios.ts @@ -2,6 +2,7 @@ import axios from 'axios'; export const api = axios.create({ baseURL: process.env.NEXT_PUBLIC_API_BASE_URL || '', + withCredentials: true, headers: { 'Content-Type': 'application/json', }, diff --git a/src/provider/userProvider.tsx b/src/provider/userProvider.tsx index 1b29eb8..43cf2e6 100644 --- a/src/provider/userProvider.tsx +++ b/src/provider/userProvider.tsx @@ -35,20 +35,34 @@ export function UserProvider({ children }: { children: React.ReactNode }) { const fetchUser = async () => { try { const { data } = await api.get("/artist"); + + if (!data || typeof data !== 'object') { + throw new Error('Invalid response data'); + } + + const requiredFields: (keyof Artist)[] = ['uuid', 'name', 'role', 'email', 'artistImage']; + const missingFields = requiredFields.filter(field => !(field in data)); + + if (missingFields.length > 0) { + throw new Error(`Missing required fields: ${missingFields.join(', ')}`); + } + setUser(data); } catch (err) { - if (err instanceof AxiosError) { + if (axios.isAxiosError(err)) { if (err.response?.status === 401) { setIsLoggedIn(false); setUser(null); + } else { + setError( + new Error( + err.response?.data?.message || + `Failed to fetch user data: ${err.response?.status}` + ) + ); } - setError( - new Error( - err.response?.data?.message || "Failed to fetch user data" - ) - ); } else { - setError(new Error("An unexpected error occurred")); + setError(new Error("An unexpected error occurred while fetching user data")); } } finally { setIsLoading(false); diff --git a/src/services/albumService.ts b/src/services/albumService.ts index f28cb8d..2f2fa9a 100644 --- a/src/services/albumService.ts +++ b/src/services/albumService.ts @@ -1,4 +1,4 @@ -import axios from 'axios'; +import { api } from '@/lib/axios'; export interface Album { uuid: string; @@ -8,11 +8,9 @@ export interface Album { releaseDate: string; } -const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL; - export const getAllAlbums = async (): Promise => { try { - const response = await axios.get(`${BASE_URL}/album`); + const response = await api.get("/album"); return response.data; } catch (error) { console.error('앨범 데이터 조회 실패:', error);