Skip to content

Commit

Permalink
Feat: #118 네트워크 코드 작성 방식 변경을 위한 useAxios 수정 및 로그인 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoonyesol committed Sep 10, 2024
1 parent f8c3b29 commit cc3a84d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/hooks/useAxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ export default function useAxios<T, P extends unknown[]>(fetchCallback: PromiseC
const response = await fetchCallback(...params);
setHeaders(response.headers);
setData(response.data);
return response;
} catch (error: unknown) {
setError(error as Error);
errorHandler(error as Error);
throw error;
} finally {
setLoading(false);
}
Expand Down
26 changes: 11 additions & 15 deletions src/pages/user/SignInPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ import AuthFormLayout from '@layouts/AuthFormLayout';
import { useNavigate } from 'react-router-dom';
import { AxiosError } from 'axios';
import useToast from '@hooks/useToast';
import useAxios from '@hooks/useAxios';
import { login } from '@services/authService';
import { useEffect } from 'react';
import type { UserSignInForm } from '@/types/UserType';
import { useAuthStore } from '@/stores/useAuthStore';
import useAxios from '@/hooks/useAxios';

export default function SignInPage() {
const { onLogin } = useAuthStore();
const { toastError } = useToast();
const navigate = useNavigate();
const { error, fetchData, headers, loading } = useAxios(login);
const { fetchData } = useAxios(login);

const {
register,
Expand All @@ -33,30 +32,27 @@ export default function SignInPage() {
});

const onSubmit = async (formData: UserSignInForm) => {
await fetchData(formData);
};
try {
const response = await fetchData(formData);
if (!response.headers) return;

useEffect(() => {
if (headers) {
const accessToken = headers.authorization;
const accessToken = response.headers.authorization;
if (!accessToken) {
toastError('로그인에 실패했습니다.');
return;
}

onLogin(accessToken.split(' ')[1]);
navigate('/', { replace: true });
return;
}

if (error instanceof AxiosError) {
if (error.response?.status === 401) {
} catch (error) {
const axiosError = error as AxiosError;
if (axiosError.response?.status === 401) {
toastError('아이디와 비밀번호를 한번 더 확인해 주세요.');
return;
}
toastError(`로그인 도중 오류가 발생했습니다: ${error.message}`);
toastError(`로그인 도중 오류가 발생했습니다: ${axiosError.message}`);
}
}, [headers, error]);
};

return (
<>
Expand Down

0 comments on commit cc3a84d

Please sign in to comment.