-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] 회원가입 입력 폼 #9
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
Changes from 7 commits
dfce9a0
70be73c
43a5406
28ac69a
87d2d6e
2f6053c
b851720
b0b6221
ea2f6de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { z } from 'zod' | ||
|
|
||
| import { baseApi } from '@/shared/api/base-api' | ||
|
|
||
| /** | ||
| * Zod schema (응답 검증) | ||
| */ | ||
| export const signupResponseSchema = z.object({ | ||
| status: z.number(), | ||
| code: z.string(), | ||
| message: z.string(), | ||
| data: z.object({ | ||
| userId: z.string(), | ||
| email: z.string(), | ||
| nickname: z.string() | ||
| }) | ||
| }) | ||
|
|
||
| export type SignupResponse = z.infer<typeof signupResponseSchema> | ||
|
|
||
| /** | ||
| * 요청 타입 | ||
| */ | ||
| type SignupRequest = { | ||
| email: string | ||
| nickname: string | ||
| password: string | ||
| } | ||
|
|
||
| /** | ||
| * API 함수 | ||
| */ | ||
| export async function signup(request: SignupRequest) { | ||
| const response = await baseApi.post('/api/auth/signup', request) | ||
|
|
||
|
Member
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. baseApi의 baseURL에 이미 /api가 포함되어 있어서 실제 요청이 /api/api/auth/signup으로 나갑니다! /auth/signup으로 변경하면 될 것 같아요!
Contributor
Author
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. baseURL과 중복되지 않도록 /auth/signup로 수정했습니다!! 감사합니다~ |
||
| // 응답 검증 | ||
| return signupResponseSchema.parse(response.data) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| export type SignupFormData = { | ||
| nickname: string | ||
| email: string | ||
| password: string | ||
| confirmPassword: string | ||
| inviteCode: string | ||
| } | ||
|
|
||
| export type SignupFieldName = keyof SignupFormData | ||
| export type SignupFieldErrors = Partial<Record<SignupFieldName, string>> | ||
|
|
||
| export interface SignupResponseData { | ||
| userId: string | ||
| email: string | ||
| nickname: string | ||
| } | ||
|
|
||
| export interface ApiResponse<T> { | ||
| status: number | ||
| code: string | ||
| message: string | ||
| data: T | null | ||
| } | ||
|
|
||
| export const initialSignupFormData: SignupFormData = { | ||
| nickname: '', | ||
| email: '', | ||
| password: '', | ||
| confirmPassword: '', | ||
| inviteCode: '' | ||
| } | ||
|
|
||
| export const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ | ||
|
|
||
| export function validateSignupField( | ||
| field: SignupFieldName, | ||
| value: string, | ||
| currentFormData: SignupFormData | ||
| ) { | ||
| switch (field) { | ||
| case 'nickname': | ||
| if (!value.trim()) return '닉네임을 입력해주세요.' | ||
| if (value.trim().length < 2) return '닉네임은 2자 이상 입력해주세요.' | ||
| if (value.trim().length > 20) return '닉네임은 20자 이하로 입력해주세요.' | ||
| return '' | ||
|
|
||
| case 'email': | ||
| if (!value.trim()) return '이메일을 입력해주세요.' | ||
| if (!emailRegex.test(value.trim())) return '올바른 이메일 형식을 입력해주세요.' | ||
| return '' | ||
|
|
||
| case 'password': | ||
| if (!value) return '비밀번호를 입력해주세요.' | ||
| if (value.length < 8) return '비밀번호는 8자 이상 입력해주세요.' | ||
| return '' | ||
|
|
||
| case 'confirmPassword': | ||
| if (!value) return '비밀번호 확인을 입력해주세요.' | ||
| if (value !== currentFormData.password) return '비밀번호가 일치하지 않습니다.' | ||
| return '' | ||
|
|
||
| case 'inviteCode': | ||
| return '' | ||
|
|
||
| default: | ||
| return '' | ||
| } | ||
| } | ||
|
|
||
| export function validateSignupForm(currentFormData: SignupFormData, isEmailVerified: boolean) { | ||
| const nextErrors: SignupFieldErrors = {} | ||
|
|
||
| ;(Object.keys(currentFormData) as SignupFieldName[]).forEach((field) => { | ||
| const message = validateSignupField(field, currentFormData[field], currentFormData) | ||
| if (message) { | ||
| nextErrors[field] = message | ||
| } | ||
| }) | ||
|
|
||
| if (!isEmailVerified) { | ||
| nextErrors.email = nextErrors.email || '이메일 인증이 필요합니다.' | ||
| } | ||
|
|
||
| return nextErrors | ||
| } |
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.
현재 signupResponseSchema가 백엔드 공통 응답 포맷과 맞지 않아 정상 응답이 반환되더라도 Zod 검증 단계에서 실패하면서 화면에서는 에러로 표시됩니다!
이미 공통 응답을 처리하는 apiResponseSchema 헬퍼가 있으니 개별 API에서 응답 구조를 다시 정의하기보다 해당 헬퍼를 재사용하는 방식으로 통일하면 좋을 것 같습니다!
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.
apiResponse를 사용하도록 수정했습니다!