-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
126c676
8115c45
6b5851f
87710e3
468c1fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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}`, | ||
); | ||
return response; | ||
}; |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ ๏ธ Refactor suggestion API ์์ฒญ์ ๋ํ ์๋ฌ ์ฒ๋ฆฌ์ ํ์ ์์ ์ฑ ๊ฐ์ ์ด ํ์ํฉ๋๋ค. ๋ค์ ์ฌํญ๋ค์ ๊ฐ์ ํ๋ ๊ฒ์ด ์ข๊ฒ ์ต๋๋ค:
๋ค์๊ณผ ๊ฐ์ด ๊ฐ์ ํด๋ณด์ธ์: +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
Suggested change
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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;
+ }
};
|
This file was deleted.
This file was deleted.
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, | ||
}); | ||
}; |
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('๋ฌธ์ ๋งค์์ง ์ ์ก์ ์คํจํ์ต๋๋ค.'); | ||
}, | ||
}); | ||
}; |
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('ํ์๊ฐ์ ์ ์คํจํ์ต๋๋ค.'); | ||
}, | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URL ํ๋ผ๋ฏธํฐ ์ธ์ฝ๋ฉ ๋ฐ ์๋ฌ ์ฒ๋ฆฌ๊ฐ ํ์ํฉ๋๋ค
๋ณด์ ๋ฐ ์์ ์ฑ ํฅ์์ ์ํ ์์ ์ฌํญ๋ค์ ๋๋ค:
๋ค์๊ณผ ๊ฐ์ด ์์ ํ๋ ๊ฒ์ ์ ์๋๋ฆฝ๋๋ค:
๐ Committable suggestion