@@ -6,10 +6,11 @@ import {
66import  {  createUser ,  savePassword  }  from  '@/db/query/User' ; 
77import  {  z  }  from  'zod' ; 
88import  {  signIn  as  signInUser  }  from  '@/auth' ; 
9+ import  {  redirect  }  from  'next/navigation' ; 
910
1011// =============================== signUp =============================== 
1112const  signUpSchema  =  z . object ( { 
12-   email : z . string ( ) . email ( 'Please enter valid message ' ) . min ( 5 ) , 
13+   email : z . string ( ) . email ( 'Please enter valid email address. ' ) . min ( 5 ) , 
1314} ) ; 
1415
1516export  async  function  signUp ( prevState : any ,  formData : FormData )  { 
@@ -22,7 +23,7 @@ export async function signUp(prevState: any, formData: FormData) {
2223    return  { 
2324      type : 'error' , 
2425      errors : validatedFields . error . flatten ( ) . fieldErrors , 
25-       message : 'Missing Fields. Failed to signUp. ' , 
26+       message : 'Missing Fields!! ' , 
2627    } ; 
2728  } 
2829
@@ -32,7 +33,9 @@ export async function signUp(prevState: any, formData: FormData) {
3233    if  ( ! emailData . success )  { 
3334      return  { 
3435        type : 'error' , 
35-         errors : null , 
36+         errors : { 
37+           email : undefined , 
38+         } , 
3639        message : 'Failed to signUp.' , 
3740      } ; 
3841    } 
@@ -46,7 +49,9 @@ export async function signUp(prevState: any, formData: FormData) {
4649    console . error ( 'Failed to signUp' ,  error ) ; 
4750    return  { 
4851      type : 'error' , 
49-       errors : null , 
52+       errors : { 
53+         email : undefined , 
54+       } , 
5055      message : error . message  ||  'Failed to signUp.' , 
5156    } ; 
5257  } 
@@ -56,7 +61,7 @@ export async function signUp(prevState: any, formData: FormData) {
5661const  onBoardingSchema  =  z . object ( { 
5762  name : z . string ( ) . min ( 2 ,  {  message : 'Must be 2 or more characters long'  } ) , 
5863  username : z . string ( ) . min ( 3 ,  {  message : 'Must be 3 or more characters long'  } ) , 
59-   email : z . string ( ) . email ( 'Please enter valid message ' ) . min ( 5 ) , 
64+   email : z . string ( ) . email ( 'Please enter valid email address. ' ) . min ( 5 ) , 
6065  password : z . string ( ) . min ( 8 ,  {  message : 'Must be 8 or more characters long'  } ) , 
6166  password2 : z . string ( ) , 
6267} ) ; 
@@ -79,7 +84,7 @@ export async function onBoarding(
7984    return  { 
8085      type : 'error' , 
8186      errors : validatedFields . error . flatten ( ) . fieldErrors , 
82-       message : 'Missing Fields. Failed to signUp. ' , 
87+       message : 'Missing Fields!! ' , 
8388    } ; 
8489  } 
8590
@@ -92,9 +97,9 @@ export async function onBoarding(
9297        username : undefined , 
9398        email : undefined , 
9499        password : undefined , 
95-         password2 : 'Passwords do not match' , 
100+         password2 : undefined , 
96101      } , 
97-       message : 'Passwords do not match. Failed to signUp. ' , 
102+       message : 'Passwords do not match.' , 
98103    } ; 
99104  } 
100105
@@ -109,8 +114,14 @@ export async function onBoarding(
109114    if  ( user . length  ===  0 )  { 
110115      return  { 
111116        type : 'error' , 
112-         errors : null , 
113-         message : 'Failed to signUp.' , 
117+         errors : { 
118+           name : undefined , 
119+           username : undefined , 
120+           email : undefined , 
121+           password : undefined , 
122+           password2 : undefined , 
123+         } , 
124+         message : 'Failed to signUp. Please try again.' , 
114125      } ; 
115126    } 
116127
@@ -123,7 +134,13 @@ export async function onBoarding(
123134    console . error ( 'Failed to signUp' ,  error ) ; 
124135    return  { 
125136      type : 'error' , 
126-       errors : null , 
137+       errors : { 
138+         name : undefined , 
139+         username : undefined , 
140+         email : undefined , 
141+         password : undefined , 
142+         password2 : undefined , 
143+       } , 
127144      message : error . message  ||  'Failed to signUp.' , 
128145    } ; 
129146  } 
@@ -146,46 +163,43 @@ export async function signIn(prevState: any, formData: FormData) {
146163    return  { 
147164      type : 'error' , 
148165      errors : validatedFields . error . flatten ( ) . fieldErrors , 
149-       message : 'Missing Fields. Failed to signIn. ' , 
166+       message : 'Missing Fields!! ' , 
150167    } ; 
151168  } 
152169
153-   // Call the loginUser function 
154-   let  user  =  await  signInUser ( 'credentials' ,  { 
155-     username : validatedFields . data . username , 
156-     password : validatedFields . data . password , 
157-     redirect : true , 
158-     redirectTo : '/profile' , 
159-   } ) ; 
160- 
161-   if  ( ! user )  { 
162-     return  { 
163-       type : 'error' , 
164-       errors : null , 
165-       message : 'Invalid username or password.' , 
166-     } ; 
170+   try  { 
171+     await  signInUser ( 'credentials' ,  { 
172+       username : validatedFields . data . username , 
173+       password : validatedFields . data . password , 
174+       redirect : false , 
175+     } ) ; 
176+   }  catch  ( error : any )  { 
177+     if  ( error . code  ===  'invalid-credentials' )  { 
178+       return  { 
179+         type : 'error' , 
180+         errors : { 
181+           username : undefined , 
182+           password : undefined , 
183+         } , 
184+         message : error . message , 
185+       } ; 
186+     }  else  { 
187+       return  { 
188+         type : 'error' , 
189+         errors : { 
190+           username : undefined , 
191+           password : undefined , 
192+         } , 
193+         message : 'Something went wrong. Please try again.' , 
194+       } ; 
195+     } 
167196  } 
168- 
169-   return  { 
170-     type : 'success' , 
171-     errors : null , 
172-     message : 'Successfully signed in.' , 
173-   } ; 
174- 
175-   // try { 
176-   // } catch (error: any) { 
177-   //   console.error('Failed to signIn', error); 
178-   //   return { 
179-   //     type: 'error', 
180-   //     errors: null, 
181-   //     message: error.message || 'Failed to signIn.', 
182-   //   }; 
183-   // } 
197+   redirect ( '/profile' ) ; 
184198} 
185199
186200// =============================== forgotPassword =============================== 
187201const  forgetPasswordSchema  =  z . object ( { 
188-   email : z . string ( ) . email ( 'Please enter valid message ' ) . min ( 5 ) , 
202+   email : z . string ( ) . email ( 'Please enter valid email address. ' ) . min ( 5 ) , 
189203} ) ; 
190204
191205export  async  function  forgotPassword ( prevState : any ,  formData : FormData )  { 
@@ -197,7 +211,7 @@ export async function forgotPassword(prevState: any, formData: FormData) {
197211    return  { 
198212      type : 'error' , 
199213      errors : validatedFields . error . flatten ( ) . fieldErrors , 
200-       message : 'Missing Fields. Failed to send email. ' , 
214+       message : 'Missing Fields!! ' , 
201215    } ; 
202216  } 
203217
@@ -209,7 +223,9 @@ export async function forgotPassword(prevState: any, formData: FormData) {
209223    if  ( ! emailData . success )  { 
210224      return  { 
211225        type : 'error' , 
212-         errors : null , 
226+         errors : { 
227+           email : undefined , 
228+         } , 
213229        message : emailData . message  ||  'Failed to send email. Please try again.' , 
214230      } ; 
215231    } 
@@ -223,7 +239,9 @@ export async function forgotPassword(prevState: any, formData: FormData) {
223239    console . error ( 'Failed to send email' ,  error ) ; 
224240    return  { 
225241      type : 'error' , 
226-       errors : null , 
242+       errors : { 
243+         email : undefined , 
244+       } , 
227245      message : error . message  ||  'Failed to send email.' , 
228246    } ; 
229247  } 
@@ -250,7 +268,7 @@ export async function resetPassword(
250268    return  { 
251269      type : 'error' , 
252270      errors : validatedFields . error . flatten ( ) . fieldErrors , 
253-       message : 'Missing Fields. Failed to reset password. ' , 
271+       message : 'Missing Fields!! ' , 
254272    } ; 
255273  } 
256274
@@ -260,9 +278,9 @@ export async function resetPassword(
260278      type : 'error' , 
261279      errors : { 
262280        password : undefined , 
263-         password2 : 'Passwords do not match' , 
281+         password2 : undefined , 
264282      } , 
265-       message : 'Make sure passwords  match. Failed to reset password .' , 
283+       message : 'Passwords do not  match.' , 
266284    } ; 
267285  } 
268286
@@ -272,7 +290,10 @@ export async function resetPassword(
272290    if  ( ! user . success )  { 
273291      return  { 
274292        type : 'error' , 
275-         errors : null , 
293+         errors : { 
294+           password : undefined , 
295+           password2 : undefined , 
296+         } , 
276297        message : user . message  ||  'Failed to reset password.' , 
277298      } ; 
278299    } 
@@ -285,7 +306,10 @@ export async function resetPassword(
285306    console . error ( 'Failed to reset password.' ,  error ) ; 
286307    return  { 
287308      type : 'error' , 
288-       errors : null , 
309+       errors : { 
310+         password : undefined , 
311+         password2 : undefined , 
312+       } , 
289313      message : error . message  ||  'Failed to reset password.' , 
290314    } ; 
291315  } 
@@ -316,7 +340,7 @@ export async function changePassword(
316340    return  { 
317341      type : 'error' , 
318342      errors : validatedFields . error . flatten ( ) . fieldErrors , 
319-       message : 'Missing Fields. Failed to change password. ' , 
343+       message : 'Missing Fields!! ' , 
320344    } ; 
321345  } 
322346
@@ -327,9 +351,9 @@ export async function changePassword(
327351      errors : { 
328352        oldPassword : undefined , 
329353        newPassword : undefined , 
330-         password2 : 'Passwords do not match' , 
354+         password2 : undefined , 
331355      } , 
332-       message : 'Make sure passwords  match. Failed to change password .' , 
356+       message : 'Passwords do not  match.' , 
333357    } ; 
334358  } 
335359
@@ -345,7 +369,7 @@ export async function changePassword(
345369      return  { 
346370        type : 'error' , 
347371        errors : { 
348-           oldPassword : "Old password doesn't match." , 
372+           oldPassword : undefined , 
349373          newPassword : undefined , 
350374          password2 : undefined , 
351375        } , 
@@ -361,7 +385,11 @@ export async function changePassword(
361385    console . error ( 'Failed to change password.' ,  error ) ; 
362386    return  { 
363387      type : 'error' , 
364-       errors : null , 
388+       errors : { 
389+         oldPassword : undefined , 
390+         newPassword : undefined , 
391+         password2 : undefined , 
392+       } , 
365393      message : error . message  ||  'Failed to change password.' , 
366394    } ; 
367395  } 
0 commit comments