Skip to content

Commit

Permalink
제발 유저 정보
Browse files Browse the repository at this point in the history
  • Loading branch information
ipcgrdn committed Nov 20, 2024
1 parent 9fdd2d7 commit cae1e07
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions src/provider/userProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,38 @@ 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>("/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);
}
};

useEffect(() => {
if (!isLoggedIn) {
setUser(null);
setIsLoading(false);
return;
}

fetchUser();
}, [isLoggedIn]);
}, [isLoggedIn]);

const logout = useCallback(async () => {
try {
Expand Down

0 comments on commit cae1e07

Please sign in to comment.