Skip to content

Commit

Permalink
refactor: 리다이렉트 유틸함수 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
chchaeun committed Apr 15, 2024
1 parent b37d15e commit bd25f09
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/components/reset/id.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>('');
//TODO) 인증번호 전송 여부 Toast 추가
Expand Down Expand Up @@ -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 (
Expand Down
6 changes: 4 additions & 2 deletions src/components/reset/pw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>('');
const [passwordConfirm, setPasswordConfirm] = React.useState<string>('');
Expand Down Expand Up @@ -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');
}
Expand Down
10 changes: 6 additions & 4 deletions src/hooks/api/signup/usePostSignup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
},
});
};
9 changes: 9 additions & 0 deletions src/utils/oAuth.ts
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit bd25f09

Please sign in to comment.