Skip to content

Commit

Permalink
πŸ› fix: νšŒμ›κ°€μž… μ„±κ³΅ν•˜μ˜€μ„ λ•Œλ„ onError 처리 문제 ν•΄κ²° #73
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeona01 committed Oct 31, 2024
2 parents 6e0e248 + 4757d15 commit 2681138
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 53 deletions.
24 changes: 4 additions & 20 deletions src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const signIn = async (
};

// 1.2 μ‚¬μš©μž λ‘œκ·Έμ•„μ›ƒ
export const logout = async () => {
export const logout = async (): Promise<RESTYPE<null>> => {
const response = await api.post('/auth/logout');
return response.data;
};
Expand Down Expand Up @@ -98,23 +98,6 @@ export const signUp = async (
return response.data;
};

// 2.6 κΈ°λ³Έ 고용주 νšŒμ›κ°€μž…
/*
export const ownerSignUp = async (
signupInfo: OwnerInfoRequest,
logoImage?: File,
): Promise<RESTYPE<SignUpResponse>> => {
const formData = new FormData();
// νšŒμ‚¬ 둜고
if (logoImage) {
formData.append('logo_image', logoImage);
}
formData.append('signup_info', JSON.stringify(signupInfo));
const response = await apiWithoutAuth.post('/auth/owners', formData);
return response.data;
};
*/

// 2.7 이메일 μΈμ¦μ½”λ“œ 검증
export const patchAuthentication = async (
info: AuthenticationRequest,
Expand All @@ -138,14 +121,15 @@ export const reIssueAuthentication = async (
};

// 2.9 νƒˆν‡΄ν•˜κΈ°
export const withdraw = async () => {
export const withdraw = async (): Promise<RESTYPE<null>> => {
const response = await api.delete('/auth');
return response.data;
};

// 고용주 νšŒμ›κ°€μž…
export const signUpEmployer = async (
signupInfo: EmployerRegistrationRequest,
): Promise<{ success: boolean }> => {
): Promise<RESTYPE<SignUpResponse>> => {
const response = await api.post(`/auth/owners`, signupInfo);
return response.data;
};
93 changes: 60 additions & 33 deletions src/hooks/api/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import {
AuthenticationResponse,
SignInResponse,
SignUpResponse,
TempSignUpResponse,
} from '@/types/api/auth';
import {
Expand Down Expand Up @@ -61,10 +62,12 @@ export const useSignIn = () => {
return useMutation({
mutationFn: signIn,
onSuccess: (data: RESTYPE<SignInResponse>) => {
setAccessToken(data.data.access_token);
setRefreshToken(data.data.refresh_token);
navigate('/splash');
window.location.reload();
if (data.success) {
setAccessToken(data.data.access_token);
setRefreshToken(data.data.refresh_token);
navigate('/splash');
window.location.reload();
}
},
onError: () => {
alert('아이디 ν˜Ήμ€ λΉ„λ°€λ²ˆν˜Έλ₯Ό λ‹€μ‹œ ν™•μΈν•΄μ£Όμ„Έμš”.');
Expand All @@ -78,15 +81,17 @@ export const useLogout = () => {
const { updateAccountType, updateName } = useUserStore();
return useMutation({
mutationFn: logout,
onSuccess: () => {
// 토큰 μ‚­μ œ
deleteAccessToken();
deleteRefreshToken();
// μœ μ € νƒ€μž… μ „μ—­ λ³€μˆ˜ μ΄ˆκΈ°ν™”
updateAccountType(undefined);
updateName('');
// μŠ€ν”Œλž˜μ‹œ 이동
navigate('/splash');
onSuccess: (data: RESTYPE<null>) => {
if (data.success) {
// 토큰 μ‚­μ œ
deleteAccessToken();
deleteRefreshToken();
// μœ μ € νƒ€μž… μ „μ—­ λ³€μˆ˜ μ΄ˆκΈ°ν™”
updateAccountType(undefined);
updateName('');
// μŠ€ν”Œλž˜μ‹œ 이동
navigate('/splash');
}
},
onError: () => {
alert('λ‘œκ·Έμ•„μ›ƒμ„ λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”.');
Expand All @@ -101,9 +106,11 @@ export const useReIssueToken = () => {
return useMutation({
mutationFn: reIssueToken,
onSuccess: (data: RESTYPE<SignInResponse>) => {
setAccessToken(data.data.access_token);
setRefreshToken(data.data.refresh_token);
navigate('/splash'); // μž¬λ°œκΈ‰ ν›„ μœ ν˜• 확인
if (data.success) {
setAccessToken(data.data.access_token);
setRefreshToken(data.data.refresh_token);
navigate('/splash'); // μž¬λ°œκΈ‰ ν›„ μœ ν˜• 확인
}
},
onError: () => {
alert('λ§Œλ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€. λ‹€μ‹œ λ‘œκ·ΈμΈν•΄μ£Όμ„Έμš”.');
Expand Down Expand Up @@ -138,8 +145,14 @@ export const useGetUserType = () => {

// 2.4 κΈ°λ³Έ μž„μ‹œνšŒμ›κ°€μž… ν›…
export const useTempSignUp = () => {
const { updateTryCnt } = useEmailTryCountStore();
return useMutation({
mutationFn: tempSignUp,
onSuccess: (data: RESTYPE<TempSignUpResponse>) => {
if (data.success) {
updateTryCnt(data.data.try_cnt);
}
},
onError: (error) => {
console.log('μž„μ‹œ νšŒμ›κ°€μž… μ‹€νŒ¨ : ', error.message);
},
Expand All @@ -152,9 +165,11 @@ export const useSignUp = () => {
return useMutation({
mutationFn: signUp,
onSuccess: (data: RESTYPE<SignInResponse>) => {
deleteTemporaryToken();
setAccessToken(data.data.access_token);
setRefreshToken(data.data.refresh_token);
if (data.success) {
deleteTemporaryToken();
setAccessToken(data.data.access_token);
setRefreshToken(data.data.refresh_token);
}
},
onError: () => {
alert('νšŒμ›κ°€μž…μ— μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€. λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”.');
Expand All @@ -168,7 +183,9 @@ export const usePatchAuthentication = () => {
return useMutation({
mutationFn: patchAuthentication,
onSuccess: (data: RESTYPE<AuthenticationResponse>) => {
setTemporaryToken(data.data.temporary_token);
if (data.success) {
setTemporaryToken(data.data.temporary_token);
}
},
onError: (error) => {
alert('μΈμ¦μ½”λ“œλ₯Ό λ‹€μ‹œ ν™•μΈν•΄μ£Όμ„Έμš”.');
Expand All @@ -184,8 +201,10 @@ export const useReIssueAuthentication = () => {
return useMutation({
mutationFn: reIssueAuthentication,
onSuccess: (data: RESTYPE<TempSignUpResponse>) => {
// 이메일 μž¬λ°œμ†‘ 횟수 μ—…λ°μ΄νŠΈ
updateTryCnt(data.data.try_cnt);
if (data.success) {
// 이메일 μž¬λ°œμ†‘ 횟수 μ—…λ°μ΄νŠΈ
updateTryCnt(data.data.try_cnt);
}
},
onError: () => {
alert('μΈμ¦μ½”λ“œ μž¬λ°œμ†‘μ΄ μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€. νšŒμ›κ°€μž…μ„ λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”.');
Expand All @@ -200,15 +219,17 @@ export const useWithdraw = () => {
const { updateAccountType, updateName } = useUserStore();
return useMutation({
mutationFn: withdraw,
onSuccess: () => {
// 토큰 μ‚­μ œ
deleteAccessToken();
deleteRefreshToken();
// μœ μ € νƒ€μž… μ „μ—­ λ³€μˆ˜ μ΄ˆκΈ°ν™”
updateAccountType(undefined);
updateName('');
// μŠ€ν”Œλž˜μ‹œ 이동
navigate('/splash');
onSuccess: (data: RESTYPE<null>) => {
if (data.success) {
// 토큰 μ‚­μ œ
deleteAccessToken();
deleteRefreshToken();
// μœ μ € νƒ€μž… μ „μ—­ λ³€μˆ˜ μ΄ˆκΈ°ν™”
updateAccountType(undefined);
updateName('');
// μŠ€ν”Œλž˜μ‹œ 이동
navigate('/splash');
}
},
onError: () => {
alert('νƒˆν‡΄μ— μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€.');
Expand All @@ -221,10 +242,16 @@ export const useSignupEmployer = (setSuccess: () => void) => {
const navigate = useNavigate();
return useMutation({
mutationFn: signUpEmployer,
onSuccess: () => {
setSuccess();
onSuccess: (data: RESTYPE<SignUpResponse>) => {
if (data.success) {
deleteTemporaryToken();
setAccessToken(data.data.access_token);
setRefreshToken(data.data.refresh_token);
setSuccess();
}
},
onError: () => {
alert('νšŒμ›κ°€μž…μ— μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€. λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”.');
navigate('/signin');
},
});
Expand Down

0 comments on commit 2681138

Please sign in to comment.