@@ -132,47 +132,33 @@ const sendOTP = async (email: string, otp: string, phone: string) => {
132132export const verifyPhone = async (
133133 req : Request < unknown , unknown , VerifyPhoneRequest > ,
134134 res : Response ,
135+ next : NextFunction ,
135136) => {
136- const { phoneNum, password, country, email, userRole } = req . body ;
137+ try {
138+ const { phoneNum, password, country, email, userRole } = req . body ;
137139
138- // Input validation
139- if ( ! email || ! password ) {
140- return res
141- . status ( 400 )
142- . json ( { status : "FAILED" , message : "Empty input fields" } ) ;
143- }
140+ // Input validation
141+ if ( ! email || ! password ) {
142+ throw new AppError ( "Empty input fields" , 400 ) ;
143+ }
144144
145- if ( password . length < 8 ) {
146- return res . status ( 400 ) . json ( {
147- status : "FAILED" ,
148- message : "Password must be at least 8 characters" ,
149- } ) ;
150- }
145+ if ( password . length < 8 ) {
146+ throw new AppError ( "Password must be at least 8 characters" , 400 ) ;
147+ }
151148
152- if ( ! / ^ [ \w - ] + @ ( [ \w - ] + \. ) + [ \w - ] { 2 , 4 } $ / . test ( email ) ) {
153- return res
154- . status ( 400 )
155- . json ( { status : "FAILED" , message : "Invalid email entered" } ) ;
156- }
149+ if ( ! / ^ [ \w - ] + @ ( [ \w - ] + \. ) + [ \w - ] { 2 , 4 } $ / . test ( email ) ) {
150+ throw new AppError ( "Invalid email entered" , 400 ) ;
151+ }
157152
158- const validRoles = [ "buyer" , "farmer" ] as const ;
159- if (
160- ! userRole ||
161- ! validRoles . includes ( userRole as ( typeof validRoles ) [ number ] )
162- ) {
163- return res . status ( 400 ) . json ( {
164- status : "FAILED" ,
165- message : `Invalid role: ${ userRole } . Valid roles are: ${ validRoles . join ( ", " ) } ` ,
166- } ) ;
167- }
153+ const validRoles = [ "buyer" , "farmer" ] as const ;
154+ if ( ! userRole || ! validRoles . includes ( userRole as ( typeof validRoles ) [ number ] ) ) {
155+ throw new AppError ( `Invalid role: ${ userRole } . Valid roles are: ${ validRoles . join ( ", " ) } ` , 400 ) ;
156+ }
168157
169- try {
170158 // Check if user already exists
171159 const userExists = await User . findOne ( { where : { email } } ) ;
172160 if ( userExists ) {
173- return res
174- . status ( 400 )
175- . json ( { message : "This email is already registered." } ) ;
161+ throw new AppError ( "This email is already registered." , 400 ) ;
176162 }
177163
178164 // Find or create role
@@ -236,13 +222,11 @@ export const verifyPhone = async (
236222 userID : user . id ,
237223 } ) ;
238224 } catch ( error ) {
239- if ( error instanceof AppError ) {
240- return res . status ( error . statusCode ) . json ( { message : error . message } ) ;
241- }
242225 if ( error instanceof Error ) {
243- return res . status ( 500 ) . json ( { message : error . message } ) ;
226+ next ( new AppError ( error . message , error instanceof AppError ? error . statusCode : 500 ) ) ;
227+ } else {
228+ next ( new AppError ( "An unexpected error occurred" , 500 ) ) ;
244229 }
245- return res . status ( 500 ) . json ( { message : "An unexpected error occurred" } ) ;
246230 }
247231} ;
248232
@@ -419,7 +403,11 @@ export const logIn = async (
419403 userData : user ,
420404 } ) ;
421405 } catch ( error ) {
422- next ( error ) ;
406+ if ( error instanceof Error ) {
407+ next ( new AppError ( error . message , error instanceof AppError ? error . statusCode : 500 ) ) ;
408+ } else {
409+ next ( new AppError ( "An unexpected error occurred" , 500 ) ) ;
410+ }
423411 }
424412} ;
425413
0 commit comments