-
Notifications
You must be signed in to change notification settings - Fork 44
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
[권주현] Week19 #493
The head ref may contain hidden characters: "part3-\uAD8C\uC8FC\uD604-tanstack"
[권주현] Week19 #493
Changes from 1 commit
c104336
6d4f19c
fd621f1
e46f767
2ee2123
5a34214
45fcff1
bda92f4
1112228
79b0587
5cc47fb
27be6a7
5623f94
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { CODEIT_BASE_URL } from "@/constants"; | ||
import { FormValues } from "./common/Auth/Form"; | ||
import { | ||
FolderData, | ||
LinkData, | ||
Comment on lines
3
to
4
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. 타입 또는 인터페이스를 선언할 때, 해당 타입들이 특정 데이터의 타입임을 표현하고 있는데요. |
||
|
@@ -9,6 +8,7 @@ import { | |
Response, | ||
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. NextJS에서 이미 서버 리스폰스 객체를 위한 Response라는 예약어가 사용되고 있을텐데요, 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. 예약어는 NextResponse로 설정되어 있어 충돌은 없지만 Response라는 단어가 너무 포괄적인 단어라 수정해야 할 듯 합니다. |
||
UserData, | ||
} from "./types/api"; | ||
import { FormValues } from "./types/form"; | ||
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. 앞으로 form value들이 굉장히 다양하게 존재하게 될 텐데요. |
||
|
||
interface TokenProp { | ||
token: string; | ||
|
@@ -134,52 +134,52 @@ export async function getLinksByFolderId({ | |
|
||
// POST | ||
|
||
export async function postEmailCheck(email: string): Promise<void | string> { | ||
const response = await fetch(`${CODEIT_BASE_URL}/check-email`, { | ||
export async function postEmailCheck(email: string): Promise<boolean | string> { | ||
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. 크게 프로젝트 아키텍쳐를 레이어로 나누어보면, 네트워크 요청을 전담하는 controller layer와 비즈니스 로직을 담당하는 service layer, 그리고 결과물을 사용자에게 보여주고 ui interaction을 처리하는 ui layer로 구분해볼 수 있어요. 여기서 지금 작성된 코드를 살펴보면, ui 레이어에서 바로 필요한 데이터를 받아와 활용하기 어려워보여요. |
||
const response = await fetch(`${CODEIT_BASE_URL}/users/check-email`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ email }), | ||
}); | ||
const result = await response.json(); | ||
|
||
if (response.status === 409) { | ||
const data = await response.json(); | ||
return data.error.message; | ||
return result.message; | ||
} | ||
|
||
if (!response.ok) { | ||
throw new Error("잘못된 요청입니다."); | ||
} | ||
|
||
return; | ||
return result; | ||
} | ||
|
||
interface postData { | ||
data: { | ||
accessToken: string; | ||
refreshToken: string; | ||
}; | ||
data: | ||
| { | ||
accessToken: string; | ||
refreshToken: string; | ||
} | ||
| { message: string }; | ||
} | ||
Comment on lines
158
to
165
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. @kuum97 또한, 서버에서 반환되는 응답이 data type이거나 오류인경우 message: string으로 반환된다면, 이를 공통적으로 활용할 수 있는 |
||
|
||
export async function postSignup({ | ||
email, | ||
password, | ||
}: FormValues): Promise<postData> { | ||
const response = await fetch(`${CODEIT_BASE_URL}/sign-up`, { | ||
}: FormValues): Promise<postData | void> { | ||
const response = await fetch(`${CODEIT_BASE_URL}/auth/sign-up`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ email, password }), | ||
}); | ||
const data = await response.json(); | ||
const result = await response.json(); | ||
|
||
if (!response.ok) { | ||
return data.error.message; | ||
return result.message; | ||
} | ||
|
||
return data; | ||
} | ||
|
||
export async function postSignin({ | ||
|
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. 기본 html 태그 타입을 상속해주었습니다. |
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. useSignup과 마찬가지로 쿼리 적용과 커스텀훅으로 묶어 구현해주었는데 인증 후 리다이렉트는 미들웨어를 구현해야 하는데 아직 미구현 상태라서 이 부분은 미완성 상태입니다. |
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. 이메일 중복체크와 회원가입 요청 및 라우팅을 커스텀훅으로 묶어주었습니다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { postEmailCheck, postSignup } from "@/api"; | ||
import { AuthHookProp, FormValues } from "@/types/form"; | ||
import { useRouter } from "next/router"; | ||
import { SubmitHandler } from "react-hook-form"; | ||
|
||
function useSignup({ setError }: AuthHookProp) { | ||
const router = useRouter(); | ||
|
||
const handleEmailCheck = async (e: React.FocusEvent<HTMLInputElement>) => { | ||
const email = e.target.value; | ||
try { | ||
if (email.length === 0) return; | ||
|
||
const result = await postEmailCheck(email); | ||
|
||
if (typeof result === "string") { | ||
setError( | ||
"email", | ||
{ type: "value", message: result }, | ||
{ shouldFocus: true } | ||
); | ||
} | ||
|
||
return; | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
const handleSignup: SubmitHandler<FormValues> = async ({ | ||
email, | ||
password, | ||
}) => { | ||
try { | ||
await postSignup({ email, password }); | ||
|
||
router.replace("/signin"); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
return { handleEmailCheck, handleSignup }; | ||
} | ||
|
||
export default useSignup; |
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. 유저 인증 폼의 타입을 따로 파일을 만들어 분리해주었습니다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { TProps } from "@/common/Auth/Header"; | ||
import { ReactNode } from "react"; | ||
import { | ||
SubmitHandler, | ||
UseFormHandleSubmit, | ||
UseFormSetError, | ||
} from "react-hook-form"; | ||
|
||
export interface AuthHookProp { | ||
setError: UseFormSetError<FormValues>; | ||
} | ||
|
||
export interface FormValues { | ||
email: string; | ||
password: string; | ||
passwordConfirm?: string; | ||
} | ||
|
||
export interface AuthFormProps { | ||
children?: ReactNode; | ||
onSubmit: SubmitHandler<FormValues>; | ||
handleSubmit: UseFormHandleSubmit<FormValues, undefined>; | ||
purpose: TProps; | ||
} |
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.
api 요청 경로 변경 코드입니다.