@@ -2,59 +2,59 @@ import type { Request, Response, NextFunction } from "express";
22import bcrypt from "bcryptjs" ;
33import User from "../models/userModel.js" ;
44import { generateToken } from "../utils/generateToken.js" ;
5- import { validateEmail , validatePassword } from "../utils/validateInputs.js" ;
5+ import { userSchema , loginSchema } from "../utils/validateInputs.js" ;
66
77export const registerUser = async ( req : Request , res : Response , next : NextFunction ) => {
8- try {
9- const { name , email , password } = req . body ;
8+ try {
9+ const parseResult = userSchema . safeParse ( req . body ) ;
1010
11- if ( ! name || ! email || ! password )
12- return res . status ( 400 ) . json ( { success : false , message : "All fields are required" } ) ;
13-
14- if ( ! validateEmail ( email ) )
15- return res . status ( 400 ) . json ( { success : false , message : "Invalid email format" } ) ;
11+ if ( ! parseResult . success ) {
12+ return res . status ( 400 ) . json ( { success : false , message : parseResult . error . issues [ 0 ] ?. message } ) ;
13+ }
1614
17- if ( ! validatePassword ( password ) )
18- return res . status ( 400 ) . json ( { success : false , message : "Password must be at least 6 characters" } ) ;
15+ const { name, email, password } = parseResult . data ;
1916
20- const existingUser = await User . findOne ( { email } ) ;
21- if ( existingUser )
22- return res . status ( 400 ) . json ( { success : false , message : "Email already registered" } ) ;
17+ const existingUser = await User . findOne ( { email } ) ;
18+ if ( existingUser )
19+ return res . status ( 400 ) . json ( { success : false , message : "Email already registered" } ) ;
2320
24- const hashedPassword = await bcrypt . hash ( password , 10 ) ;
25- const user = await User . create ( { name, email, password : hashedPassword } ) ;
21+ const hashedPassword = await bcrypt . hash ( password , 10 ) ;
22+ const user = await User . create ( { name, email, password : hashedPassword } ) ;
2623
27- res . status ( 201 ) . json ( {
28- success : true ,
29- message : "User registered successfully" ,
30- token : generateToken ( user . _id . toString ( ) ) ,
31- } ) ;
32- } catch ( err ) {
33- next ( err ) ;
34- }
24+ res . status ( 201 ) . json ( {
25+ success : true ,
26+ message : "User registered successfully" ,
27+ token : generateToken ( user . _id . toString ( ) ) ,
28+ } ) ;
29+ } catch ( err ) {
30+ next ( err ) ;
31+ }
3532} ;
3633
3734export const loginUser = async ( req : Request , res : Response , next : NextFunction ) => {
38- try {
39- const { email, password } = req . body ;
40-
41- if ( ! email || ! password )
42- return res . status ( 400 ) . json ( { success : false , message : "Email and password required" } ) ;
43-
44- const user = await User . findOne ( { email } ) ;
45- if ( ! user )
46- return res . status ( 400 ) . json ( { success : false , message : "Invalid credentials" } ) ;
47-
48- const isMatch = await bcrypt . compare ( password , user . password ) ;
49- if ( ! isMatch )
50- return res . status ( 400 ) . json ( { success : false , message : "Invalid credentials" } ) ;
51-
52- res . json ( {
53- success : true ,
54- message : "Login successful" ,
55- token : generateToken ( user . _id . toString ( ) ) ,
56- } ) ;
57- } catch ( err ) {
58- next ( err ) ;
35+ try {
36+ const parseResult = loginSchema . safeParse ( req . body ) ;
37+
38+ if ( ! parseResult . success ) {
39+ return res . status ( 400 ) . json ( { success : false , message : parseResult . error . issues [ 0 ] ?. message || "Validation error" } ) ;
5940 }
41+
42+ const { email, password } = parseResult . data ;
43+
44+ const user = await User . findOne ( { email } ) ;
45+ if ( ! user )
46+ return res . status ( 400 ) . json ( { success : false , message : "Invalid credentials" } ) ;
47+
48+ const isMatch = await bcrypt . compare ( password , user . password ) ;
49+ if ( ! isMatch )
50+ return res . status ( 400 ) . json ( { success : false , message : "Invalid credentials" } ) ;
51+
52+ res . json ( {
53+ success : true ,
54+ message : "Login successful" ,
55+ token : generateToken ( user . _id . toString ( ) ) ,
56+ } ) ;
57+ } catch ( err ) {
58+ next ( err ) ;
59+ }
6060} ;
0 commit comments