Skip to content
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@radix-ui/react-dialog": "^1.1.15",
"@tailwindcss/vite": "^4.2.2",
"@tanstack/react-query": "^5.95.2",
"axios": "^1.13.6",
"axios": "1.13.6",
"lucide-react": "^1.6.0",
"react": "^19.2.4",
"react-dom": "^19.2.4",
Expand Down
38 changes: 38 additions & 0 deletions src/features/auth-dialog/api/signup.ts
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()
})
})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

현재 signupResponseSchema가 백엔드 공통 응답 포맷과 맞지 않아 정상 응답이 반환되더라도 Zod 검증 단계에서 실패하면서 화면에서는 에러로 표시됩니다!

Image

이미 공통 응답을 처리하는 apiResponseSchema 헬퍼가 있으니 개별 API에서 응답 구조를 다시 정의하기보다 해당 헬퍼를 재사용하는 방식으로 통일하면 좋을 것 같습니다!

export const signupResponseSchema = apiResponseSchema(
  z.object({
    userId: z.string(),
    email: z.string(),
    nickname: z.string()
  })
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

apiResponse를 사용하도록 수정했습니다!


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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

baseApi의 baseURL에 이미 /api가 포함되어 있어서 실제 요청이 /api/api/auth/signup으로 나갑니다! /auth/signup으로 변경하면 될 것 같아요!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

baseURL과 중복되지 않도록 /auth/signup로 수정했습니다!! 감사합니다~

// 응답 검증
return signupResponseSchema.parse(response.data)
}
85 changes: 85 additions & 0 deletions src/features/auth-dialog/model/signup.schema.ts
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
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

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
}
Loading
Loading