@@ -11,22 +11,33 @@ const signin = (modelname) => {
1111                return  res . json ( {  Error : "The Model cannot find in Project" } ) 
1212            } 
1313
14-             const  email   =  req . body . email 
14+             const  {   email,  password  }   =  req . body ; 
1515
16-             const  checkemail  =  await  Model . findOne ( {  email : email  } ) 
17- 
18-             if ( checkemail ) { 
19-                 return  res . json ( {  Error : "No User Found by Given Email" } ) 
16+             if  ( ! email  ||  ! password )  { 
17+                 return  res . status ( 400 ) . json ( {  error : "Email and password are required"  } ) ; 
2018            } 
2119
22-             const  checlpass  =  await  bcrypt . compare ( req . body . password ,  checkemail . password ) 
20+             const  user  =  await  Model . findOne ( {  email } ) ; 
21+             if  ( ! user )  { 
22+                 return  res . status ( 404 ) . json ( {  error : "No user found with the given email"  } ) ; 
23+             } 
2324
24-             if ( ! checlpass ) { 
25-                 return  res . json ( {  Error : "Password Not Match..." } ) 
25+             const  isPasswordValid  =  await  bcrypt . compare ( password ,  user . password ) ; 
26+             if  ( ! isPasswordValid )  { 
27+                 return  res . json ( {  error : "Incorrect password"  } ) ; 
2628            } 
2729
28-             const  token  =  jwt . sign ( {  id : checkemail . _id ,  role :checkemail . role  } ,  process . env . JWT_SECRET ) ; 
29-             return  res . json ( {  Status : "Success" ,  Result : checkemail ,  Token : token  } ) 
30+             const  token  =  jwt . sign ( 
31+                 {  id : user . _id ,  role : user . role  } , 
32+                 process . env . JWT_SECRET , 
33+                 {  expiresIn : "1d"  }  
34+             ) ; 
35+ 
36+             return  res . json ( { 
37+                 status : "success" , 
38+                 user, 
39+                 token, 
40+             } ) ; 
3041
3142        } 
3243        catch ( err ) { 
0 commit comments