Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ”€ ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€ ๋ฆฌํŒฉํ† ๋ง ์ง„ํ–‰ #62

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions src/entities/signUp/api/checkSmsCode.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/entities/signUp/api/getCheckSmsCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import axios from 'axios';
import { toast } from 'react-toastify';

export const getCheckSmsCode = async (phoneNumber: string, code: string) => {
if (!phoneNumber || !code) {
toast.error('์ „ํ™”๋ฒˆํ˜ธ์™€ ์ธ์ฆ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
return;
}

const response = await axios.get(
`/api/auth/sms?phoneNumber=${phoneNumber}&code=${code}`,
);
Comment on lines +10 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

โš ๏ธ Potential issue

URL ํŒŒ๋ผ๋ฏธํ„ฐ ์ธ์ฝ”๋”ฉ ๋ฐ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค

๋ณด์•ˆ ๋ฐ ์•ˆ์ •์„ฑ ํ–ฅ์ƒ์„ ์œ„ํ•œ ์ˆ˜์ •์‚ฌํ•ญ๋“ค์ž…๋‹ˆ๋‹ค:

  1. URL ํŒŒ๋ผ๋ฏธํ„ฐ์— encodeURIComponent๋ฅผ ์ ์šฉํ•˜์—ฌ XSS ๊ณต๊ฒฉ์„ ๋ฐฉ์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
  2. axios ์š”์ฒญ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์„ ์ œ์•ˆ๋“œ๋ฆฝ๋‹ˆ๋‹ค:

-  const response = await axios.get(
-    `/api/auth/sms?phoneNumber=${phoneNumber}&code=${code}`,
-  );
+  try {
+    const response = await axios.get(
+      `/api/auth/sms?phoneNumber=${encodeURIComponent(phoneNumber)}&code=${encodeURIComponent(code)}`,
+    );
+    return response;
+  } catch (error) {
+    toast.error('์ธ์ฆ๋ฒˆํ˜ธ ํ™•์ธ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
+    throw error;
+  }
-  return response;
๐Ÿ“ Committable suggestion

โ€ผ๏ธ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const response = await axios.get(
`/api/auth/sms?phoneNumber=${phoneNumber}&code=${code}`,
);
try {
const response = await axios.get(
`/api/auth/sms?phoneNumber=${encodeURIComponent(phoneNumber)}&code=${encodeURIComponent(code)}`,
);
return response;
} catch (error) {
toast.error('์ธ์ฆ๋ฒˆํ˜ธ ํ™•์ธ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
throw error;
}

return response;
};
12 changes: 12 additions & 0 deletions src/entities/signUp/api/postSendSms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import axios from 'axios';
import { toast } from 'react-toastify';

export const postSendSms = async (phoneNumber: string) => {
if (!phoneNumber) {
toast.error('์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
return;
}

const response = await axios.post('/api/auth/sms', { phoneNumber });
return response;
};
Comment on lines +4 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ› ๏ธ Refactor suggestion

API ์š”์ฒญ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ์™€ ํƒ€์ž… ์•ˆ์ „์„ฑ ๊ฐœ์„ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์‚ฌํ•ญ๋“ค์„ ๊ฐœ์„ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค:

  1. axios ์š”์ฒญ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๊ฐ€ ๋ˆ„๋ฝ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค
  2. ์‘๋‹ต ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์ด ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค
  3. ์ „ํ™”๋ฒˆํ˜ธ ํ˜•์‹์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐœ์„ ํ•ด๋ณด์„ธ์š”:

+interface SendSmsResponse {
+  success: boolean;
+  message: string;
+}
+
 export const postSendSms = async (phoneNumber: string) => {
   if (!phoneNumber) {
     toast.error('์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
     return;
   }
+  
+  if (!/^01[0-9]{8,9}$/.test(phoneNumber)) {
+    toast.error('์˜ฌ๋ฐ”๋ฅธ ์ „ํ™”๋ฒˆํ˜ธ ํ˜•์‹์ด ์•„๋‹™๋‹ˆ๋‹ค.');
+    return;
+  }
 
-  const response = await axios.post('/api/auth/sms', { phoneNumber });
-  return response;
+  try {
+    const { data } = await axios.post<SendSmsResponse>('/api/auth/sms', { phoneNumber });
+    return data;
+  } catch (error) {
+    if (axios.isAxiosError(error)) {
+      toast.error(error.response?.data?.message || 'SMS ์ „์†ก์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
+    }
+    throw error;
+  }
 };
๐Ÿ“ Committable suggestion

โ€ผ๏ธ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const postSendSms = async (phoneNumber: string) => {
if (!phoneNumber) {
toast.error('์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
return;
}
const response = await axios.post('/api/auth/sms', { phoneNumber });
return response;
};
interface SendSmsResponse {
success: boolean;
message: string;
}
export const postSendSms = async (phoneNumber: string) => {
if (!phoneNumber) {
toast.error('์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
return;
}
if (!/^01[0-9]{8,9}$/.test(phoneNumber)) {
toast.error('์˜ฌ๋ฐ”๋ฅธ ์ „ํ™”๋ฒˆํ˜ธ ํ˜•์‹์ด ์•„๋‹™๋‹ˆ๋‹ค.');
return;
}
try {
const { data } = await axios.post<SendSmsResponse>('/api/auth/sms', { phoneNumber });
return data;
} catch (error) {
if (axios.isAxiosError(error)) {
toast.error(error.response?.data?.message || 'SMS ์ „์†ก์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
}
throw error;
}
};

14 changes: 14 additions & 0 deletions src/entities/signUp/api/postSignup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import axios from 'axios';

interface SignUpData {
name: string;
nickname: string;
email: string;
password: string;
phoneNumber: string;
}
Comment on lines +3 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ› ๏ธ Refactor suggestion

ํšŒ์›๊ฐ€์ž… ๋ฐ์ดํ„ฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

SignUpData ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๋กœ์ง์ด ๋ˆ„๋ฝ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด Zod๋ฅผ ์‚ฌ์šฉํ•œ ์Šคํ‚ค๋งˆ ๊ฒ€์ฆ์„ ์ถ”๊ฐ€ํ•ด๋ณด์„ธ์š”:

import { z } from 'zod';

export const SignUpSchema = z.object({
  name: z.string().min(2, '์ด๋ฆ„์€ 2๊ธ€์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค'),
  nickname: z.string().min(2, '๋‹‰๋„ค์ž„์€ 2๊ธ€์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค'),
  email: z.string().email('์˜ฌ๋ฐ”๋ฅธ ์ด๋ฉ”์ผ ํ˜•์‹์ด ์•„๋‹™๋‹ˆ๋‹ค'),
  password: z.string().min(8, '๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” 8์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค'),
  phoneNumber: z.string().regex(/^01[0-9]{8,9}$/, '์˜ฌ๋ฐ”๋ฅธ ์ „ํ™”๋ฒˆํ˜ธ ํ˜•์‹์ด ์•„๋‹™๋‹ˆ๋‹ค')
});

export type SignUpData = z.infer<typeof SignUpSchema>;


export const postSignup = async (data: SignUpData) => {
const response = await axios.post('/api/auth/signup', data);
return response;
};
Comment on lines +11 to +14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ› ๏ธ Refactor suggestion

์—๋Ÿฌ ์ฒ˜๋ฆฌ์™€ ์‘๋‹ต ํƒ€์ž… ์ •์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

API ์‘๋‹ต์— ๋Œ€ํ•œ ํƒ€์ž… ์ •์˜์™€ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๊ฐ€ ๋ˆ„๋ฝ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐœ์„ ํ•ด๋ณด์„ธ์š”:

+interface SignUpResponse {
+  success: boolean;
+  message: string;
+  userId?: string;
+}
+
 export const postSignup = async (data: SignUpData) => {
+  try {
+    await SignUpSchema.parseAsync(data);
+    const { data: response } = await axios.post<SignUpResponse>('/api/auth/signup', data);
+    return response;
+  } catch (error) {
+    if (error instanceof z.ZodError) {
+      toast.error(error.errors[0].message);
+    } else if (axios.isAxiosError(error)) {
+      toast.error(error.response?.data?.message || 'ํšŒ์›๊ฐ€์ž…์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
+    }
+    throw error;
+  }
 };

Committable suggestion skipped: line range outside the PR's diff.

25 changes: 0 additions & 25 deletions src/entities/signUp/api/sendSms.ts

This file was deleted.

33 changes: 0 additions & 33 deletions src/entities/signUp/api/signup.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/entities/signUp/model/useCheckSmsCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useQuery } from '@tanstack/react-query';
import { getCheckSmsCode } from '../api/getCheckSmsCode';

export const useCheckSmsCode = (phoneNumber: string, code: string) => {
return useQuery({
queryKey: ['CheckSmsCode', phoneNumber, code],
queryFn: () => getCheckSmsCode(phoneNumber, code),
enabled: false,
});
};
20 changes: 20 additions & 0 deletions src/entities/signUp/model/useSendSms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useMutation } from '@tanstack/react-query';
import { toast } from 'react-toastify';
import { postSendSms } from '../api/postSendSms';

export const useSendSms = (
setIsSmsSent: React.Dispatch<React.SetStateAction<boolean>>,
setTimer: React.Dispatch<React.SetStateAction<number>>,
) => {
return useMutation({
mutationFn: (phoneNumber: string) => postSendSms(phoneNumber),
onSuccess: () => {
setIsSmsSent(true);
setTimer(180);
toast.success('๋ฌธ์ž ๋ฉ”์‹œ์ง€ ์ „์†ก์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
},
onError: () => {
toast.error('๋ฌธ์ œ ๋งค์‹œ์ง€ ์ „์†ก์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
},
});
};
27 changes: 27 additions & 0 deletions src/entities/signUp/model/useSignup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useMutation } from '@tanstack/react-query';
import { useRouter } from 'next/navigation';
import { toast } from 'react-toastify';
import { postSignup } from '../api/postSignup';

interface SignUpData {
name: string;
nickname: string;
email: string;
password: string;
phoneNumber: string;
}

export const useSignup = () => {
const router = useRouter();

return useMutation({
mutationFn: (data: SignUpData) => postSignup(data),
onSuccess: () => {
toast.success('ํšŒ์›๊ฐ€์ž…์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
router.push('/signIn');
},
onError: () => {
toast.error('ํšŒ์›๊ฐ€์ž…์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
},
});
};
6 changes: 5 additions & 1 deletion src/entities/signUp/model/useTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { useEffect } from 'react';
export const useTimer = (
timer: number,
setTimer: React.Dispatch<React.SetStateAction<number>>,
setIsSmsSent: React.Dispatch<React.SetStateAction<boolean>>, // ์ถ”๊ฐ€
) => {
useEffect(() => {
if (timer > 0) {
const intervalId = setInterval(() => {
setTimer((prev) => prev - 1);
}, 1000);

return () => clearInterval(intervalId);
} else if (timer === 0) {
setIsSmsSent(false);
}
}, [timer, setTimer]);
}, [timer, setTimer, setIsSmsSent]);
Comment on lines +15 to +18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ› ๏ธ Refactor suggestion

ํƒ€์ด๋จธ ๋กœ์ง ๊ฐœ์„ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค

ํƒ€์ด๋จธ ๊ด€๋ จ ์ƒ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ  cleanup ๋กœ์ง์„ ๋ณด์™„ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์„ ์ œ์•ˆ๋“œ๋ฆฝ๋‹ˆ๋‹ค:

+ const TIMER_ENDED = 0;
+ 
  useEffect(() => {
    if (timer > 0) {
      const intervalId = setInterval(() => {
        setTimer((prev) => prev - 1);
      }, 1000);

      return () => clearInterval(intervalId);
-    } else if (timer === 0) {
+    } else if (timer === TIMER_ENDED) {
      setIsSmsSent(false);
    }
+    
+    return () => {
+      setIsSmsSent(false);
+    };
  }, [timer, setTimer, setIsSmsSent]);
๐Ÿ“ Committable suggestion

โ€ผ๏ธ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} else if (timer === 0) {
setIsSmsSent(false);
}
}, [timer, setTimer]);
}, [timer, setTimer, setIsSmsSent]);
const TIMER_ENDED = 0;
useEffect(() => {
if (timer > 0) {
const intervalId = setInterval(() => {
setTimer((prev) => prev - 1);
}, 1000);
return () => clearInterval(intervalId);
} else if (timer === TIMER_ENDED) {
setIsSmsSent(false);
}
return () => {
setIsSmsSent(false);
};
}, [timer, setTimer, setIsSmsSent]);

};
27 changes: 14 additions & 13 deletions src/entities/signUp/ui/SignUpForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use client';

import { useRouter } from 'next/navigation';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { Button, Input } from '@/shared/ui';
import { checkSmsCode } from '../../api/checkSmsCode';
import { sendSms } from '../../api/sendSms';
import { signUp } from '../../api/signup';
import { showError } from '../../model/showError';
import { useCheckSmsCode } from '../../model/useCheckSmsCode';
import { useSendSms } from '../../model/useSendSms';
import { useSignup } from '../../model/useSignup';
import { useTimer } from '../../model/useTimer';

type FormData = {
Expand All @@ -29,15 +28,19 @@ const SignUpForm = () => {
} = useForm<FormData>();
const [isSmsSent, setIsSmsSent] = useState(false);
const [timer, setTimer] = useState(0);
const { mutate: signup } = useSignup();
const { mutate: sendSms } = useSendSms(setIsSmsSent, setTimer);
const { refetch: checkSmsCode } = useCheckSmsCode(
watch('phoneNumber'),
watch('code'),
);

useTimer(timer, setTimer);

const router = useRouter();
useTimer(timer, setTimer, setIsSmsSent);

return (
<form
onSubmit={handleSubmit(
(data) => signUp(data, router),
(data) => signup(data),
(errors) => {
const firstError = Object.values(errors)[0];
if (firstError && firstError.message) {
Expand Down Expand Up @@ -126,10 +129,10 @@ const SignUpForm = () => {
type="text"
placeholder="์ธ์ฆ ๋ฒˆํ˜ธ ์ž…๋ ฅ"
style={{ width: '80%' }}
disabled={!isSmsSent}
disabled={!isSmsSent && watch('code') !== null}
/>
<Button
onClick={() => checkSmsCode(watch('phoneNumber'), watch('code'))}
onClick={() => checkSmsCode()}
text="ํ™•์ธ"
width="20%"
disabled={!isSmsSent}
Expand All @@ -138,9 +141,7 @@ const SignUpForm = () => {
</div>
<button
type="button"
onClick={() =>
sendSms(watch('phoneNumber'), setIsSmsSent, setTimer)
}
onClick={() => sendSms(watch('phoneNumber'))}
className="text-caption2 text-gray-300"
disabled={isSmsSent}
>
Expand Down
Loading