From 0b05c9ce16f1c1d0b7cd9e9200459a7bac10da68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=84=EC=A7=84=ED=98=B8?= Date: Sat, 9 Dec 2023 16:10:37 +0900 Subject: [PATCH 1/5] feat: add ResponseError class --- src/apis/fetch.ts | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/apis/fetch.ts b/src/apis/fetch.ts index 481619b..8c9c01b 100644 --- a/src/apis/fetch.ts +++ b/src/apis/fetch.ts @@ -1,3 +1,14 @@ +import { ErrorResponse } from '@interfaces/api'; + +export class ResponseError extends Error { + errorData: ErrorResponse; + + constructor(errorData: ErrorResponse) { + super(); + this.errorData = errorData; + } +} + class Fetch { private baseURL: string; private accessToken: string | undefined; @@ -18,7 +29,15 @@ class Fetch { return data; } - async post({ path, headers, body }: { path: string; headers?: HeadersInit; body: object }) { + async post({ + path, + headers, + body, + }: { + path: string; + headers?: HeadersInit; + body: object; + }) { const response = await fetch(`${this.baseURL}${path}`, { method: 'POST', headers: { @@ -28,8 +47,14 @@ class Fetch { }, body: JSON.stringify(body), }); - const data: T = await response.json(); - return data; + + const data = await response.json(); + + if (!response.ok) { + throw new ResponseError(data); + } + + return data as TData; } setAccessToken(token: string) { From 3fa748ba1363286dc9284a4f38c3839cb82cf8bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=84=EC=A7=84=ED=98=B8?= Date: Sat, 9 Dec 2023 16:15:15 +0900 Subject: [PATCH 2/5] feat: ErrorResponse interface --- src/interfaces/api/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/interfaces/api/index.ts diff --git a/src/interfaces/api/index.ts b/src/interfaces/api/index.ts new file mode 100644 index 0000000..afc9f68 --- /dev/null +++ b/src/interfaces/api/index.ts @@ -0,0 +1,11 @@ +export interface ErrorResponse { + abCode: string; + errorContent: ErrorContent; +} + +interface ErrorContent { + message: string; + hint: string; + httpCode: number; + payload?: number; +} From 8434fc58bcfa4cce1c96efec336371e455a3c7dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=84=EC=A7=84=ED=98=B8?= Date: Sat, 9 Dec 2023 16:19:53 +0900 Subject: [PATCH 3/5] refactor: remove console --- src/hooks/useFunnel/useFunnel.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/hooks/useFunnel/useFunnel.tsx b/src/hooks/useFunnel/useFunnel.tsx index 7f4825d..20aa526 100644 --- a/src/hooks/useFunnel/useFunnel.tsx +++ b/src/hooks/useFunnel/useFunnel.tsx @@ -20,7 +20,6 @@ const useFunnel = ( const FunnelComponent = Object.assign( (props: Omit, 'steps' | 'step'>) => { - console.log('🚀 ~ file: useFunnel.tsx:21 ~ step:', step); return steps={steps} step={step} {...props} />; }, { Step } From 717c792bef6b2808cf009548a4f6724b16479938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=84=EC=A7=84=ED=98=B8?= Date: Sat, 9 Dec 2023 16:20:20 +0900 Subject: [PATCH 4/5] fix: navigate to signup page if error --- src/routes/Auth/kakao/KakaoLogin.tsx | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/routes/Auth/kakao/KakaoLogin.tsx b/src/routes/Auth/kakao/KakaoLogin.tsx index d74af92..3d5e575 100644 --- a/src/routes/Auth/kakao/KakaoLogin.tsx +++ b/src/routes/Auth/kakao/KakaoLogin.tsx @@ -3,6 +3,8 @@ import { useNavigate } from 'react-router'; import { kakaoLogin } from '@apis/oauth/kakao'; +import { ResponseError } from '@apis/fetch'; + import { Container } from './KakaoLogin.styles'; const KakaoLogin = () => { @@ -12,11 +14,17 @@ const KakaoLogin = () => { const handleKakaoLogin = async () => { if (kakaoCode) { - const response = await kakaoLogin(kakaoCode); - - if (response.accessToken) { - /** TBD */ - response.newMember ? navigate('/onboard') : navigate('/'); + try { + const response = await kakaoLogin(kakaoCode); + if (response && response.accessToken) { + response.newMember ? navigate('/onboard') : navigate('/'); + } + } catch (err) { + if (err instanceof ResponseError) { + if (err.errorData.abCode === 'ILLEGAL_JOIN_STATUS') { + navigate(`/signup`, { state: { memberId: err.errorData.errorContent.payload } }); + } + } } } else { throw new Error('code is invalid'); From 28150df6e28e41b5eabc774fb811a4639398d17d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=84=EC=A7=84=ED=98=B8?= Date: Sat, 9 Dec 2023 16:20:36 +0900 Subject: [PATCH 5/5] feat: get memberId state from location --- src/routes/Auth/signup/Signup.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/routes/Auth/signup/Signup.tsx b/src/routes/Auth/signup/Signup.tsx index 3914fbf..89660df 100644 --- a/src/routes/Auth/signup/Signup.tsx +++ b/src/routes/Auth/signup/Signup.tsx @@ -1,4 +1,5 @@ import React, { useState } from 'react'; +import { useLocation } from 'react-router-dom'; import useFunnel from '@hooks/useFunnel/useFunnel'; @@ -7,6 +8,8 @@ import 약관동의 from './약관동의'; import 정보입력 from './정보입력'; const Signup = () => { + const { state } = useLocation(); + const [registerData, setRegisterData] = useState(); const steps = ['정보입력', '약관동의', '가입성공']; const [Funnel, setStep] = useFunnel(steps);