@@ -4,35 +4,27 @@ import { createUser, findUserByEmail } from '@/services/authService';
44import { generateAccessToken , generateRefreshToken } from '@/utils/generateToken' ;
55import { AUTH_MESSAGES } from '@/constants/auth_message' ;
66import { storeRefreshToken } from '@/services/refreshTokenService' ;
7-
8- // 유효성 검사 함수
9- const validateSignupInput = ( username : string , email : string , password : string ) : string | null => {
10- if ( ! username || username . length < 3 || username . length > 30 ) {
11- return AUTH_MESSAGES . USERNAME_INVALID ;
12- }
13-
14- const emailRegex = / ^ [ ^ \s @ ] + @ [ ^ \s @ ] + \. [ ^ \s @ ] + $ / ;
15- if ( ! email || ! emailRegex . test ( email ) ) {
16- return AUTH_MESSAGES . EMAIL_INVALID ;
17- }
18-
19- if ( ! password || password . length < 8 ) {
20- return AUTH_MESSAGES . PASSWORD_INVALID ;
21- }
22-
23- return null ;
24- } ;
7+ import { validateUser } from '@medi-map/validation' ;
258
269// 회원가입 처리
2710export const signup = async ( req : Request , res : Response ) : Promise < Response > => {
2811 const { username, email, password } = req . body ;
2912
3013 try {
3114 // 유효성 검사
32- const validationError = validateSignupInput ( username , email , password ) ;
33- if ( validationError ) {
34- console . error ( 'Validation error:' , validationError ) ;
35- return res . status ( 400 ) . json ( { message : validationError } ) ;
15+ const validationResult = validateUser ( req . body ) ;
16+
17+ if ( ! validationResult . success ) {
18+ const flattenedErrors = validationResult . error . flatten ( ) . fieldErrors ;
19+
20+ console . error ( 'Validation error:' , flattenedErrors ) ;
21+
22+ const [ firstField , firstMessages ] = Object . entries ( flattenedErrors ) [ 0 ] ;
23+
24+ return res . status ( 400 ) . json ( {
25+ message : firstMessages [ 0 ] ,
26+ field : firstField
27+ } ) ;
3628 }
3729
3830 // 이메일 중복 확인
0 commit comments