-
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
π λ‘κ·ΈμΈ νμ΄μ§ 리ν©ν λ§ μ§ν #63
Changes from all commits
126c676
8115c45
6b5851f
87710e3
468c1fe
66f079d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import axios from 'axios'; | ||
|
||
interface SignInData { | ||
nickname: string; | ||
password: string; | ||
} | ||
|
||
export const postSignin = async (data: SignInData) => { | ||
const response = await axios.post('/api/auth/signin', data); | ||
return response; | ||
}; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { useRouter } from 'next/navigation'; | ||
import { toast } from 'react-toastify'; | ||
import { postSignin } from '../api/postSignin'; | ||
|
||
interface SignInData { | ||
nickname: string; | ||
password: string; | ||
} | ||
|
||
export const useSignin = () => { | ||
const router = useRouter(); | ||
|
||
return useMutation({ | ||
mutationFn: (data: SignInData) => postSignin(data), | ||
onSuccess: () => { | ||
toast.success('λ‘κ·ΈμΈμ΄ μλ£λμμ΅λλ€.'); | ||
router.push('/'); | ||
}, | ||
onError: () => { | ||
toast.error('λ‘κ·ΈμΈμ μ€ν¨νμ΅λλ€.'); | ||
}, | ||
}); | ||
Comment on lines
+14
to
+23
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 mutation μ€μ κ°μ νμ mutation ꡬμ±μ λͺ κ°μ§ μ€μν μ€μ μ΄ λλ½λμμ΅λλ€: λ€μ μ€μ λ€μ μΆκ°νλ κ²μ μΆμ²λ립λλ€: return useMutation({
mutationFn: (data: SignInData) => postSignin(data),
+ retry: 1,
onSuccess: () => {
toast.success('λ‘κ·ΈμΈμ΄ μλ£λμμ΅λλ€.');
+ // λ‘κ·ΈμΈ ν κ΄λ ¨ μΏΌλ¦¬λ€ λ¬΄ν¨ν
+ queryClient.invalidateQueries({ queryKey: ['user'] });
router.push('/');
},
onError: () => {
toast.error('λ‘κ·ΈμΈμ μ€ν¨νμ΅λλ€.');
},
+ onSettled: () => {
+ // λ‘λ© μν μ²λ¦¬
+ }
});
|
||
}; |
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}`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+10
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. URL 맀κ°λ³μ μΈμ½λ© νμ URL 맀κ°λ³μμ νΉμ λ¬Έμκ° ν¬ν¨λ μ μμΌλ―λ‘ - `/api/auth/sms?phoneNumber=${phoneNumber}&code=${code}`,
+ `/api/auth/sms?phoneNumber=${encodeURIComponent(phoneNumber)}&code=${encodeURIComponent(code)}`, π Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return response; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+4
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 μλ¬ μ²λ¦¬ λ° νμ μμ μ± κ°μ νμ ν¨μμ λ°ν νμ μ λͺ μνκ³ μλ¬ μ²λ¦¬λ₯Ό κ°μ νλ κ²μ΄ μ’μ΅λλ€. -export const getCheckSmsCode = async (phoneNumber: string, code: string) => {
+interface SmsVerificationResponse {
+ success: boolean;
+ message: string;
+}
+
+export const getCheckSmsCode = async (
+ phoneNumber: string,
+ code: string
+): Promise<SmsVerificationResponse> => {
if (!phoneNumber || !code) {
toast.error('μ νλ²νΈμ μΈμ¦ λ²νΈλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ.');
- return;
+ throw new Error('μ νλ²νΈμ μΈμ¦ λ²νΈλ νμμ
λλ€.');
}
- const response = await axios.get(
- `/api/auth/sms?phoneNumber=${phoneNumber}&code=${code}`,
- );
- return response;
+ try {
+ const response = await axios.get<SmsVerificationResponse>(
+ `/api/auth/sms?phoneNumber=${encodeURIComponent(phoneNumber)}&code=${encodeURIComponent(code)}`,
+ );
+ return response.data;
+ } catch (error) {
+ toast.error('μΈμ¦ νμΈ μ€ μ€λ₯κ° λ°μνμ΅λλ€.');
+ throw error;
+ } π Committable suggestion
Suggested change
|
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; | ||
} | ||
Comment on lines
+4
to
+8
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 ν¨μμμ UI λ‘μ§ λΆλ¦¬ νμ
μ μ© μμ: export const postSendSms = async (phoneNumber: string) => {
if (!phoneNumber) {
- toast.error('μ νλ²νΈλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ.');
- return;
+ throw new Error('μ νλ²νΈλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ.');
}
const response = await axios.post('/api/auth/sms', { phoneNumber });
return response;
};
|
||
|
||
const response = await axios.post('/api/auth/sms', { phoneNumber }); | ||
return response; | ||
Comment on lines
+10
to
+11
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. API νΈμΆ μ μλ¬ μ²λ¦¬ νμ
μ μ© μμ: export const postSendSms = async (phoneNumber: string) => {
if (!phoneNumber) {
throw new Error('μ νλ²νΈλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ.');
}
+ try {
const response = await axios.post('/api/auth/sms', { phoneNumber });
return response;
+ } catch (error) {
+ throw error;
+ }
};
|
||
}; |
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; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
export const postSignup = async (data: SignUpData) => { | ||||||||||||||||||||||||
const response = await axios.post('/api/auth/signup', data); | ||||||||||||||||||||||||
return response; | ||||||||||||||||||||||||
Comment on lines
+11
to
+13
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. API νΈμΆ μ μλ¬ μ²λ¦¬ νμ
μ μ© μμ: export const postSignup = async (data: SignUpData) => {
+ try {
const response = await axios.post('/api/auth/signup', data);
return response;
+ } catch (error) {
+ throw error;
+ }
}; π Committable suggestion
Suggested change
|
||||||||||||||||||||||||
}; |
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('λ¬Έμ 맀μμ§ μ μ‘μ μ€ν¨νμ΅λλ€.'); | ||||||
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. μ€ν μμ νμ μλ¬ λ©μμ§μ μ€νλ₯Ό μμ ν΄μΌ ν©λλ€. - toast.error('λ¬Έμ 맀μμ§ μ μ‘μ μ€ν¨νμ΅λλ€.');
+ toast.error('λ¬Έμ λ©μμ§ μ μ‘μ μ€ν¨νμ΅λλ€.'); π Committable suggestion
Suggested change
|
||||||
}, | ||||||
}); | ||||||
}; |
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
8
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 νμ΄λ¨Έ λ‘μ§ κ°μ νμ νμ΄λ¨Έ λ‘μ§μ λͺ κ°μ§ κ°μ μ΄ νμν©λλ€: useEffect(() => {
+ let intervalId: NodeJS.Timeout;
if (timer > 0) {
- const intervalId = setInterval(() => {
+ intervalId = setInterval(() => {
setTimer((prev) => prev - 1);
}, 1000);
-
- return () => clearInterval(intervalId);
} else if (timer === 0) {
setIsSmsSent(false);
}
+ return () => {
+ if (intervalId) {
+ clearInterval(intervalId);
+ }
+ };
}, [timer, setTimer, setIsSmsSent]);
π Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; |
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 = { | ||||||||||||||||||
|
@@ -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), | ||||||||||||||||||
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 μλ²λ‘ μ λ¬λλ λ°μ΄ν° νν°λ§ νμ
μ μ© μμ: (data) => signup({
name: data.name,
nickname: data.nickname,
email: data.email,
password: data.password,
phoneNumber: data.phoneNumber,
}) π Committable suggestion
Suggested change
|
||||||||||||||||||
(errors) => { | ||||||||||||||||||
const firstError = Object.values(errors)[0]; | ||||||||||||||||||
if (firstError && firstError.message) { | ||||||||||||||||||
|
@@ -126,10 +129,10 @@ const SignUpForm = () => { | |||||||||||||||||
type="text" | ||||||||||||||||||
placeholder="μΈμ¦ λ²νΈ μ λ ₯" | ||||||||||||||||||
style={{ width: '80%' }} | ||||||||||||||||||
disabled={!isSmsSent} | ||||||||||||||||||
disabled={!isSmsSent && watch('code') !== null} | ||||||||||||||||||
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. μ λ ₯ νλ λΉνμ±ν 쑰건 λ‘μ§ μμ νμ
μ μ© μμ: -disabled={!isSmsSent && watch('code') !== null}
+disabled={!isSmsSent || !watch('code')} π Committable suggestion
Suggested change
|
||||||||||||||||||
/> | ||||||||||||||||||
<Button | ||||||||||||||||||
onClick={() => checkSmsCode(watch('phoneNumber'), watch('code'))} | ||||||||||||||||||
onClick={() => checkSmsCode()} | ||||||||||||||||||
text="νμΈ" | ||||||||||||||||||
width="20%" | ||||||||||||||||||
disabled={!isSmsSent} | ||||||||||||||||||
|
@@ -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} | ||||||||||||||||||
> | ||||||||||||||||||
|
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.
π οΈ Refactor suggestion
API μλ΅ νμ λ° μλ¬ μ²λ¦¬ κ°μ νμ
API νΈμΆ ꡬνμ λ€μ μ¬νλ€μ΄ 보μλμ΄μΌ ν©λλ€:
λ€μκ³Ό κ°μ΄ κ°μ νλ κ²μ μ μλ립λλ€:
π Committable suggestion