From bd25f09aae6b2c4b0a3633193b759231b3dad367 Mon Sep 17 00:00:00 2001 From: Kim Chae Eun <85024598+chchaeun@users.noreply.github.com> Date: Mon, 15 Apr 2024 16:14:14 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=A6=AC=EB=8B=A4=EC=9D=B4?= =?UTF-8?q?=EB=A0=89=ED=8A=B8=20=EC=9C=A0=ED=8B=B8=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/reset/id.tsx | 10 ++++++---- src/components/reset/pw.tsx | 6 ++++-- src/hooks/api/signup/usePostSignup.ts | 10 ++++++---- src/utils/oAuth.ts | 9 +++++++++ 4 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 src/utils/oAuth.ts diff --git a/src/components/reset/id.tsx b/src/components/reset/id.tsx index 452838a..cd28a97 100644 --- a/src/components/reset/id.tsx +++ b/src/components/reset/id.tsx @@ -6,6 +6,8 @@ import { formatphoneNumber } from '@utils/tell'; import React, { ChangeEvent } from 'react'; import { useNavigate, useSearchParams } from 'react-router-dom'; +import { isOAuthFlow, redirectToClient } from '@/utils/oAuth'; + export default function IdForm() { const [phoneNumber, setPhoneNumber] = React.useState(''); //TODO) 인증번호 전송 여부 Toast 추가 @@ -36,11 +38,11 @@ export default function IdForm() { }; const redirectLogin = () => { - if (searchParams.has('redirectUrl')) { - window.location.href = searchParams.get('redirectUrl') || ''; - return; + if (isOAuthFlow(searchParams)) { + redirectToClient(searchParams); + } else { + navigate('/login'); } - navigate('/login'); }; return ( diff --git a/src/components/reset/pw.tsx b/src/components/reset/pw.tsx index c1199f4..9dcf10a 100644 --- a/src/components/reset/pw.tsx +++ b/src/components/reset/pw.tsx @@ -6,6 +6,8 @@ import { useAlert } from '@hooks/useAlert'; import React, { ChangeEvent } from 'react'; import { useNavigate, useSearchParams } from 'react-router-dom'; +import { isOAuthFlow, redirectToClient } from '@/utils/oAuth'; + export default function ResetPwForm({ token }: { token: string }) { const [password, setPassword] = React.useState(''); const [passwordConfirm, setPasswordConfirm] = React.useState(''); @@ -44,8 +46,8 @@ export default function ResetPwForm({ token }: { token: string }) { if (resetSuccess) { alert('비밀번호가 변경되었습니다.'); - if (searchParams.has('redirectUrl')) { - window.location.href = searchParams.get('redirectUrl') || ''; + if (isOAuthFlow(searchParams)) { + redirectToClient(searchParams); } else { navigate('/login'); } diff --git a/src/hooks/api/signup/usePostSignup.ts b/src/hooks/api/signup/usePostSignup.ts index 633266e..ce93338 100644 --- a/src/hooks/api/signup/usePostSignup.ts +++ b/src/hooks/api/signup/usePostSignup.ts @@ -4,6 +4,8 @@ import { post } from '@libs/api'; import { useMutation } from '@tanstack/react-query'; import { useNavigate, useSearchParams } from 'react-router-dom'; +import { isOAuthFlow, redirectToClient } from '@/utils/oAuth'; + export interface UserRegistrationInfo { nickname: string; password: string; @@ -19,11 +21,11 @@ export const usePostSignup = (signupToken: string) => { password, }), onSuccess: () => { - if (searchParams.has('redirectUrl')) { - window.location.href = searchParams.get('redirectUrl') || ''; - return; + if (isOAuthFlow(searchParams)) { + redirectToClient(searchParams); + } else { + navigate(ROUTES.LOGIN); } - navigate(ROUTES.LOGIN); }, }); }; diff --git a/src/utils/oAuth.ts b/src/utils/oAuth.ts new file mode 100644 index 0000000..9ef6663 --- /dev/null +++ b/src/utils/oAuth.ts @@ -0,0 +1,9 @@ +const redirectToClient = (searchParams: URLSearchParams) => { + window.location.href = searchParams.get('redirectUri') || ''; +}; + +const isOAuthFlow = (searchParams: URLSearchParams) => { + return searchParams.has('redirectUri'); +}; + +export { redirectToClient, isOAuthFlow };