1
1
import { redirect } from 'next/navigation'
2
- import { z } from 'zod'
3
2
3
+ import { checkAdminExists } from '@/lib/auth-helpers'
4
4
import { Input } from '@/components/ui/input'
5
5
import { Separator } from '@/components/ui/separator'
6
- import { signIn } from '@/lib/auth'
7
- import { checkAdminExists } from '@/lib/auth-helpers'
8
- import axiosInstance from '@/utils/axios'
9
6
import FormWrapper , { SubmitButton } from './form-wrapper'
10
-
11
- // Form validation schema
12
- const formSchema = z
13
- . object ( {
14
- firstName : z
15
- . string ( )
16
- . min ( 3 , { message : 'First Name must be at least 3 characters.' } ) ,
17
- lastName : z
18
- . string ( )
19
- . min ( 3 , { message : 'Last Name must be at least 3 characters.' } ) ,
20
- email : z
21
- . string ( )
22
- . email ( { message : 'Please provide a valid email address.' } ) ,
23
- password : z
24
- . string ( )
25
- . min ( 5 , { message : 'Password must be at least 5 characters long.' } ) ,
26
- confirmPassword : z . string ( ) ,
27
- } )
28
- . refine ( ( data ) => data . password === data . confirmPassword , {
29
- message : 'Passwords must match.' ,
30
- path : [ 'confirmPassword' ] ,
31
- } )
32
-
33
- // Server action for form submission
34
- async function registerUser ( state : { error ?: string } , formData : FormData ) {
35
- 'use server'
36
-
37
- try {
38
- // Extract and validate form data
39
- const rawFormData = {
40
- firstName : formData . get ( 'firstName' ) as string ,
41
- lastName : formData . get ( 'lastName' ) as string ,
42
- email : formData . get ( 'email' ) as string ,
43
- password : formData . get ( 'password' ) as string ,
44
- confirmPassword : formData . get ( 'confirmPassword' ) as string ,
45
- }
46
-
47
- // Validate form data
48
- const validationResult = formSchema . safeParse ( rawFormData )
49
- if ( ! validationResult . success ) {
50
- const formattedErrors = validationResult . error . format ( )
51
- const errorMessages = [ ]
52
-
53
- if ( formattedErrors . firstName ?. _errors ) {
54
- errorMessages . push ( formattedErrors . firstName . _errors [ 0 ] )
55
- }
56
- if ( formattedErrors . lastName ?. _errors ) {
57
- errorMessages . push ( formattedErrors . lastName . _errors [ 0 ] )
58
- }
59
- if ( formattedErrors . email ?. _errors ) {
60
- errorMessages . push ( formattedErrors . email . _errors [ 0 ] )
61
- }
62
- if ( formattedErrors . password ?. _errors ) {
63
- errorMessages . push ( formattedErrors . password . _errors [ 0 ] )
64
- }
65
- if ( formattedErrors . confirmPassword ?. _errors ) {
66
- errorMessages . push ( formattedErrors . confirmPassword . _errors [ 0 ] )
67
- }
68
-
69
- return { error : errorMessages . join ( ', ' ) }
70
- }
71
-
72
- const { firstName, lastName, email, password } = validationResult . data
73
-
74
- await axiosInstance . post ( '/api/auth/register' , {
75
- firstName,
76
- lastName,
77
- email,
78
- password,
79
- } )
80
-
81
- await signIn ( 'credentials' , {
82
- email,
83
- password,
84
- redirect : false ,
85
- } )
86
-
87
- redirect ( '/dashboard' )
88
- } catch ( error : any ) {
89
- console . error ( 'Unexpected error during registration:' , error )
90
- return { error : error . message || 'An unexpected error occurred' }
91
- }
92
- }
7
+ import { registerUser } from './actions'
93
8
94
9
export default async function RegisterPage ( ) {
95
10
const adminExists = await checkAdminExists ( )
@@ -112,67 +27,67 @@ export default async function RegisterPage() {
112
27
< label htmlFor = 'firstName' className = 'text-sm font-medium' >
113
28
First Name
114
29
</ label >
115
- < Input
30
+ < Input
116
31
id = 'firstName'
117
- name = 'firstName'
118
- placeholder = 'John'
119
- required
32
+ name = 'firstName'
33
+ placeholder = 'John'
34
+ required
120
35
minLength = { 3 }
121
36
/>
122
37
</ div >
123
38
< div className = 'w-1/2' >
124
39
< label htmlFor = 'lastName' className = 'text-sm font-medium' >
125
40
Last Name
126
41
</ label >
127
- < Input
42
+ < Input
128
43
id = 'lastName'
129
- name = 'lastName'
130
- placeholder = 'Doe'
131
- required
44
+ name = 'lastName'
45
+ placeholder = 'Doe'
46
+ required
132
47
minLength = { 3 }
133
48
/>
134
49
</ div >
135
50
</ div >
136
-
51
+
137
52
{ /* Email Section */ }
138
53
< div >
139
54
< label htmlFor = 'email' className = 'text-sm font-medium' >
140
55
Email
141
56
</ label >
142
- < Input
57
+ < Input
143
58
id = 'email'
144
- name = 'email'
59
+ name = 'email'
145
60
type = 'email'
146
- placeholder = 'johndoe@xyz.com'
147
- required
61
+ placeholder = 'johndoe@xyz.com'
62
+ required
148
63
/>
149
64
</ div >
150
-
65
+
151
66
{ /* Password Section */ }
152
67
< div >
153
68
< label htmlFor = 'password' className = 'text-sm font-medium' >
154
69
Password
155
70
</ label >
156
- < Input
71
+ < Input
157
72
id = 'password'
158
- name = 'password'
73
+ name = 'password'
159
74
type = 'password'
160
- placeholder = '••••••••'
161
- required
75
+ placeholder = '••••••••'
76
+ required
162
77
minLength = { 5 }
163
78
/>
164
79
</ div >
165
-
80
+
166
81
< div >
167
82
< label htmlFor = 'confirmPassword' className = 'text-sm font-medium' >
168
83
Confirm Password
169
84
</ label >
170
- < Input
85
+ < Input
171
86
id = 'confirmPassword'
172
- name = 'confirmPassword'
87
+ name = 'confirmPassword'
173
88
type = 'password'
174
- placeholder = '••••••••'
175
- required
89
+ placeholder = '••••••••'
90
+ required
176
91
/>
177
92
</ div >
178
93
0 commit comments