From c0fef09f3e92a91e1ed5e043747d82390e2ac9af Mon Sep 17 00:00:00 2001 From: MOON Date: Wed, 7 Aug 2024 13:24:17 +0900 Subject: [PATCH 1/6] =?UTF-8?q?:wrench:=20=EC=9D=B4=EB=A6=84=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/oauth.ts | 11 ----------- src/apis/postKakaoOauth.ts | 11 +++++++++++ src/hooks/useKakaoLogin.ts | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) delete mode 100644 src/apis/oauth.ts create mode 100644 src/apis/postKakaoOauth.ts diff --git a/src/apis/oauth.ts b/src/apis/oauth.ts deleted file mode 100644 index ae6bd068..00000000 --- a/src/apis/oauth.ts +++ /dev/null @@ -1,11 +0,0 @@ -import axios from 'axios'; - -const postOauth = async (code: string) => { - const response = await axios.post(`${process.env.NEXT_PUBLIC_BASE_URL}/auth/signIn/KAKAO`, { - redirectUri: process.env.NEXT_PUBLIC_REDIRECT_URI, - token: code, - }); - return response.data; -}; - -export default postOauth; diff --git a/src/apis/postKakaoOauth.ts b/src/apis/postKakaoOauth.ts new file mode 100644 index 00000000..c85c2eda --- /dev/null +++ b/src/apis/postKakaoOauth.ts @@ -0,0 +1,11 @@ +import httpClient from '.'; + +const postKakaoOauth = async (code: string) => { + const response = await httpClient.post('/auth/signIn/KAKAO', { + redirectUri: process.env.NEXT_PUBLIC_REDIRECT_URI, + token: code, + }); + return response.data; +}; + +export default postKakaoOauth; diff --git a/src/hooks/useKakaoLogin.ts b/src/hooks/useKakaoLogin.ts index ea627868..1652fd29 100644 --- a/src/hooks/useKakaoLogin.ts +++ b/src/hooks/useKakaoLogin.ts @@ -1,4 +1,4 @@ -import postOauth from '@/apis/oauth'; +import postKakaoOauth from '@/apis/postKakaoOauth'; import { toast } from '@/components/ui/use-toast'; import { useMutation } from '@tanstack/react-query'; import { isAxiosError } from 'axios'; @@ -9,7 +9,7 @@ const useKakaoLogin = () => { return useMutation({ mutationFn: async (code: string) => { - const result = await postOauth(code); + const result = await postKakaoOauth(code); localStorage.setItem('accessToken', result.accessToken); localStorage.setItem('refreshToken', result.refreshToken); return result; From 122b327fac3deec941e8a49b5580e8d81d650473 Mon Sep 17 00:00:00 2001 From: MOON Date: Wed, 7 Aug 2024 15:36:03 +0900 Subject: [PATCH 2/6] =?UTF-8?q?:wrench:=20=EB=A6=AC=EB=94=94=EB=A0=89?= =?UTF-8?q?=EC=85=98=20=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95,=20?= =?UTF-8?q?=ED=86=A0=EC=8A=A4=ED=8A=B8=20=EB=A9=94=EC=8B=9C=EC=A7=80=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useRegisterMutation.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/hooks/useRegisterMutation.ts b/src/hooks/useRegisterMutation.ts index a0e8e8cb..67e7661c 100644 --- a/src/hooks/useRegisterMutation.ts +++ b/src/hooks/useRegisterMutation.ts @@ -12,10 +12,9 @@ const useRegisterMutation = (onRegisterError: (field: 'email' | 'nickname') => v onSuccess: (data) => { localStorage.setItem('accessToken', data.accessToken); localStorage.setItem('refreshToken', data.refreshToken); - router.push('/auth/SignIn'); + router.push('/epigrams'); toast({ title: '회원가입 성공!', - description: '로그인 후 이용해주세요.', className: 'bg-illust-green text-white font-semibold', }); }, From 78d7fe020e22c5fbf9cb4810e549fd0e7cd2998e Mon Sep 17 00:00:00 2001 From: MOON Date: Wed, 7 Aug 2024 15:36:38 +0900 Subject: [PATCH 3/6] =?UTF-8?q?:wrench:=20=EB=B9=84=EB=8F=99=EA=B8=B0=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/auth/SignIn.tsx | 17 +++++++++++++---- src/pages/auth/SignUp.tsx | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/pages/auth/SignIn.tsx b/src/pages/auth/SignIn.tsx index 7e4af11c..abb180c7 100644 --- a/src/pages/auth/SignIn.tsx +++ b/src/pages/auth/SignIn.tsx @@ -13,17 +13,26 @@ import useRefreshToken from '@/hooks/useRefreshToken'; export default function SignIn() { const mutationSignin = useSigninMutation(); - const { mutate: refreshAccessToken } = useRefreshToken(); + const { mutateAsync: refreshAccessToken } = useRefreshToken(); const router = useRouter(); useEffect(() => { const refreshToken = typeof window !== 'undefined' ? localStorage.getItem('refreshToken') : null; if (refreshToken) { - refreshAccessToken({ refreshToken }); - router.push('/epigrams'); + const handleTokenRefresh = async () => { + try { + await refreshAccessToken({ refreshToken }); + router.push('/epigrams'); // 토큰 갱신 후 리디렉션 + } catch { + localStorage.removeItem('refreshToken'); + router.push('/auth/SignIn'); + } + }; + + handleTokenRefresh(); } - }, [refreshAccessToken]); + }, [refreshAccessToken, router]); // 폼 정의 const form = useForm({ diff --git a/src/pages/auth/SignUp.tsx b/src/pages/auth/SignUp.tsx index 37a829fa..edad378f 100644 --- a/src/pages/auth/SignUp.tsx +++ b/src/pages/auth/SignUp.tsx @@ -13,17 +13,26 @@ import useRefreshToken from '@/hooks/useRefreshToken'; export default function SignUp() { const [focusedField, setFocusedField] = useState(null); - const { mutate: refreshAccessToken } = useRefreshToken(); + const { mutateAsync: refreshAccessToken } = useRefreshToken(); const router = useRouter(); useEffect(() => { const refreshToken = typeof window !== 'undefined' ? localStorage.getItem('refreshToken') : null; if (refreshToken) { - refreshAccessToken({ refreshToken }); - router.push('/epigrams'); + const handleTokenRefresh = async () => { + try { + await refreshAccessToken({ refreshToken }); + router.push('/epigrams'); // 토큰 갱신 후 리디렉션 + } catch { + localStorage.removeItem('refreshToken'); + router.push('/auth/SignIn'); + } + }; + + handleTokenRefresh(); } - }, [refreshAccessToken]); + }, [refreshAccessToken, router]); const form = useForm({ resolver: zodResolver(PostSignUpRequest), From 0ba6507b0e98e332da2eb9551522c608c272c412 Mon Sep 17 00:00:00 2001 From: MOON Date: Wed, 7 Aug 2024 16:15:37 +0900 Subject: [PATCH 4/6] =?UTF-8?q?:sparkles:=20router.replace()=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useRegisterMutation.ts | 2 +- src/hooks/useSignInMutation.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hooks/useRegisterMutation.ts b/src/hooks/useRegisterMutation.ts index 67e7661c..6890863c 100644 --- a/src/hooks/useRegisterMutation.ts +++ b/src/hooks/useRegisterMutation.ts @@ -12,7 +12,7 @@ const useRegisterMutation = (onRegisterError: (field: 'email' | 'nickname') => v onSuccess: (data) => { localStorage.setItem('accessToken', data.accessToken); localStorage.setItem('refreshToken', data.refreshToken); - router.push('/epigrams'); + router.replace('/epigrams'); toast({ title: '회원가입 성공!', className: 'bg-illust-green text-white font-semibold', diff --git a/src/hooks/useSignInMutation.ts b/src/hooks/useSignInMutation.ts index 416d6876..873a672e 100644 --- a/src/hooks/useSignInMutation.ts +++ b/src/hooks/useSignInMutation.ts @@ -12,7 +12,7 @@ const useSigninMutation = () => { onSuccess: (data) => { localStorage.setItem('accessToken', data.accessToken); localStorage.setItem('refreshToken', data.refreshToken); - router.push('/epigrams'); + router.replace('/epigrams'); }, onError: (error) => { if (!isAxiosError(error)) { From 1215052a7c14415b39d134b5b3219cba3bac5eed Mon Sep 17 00:00:00 2001 From: MOON <50370479+jangmoonwon@users.noreply.github.com> Date: Wed, 7 Aug 2024 16:41:35 +0900 Subject: [PATCH 5/6] Update useRegisterMutation.ts --- src/hooks/useRegisterMutation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/useRegisterMutation.ts b/src/hooks/useRegisterMutation.ts index 6890863c..67e7661c 100644 --- a/src/hooks/useRegisterMutation.ts +++ b/src/hooks/useRegisterMutation.ts @@ -12,7 +12,7 @@ const useRegisterMutation = (onRegisterError: (field: 'email' | 'nickname') => v onSuccess: (data) => { localStorage.setItem('accessToken', data.accessToken); localStorage.setItem('refreshToken', data.refreshToken); - router.replace('/epigrams'); + router.push('/epigrams'); toast({ title: '회원가입 성공!', className: 'bg-illust-green text-white font-semibold', From ab2054896eb9dc1f6e83bb3634117042d0906713 Mon Sep 17 00:00:00 2001 From: MOON <50370479+jangmoonwon@users.noreply.github.com> Date: Wed, 7 Aug 2024 16:41:55 +0900 Subject: [PATCH 6/6] Update useSignInMutation.ts --- src/hooks/useSignInMutation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/useSignInMutation.ts b/src/hooks/useSignInMutation.ts index 873a672e..416d6876 100644 --- a/src/hooks/useSignInMutation.ts +++ b/src/hooks/useSignInMutation.ts @@ -12,7 +12,7 @@ const useSigninMutation = () => { onSuccess: (data) => { localStorage.setItem('accessToken', data.accessToken); localStorage.setItem('refreshToken', data.refreshToken); - router.replace('/epigrams'); + router.push('/epigrams'); }, onError: (error) => { if (!isAxiosError(error)) {