diff --git a/apis/api.ts b/apis/api.ts index ab0b97c0a..c71448a48 100644 --- a/apis/api.ts +++ b/apis/api.ts @@ -15,16 +15,32 @@ export async function getUser() { return result; } +export async function postValidateSignUp(id: string) { + const response = await axios.post(`${BASIC_URL}/check-email`, { + email: id, + }); + console.log(response); + + if (response.status < 200 || response.status >= 300) { + throw new Error('이미 사용 중인 이메일입니다.'); + } + // const result = response.data.isUsableNickname; + + return response; +} + export async function postSignUp(id: string, pw: string) { const response = await axios.post(`${BASIC_URL}/sign-up`, { email: id, password: pw, }); + console.log(response); + if (response.status < 200 || response.status >= 300) { throw new Error('회원가입에 실패했습니다.'); } - const result = response.data.data; - return result; + // const result = response.data.data; + return response; } export async function postSignIn(id: string, pw: string) { diff --git a/hooks/useAsync.ts b/hooks/useAsync.ts index b007fe1ef..8b326bbea 100644 --- a/hooks/useAsync.ts +++ b/hooks/useAsync.ts @@ -1,30 +1,23 @@ -import { useCallback, useState } from 'react'; +import { useState, useCallback } from 'react'; -type AsyncFunction = ( - ...args: TArgs -) => Promise; +type AsyncFunction = (...args: A) => Promise; -interface UseAsyncResult { - pending: boolean; - error: Error | null; - requestFunction: (...args: any[]) => Promise; -} - -export default function useAsync( - asyncFunction: AsyncFunction -): UseAsyncResult { - const [pending, setPending] = useState(false); +const useAsync = (asyncFunction: AsyncFunction) => { + const [pending, setPending] = useState(false); const [error, setError] = useState(null); + const [result, setResult] = useState(null); const requestFunction = useCallback( - async (...args: TArgs): Promise => { + async (...args: A) => { + setPending(true); + setError(null); try { - setError(null); - setPending(true); - return await asyncFunction(...args); - } catch (error) { - setError(error as Error); - return; + const response = await asyncFunction(...args); + setResult(response); + return response; + } catch (err) { + setError(err as Error); + return null; } finally { setPending(false); } @@ -32,5 +25,7 @@ export default function useAsync( [asyncFunction] ); - return { pending, error, requestFunction }; -} + return { pending, error, result, requestFunction }; +}; + +export default useAsync; diff --git a/pages/signup/index.tsx b/pages/signup/index.tsx index 6950da3ed..0ce284448 100644 --- a/pages/signup/index.tsx +++ b/pages/signup/index.tsx @@ -7,13 +7,14 @@ import KakaotalkIcon from '../../src/images/login_kakaotalk.svg'; import EyeOnIcon from '../../src/images/eye_on.svg'; import EyeOffIcon from '../../src/images/eye_off.svg'; import { ChangeEvent, FormEvent, useState } from 'react'; -import { postSignUp } from '@/apis/api'; +import { postSignUp, postValidateSignUp } from '@/apis/api'; import { useRouter } from 'next/router'; import { validateEmail, validateSignUpPassword, validatePasswordConform, } from '@/utils/validate'; +import useAsync from '@/hooks/useAsync'; export default function SignUpPage() { const [email, setEmail] = useState(''); @@ -28,6 +29,16 @@ export default function SignUpPage() { const [isVisiblePasswordConform, setIsVisiblePasswordConform] = useState(false); const router = useRouter(); + const { + pending: validateSignUpPending, + error: validateSignUpError, + requestFunction: validateSignUpRequest, + } = useAsync(postValidateSignUp); + const { + pending: signUpPending, + error: signUpError, + requestFunction: signUpRequest, + } = useAsync(postSignUp); const handleEmailChange = (e: ChangeEvent) => { setEmail(e.target.value); @@ -46,15 +57,44 @@ export default function SignUpPage() { validateEmail(email, setShowError); validateSignUpPassword(password, setShowError); validatePasswordConform(passwordConform, password, setShowError); + if ( - showError.email.error || + (showError.email.error && + showError.email.message !== '이미 사용 중인 이메일입니다.') || showError.password.error || showError.passwordConform.error - ) { + ) + return; + + const validateResult = await validateSignUpRequest(email); + + if (validateSignUpError) { + setShowError((prev) => ({ + ...prev, + email: { + error: true, + message: '이미 사용 중인 이메일입니다.', + }, + })); + return; + } + + if (!validateResult) return; + + const signUpResult = await signUpRequest(email, password); + + if (signUpError) { + setShowError((prev) => ({ + ...prev, + email: { + error: true, + message: '이미 사용 중인 이메일입니다.', + }, + })); return; } - const result = await postSignUp(email, password); - localStorage.setItem('accessToken', result?.accessToken); + + localStorage.setItem('accessToken', signUpResult?.data.data.accessToken); router.push('/folder'); };