diff --git a/src/provider/userProvider.tsx b/src/provider/userProvider.tsx index fccf1f1..3fc633a 100644 --- a/src/provider/userProvider.tsx +++ b/src/provider/userProvider.tsx @@ -33,42 +33,23 @@ export function UserProvider({ children }: { children: React.ReactNode }) { const router = useRouter(); const fetchUser = async () => { - if (!isLoggedIn) { - setUser(null); - setIsLoading(false); - return; - } - 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 (axios.isAxiosError(err)) { + if (err instanceof AxiosError) { 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 while fetching user data")); + setError(new Error("An unexpected error occurred")); } } finally { setIsLoading(false); @@ -76,8 +57,14 @@ export function UserProvider({ children }: { children: React.ReactNode }) { }; useEffect(() => { + if (!isLoggedIn) { + setUser(null); + setIsLoading(false); + return; + } + fetchUser(); - }, [isLoggedIn]); + }, [isLoggedIn]); const logout = useCallback(async () => { try {