@@ -83,17 +83,8 @@ export async function createUser(
83
83
hashedPassword
84
84
) ;
85
85
86
- const emailToken = crypto . randomBytes ( 16 ) . toString ( "hex" ) ;
87
- await redisClient . set ( email , emailToken , { EX : 60 * 5 } ) ; // expire in 5 minutes
88
- await sendAccVerificationMail (
89
- email ,
90
- ACCOUNT_VERIFICATION_SUBJ ,
91
- username ,
92
- `http://localhost:3001/api/users/verify-email/${ email } /${ emailToken } `
93
- ) ;
94
-
95
86
return res . status ( 201 ) . json ( {
96
- message : `Created new user ${ username } successfully` ,
87
+ message : `Created new user ${ username } successfully. ` ,
97
88
data : formatUserResponse ( createdUser ) ,
98
89
} ) ;
99
90
} else {
@@ -109,6 +100,38 @@ export async function createUser(
109
100
}
110
101
}
111
102
103
+ export const sendVerificationMail = async (
104
+ req : Request ,
105
+ res : Response
106
+ ) : Promise < Response > => {
107
+ try {
108
+ const { email } = req . body ;
109
+ const user = await _findUserByEmail ( email ) ;
110
+
111
+ if ( ! user ) {
112
+ return res . status ( 404 ) . json ( { message : `User ${ email } not found` } ) ;
113
+ }
114
+
115
+ const emailToken = crypto . randomBytes ( 16 ) . toString ( "hex" ) ;
116
+ await redisClient . set ( email , emailToken , { EX : 60 * 5 } ) ; // expire in 5 minutes
117
+ await sendAccVerificationMail (
118
+ email ,
119
+ ACCOUNT_VERIFICATION_SUBJ ,
120
+ user . username ,
121
+ `http://localhost:3001/api/users/verify-email/${ email } /${ emailToken } `
122
+ ) ;
123
+
124
+ return res
125
+ . status ( 200 )
126
+ . json ( { message : "Verification email sent. Please check your inbox." } ) ;
127
+ } catch ( error ) {
128
+ return res . status ( 500 ) . json ( {
129
+ message : "Unknown error when sending verification email!" ,
130
+ error,
131
+ } ) ;
132
+ }
133
+ } ;
134
+
112
135
export const verifyUser = async (
113
136
req : Request ,
114
137
res : Response
@@ -137,10 +160,10 @@ export const verifyUser = async (
137
160
return res
138
161
. status ( 200 )
139
162
. json ( { message : `User ${ email } verified successfully.` } ) ;
140
- } catch {
163
+ } catch ( error ) {
141
164
return res
142
165
. status ( 500 )
143
- . json ( { message : "Unknown error when verifying user!" } ) ;
166
+ . json ( { message : "Unknown error when verifying user!" , error } ) ;
144
167
}
145
168
} ;
146
169
0 commit comments