From cae1e075fbf436edf9c645bc3dfb2ac5c59daee5 Mon Sep 17 00:00:00 2001 From: choi Date: Wed, 20 Nov 2024 15:54:21 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A0=9C=EB=B0=9C=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/provider/userProvider.tsx | 43 ++++++++++++----------------------- 1 file changed, 15 insertions(+), 28 deletions(-) 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 {