@@ -5,22 +5,23 @@ import {
55 userExists ,
66 validatePassword ,
77} from "../services/userService" ;
8- import { Request , Response } from "express" ;
8+ import { NextFunction , Request , Response } from "express" ;
99import { omit } from "lodash" ;
1010import { sign } from "../util/jwt" ;
1111import { generateOTP , verifyOTP } from "../util/otp" ;
1212import { sendOTP } from "../helpers/mailHelper" ;
13+ import { ApiError } from "../util/ApiError" ;
1314const omitData = [ "password" ] ;
1415
15- export const registerUser = async ( req : Request , res : Response ) => {
16+ export const registerUser = async ( req : Request , res : Response , next : NextFunction ) => {
1617 try {
1718 let user = req . body ;
1819 const userExist = await userExists ( {
1920 email : user . email ,
2021 mobile : user . mobile ,
2122 } ) ;
2223 if ( userExist ) {
23- throw new Error ( "Email or Mobile is alredy used" ) ;
24+ throw new ApiError ( 400 , "Email or Mobile is alredy used" ) ;
2425 }
2526 user = await createUser ( user ) ;
2627 const userData = omit ( user ?. toJSON ( ) , omitData ) ;
@@ -33,28 +34,22 @@ export const registerUser = async (req: Request, res: Response) => {
3334 msg : "User registered successfully" ,
3435 } ) ;
3536 } catch ( err ) {
36- let msg = "Internal Server Error" ;
37- if ( err instanceof Error ) {
38- msg = err . message ;
39- } else if ( err ) {
40- msg = err ;
41- }
42- return res . status ( 400 ) . json ( { errorMsg : msg , error : true } ) ;
37+ next ( err ) ;
4338 }
4439} ;
4540
46- export const loginUser = async ( req : Request , res : Response ) => {
41+ export const loginUser = async ( req : Request , res : Response , next : NextFunction ) => {
4742 try {
4843 const { email, password } = req . body ;
4944
5045 const user = await findOneUser ( { email } ) ;
5146 if ( ! user ) {
52- throw new Error ( "Email id is incorrect" ) ;
47+ throw new ApiError ( 400 , "Email id is incorrect" ) ;
5348 }
5449
5550 const validPassword = await validatePassword ( user . email , password ) ;
5651 if ( ! validPassword ) {
57- throw new Error ( "Password is incorrect" ) ;
52+ throw new ApiError ( 400 , "Password is incorrect" ) ;
5853 }
5954 const userData = omit ( user ?. toJSON ( ) , omitData ) ;
6055 const accessToken = sign ( { ...userData } ) ;
@@ -65,24 +60,17 @@ export const loginUser = async (req: Request, res: Response) => {
6560 error : false ,
6661 } ) ;
6762 } catch ( err ) {
68- console . error ( err ) ;
69- let msg = "Internal Server Error" ;
70- if ( err instanceof Error ) {
71- msg = err . message ;
72- } else if ( err ) {
73- msg = err ;
74- }
75- return res . status ( 400 ) . json ( { errorMsg : msg , error : true } ) ;
63+ next ( err ) ;
7664 }
7765} ;
7866
79- export const forgotPassword = async ( req : Request , res : Response ) => {
67+ export const forgotPassword = async ( req : Request , res : Response , next : NextFunction ) => {
8068 try {
8169 const { email } = req . body ;
8270
8371 let user = await findOneUser ( { email } ) ;
8472 if ( ! user ) {
85- throw new Error ( "Email id is incorrect" ) ;
73+ throw new ApiError ( 400 , "Email id is incorrect" ) ;
8674 }
8775 user = user ?. toJSON ( ) ;
8876 // generate otp
@@ -91,32 +79,25 @@ export const forgotPassword = async (req: Request, res: Response) => {
9179 const send = await sendOTP ( user . email , otp ) ;
9280 // send otp to email
9381 if ( ! send ) {
94- throw new Error ( "Failed to send OTP" ) ;
82+ throw new ApiError ( 400 , "Failed to send OTP" ) ;
9583 }
9684
9785 return res . status ( 200 ) . json ( {
9886 msg : "Email sent sucessfully" ,
9987 error : false ,
10088 } ) ;
10189 } catch ( err ) {
102- console . error ( err ) ;
103- let msg = "Internal Server Error" ;
104- if ( err instanceof Error ) {
105- msg = err . message ;
106- } else if ( err ) {
107- msg = err ;
108- }
109- return res . status ( 400 ) . json ( { errorMsg : msg , error : true } ) ;
90+ next ( err ) ;
11091 }
11192} ;
11293
113- export const resetPassword = async ( req : Request , res : Response ) => {
94+ export const resetPassword = async ( req : Request , res : Response , next : NextFunction ) => {
11495 try {
11596 const { email, otp, password } = req . body ;
11697
11798 let user = await findOneUser ( { email } ) ;
11899 if ( ! user ) {
119- throw new Error ( "Email id is incorrect" ) ;
100+ throw new ApiError ( 400 , "Email id is incorrect" ) ;
120101 }
121102 user = user ?. toJSON ( ) ;
122103 const isValid = verifyOTP ( user . email , otp ) ;
@@ -136,13 +117,6 @@ export const resetPassword = async (req: Request, res: Response) => {
136117 error : false ,
137118 } ) ;
138119 } catch ( err ) {
139- console . error ( err ) ;
140- let msg = "Internal Server Error" ;
141- if ( err instanceof Error ) {
142- msg = err . message ;
143- } else if ( err ) {
144- msg = err ;
145- }
146- return res . status ( 400 ) . json ( { errorMsg : msg , error : true } ) ;
120+ next ( err ) ;
147121 }
148122} ;
0 commit comments