@@ -96,11 +96,50 @@ app.post("/register", async (req, res) => {
96
96
let sql = "SELECT * FROM users WHERE email = ?" ;
97
97
let rows = await conn . query ( sql , [ userEmail ] ) ;
98
98
if ( rows . length > 0 ) {
99
- res
100
- . status ( 400 )
101
- . send (
102
- "Email already in use, search your inbox for a confirmation email or contact an admin for assistance" ,
103
- ) ;
99
+ // If the account is not active, resend the confirmation email
100
+ if ( ! rows [ 0 ] . active ) {
101
+ sql = "SELECT token FROM confirmations WHERE user_id = ?" ;
102
+ const tokenRows = await conn . query ( sql , [ rows [ 0 ] . id ] ) ;
103
+ if ( tokenRows . length > 0 ) {
104
+ const token = tokenRows [ 0 ] . token ;
105
+ try {
106
+ await sendConfirmationEmail (
107
+ userEmail ,
108
+ req . headers . host ,
109
+ token ,
110
+ transporter ,
111
+ ) ;
112
+ res . send (
113
+ "Account already exists but is not active. Confirmation email has been resent. Check your inbox and spam folder." ,
114
+ ) ;
115
+ } catch ( err ) {
116
+ console . error ( err ) ;
117
+ res . status ( 500 ) . send ( "Failed to send confirmation email" ) ;
118
+ return ;
119
+ }
120
+ } else {
121
+ res
122
+ . status ( 400 )
123
+ . send (
124
+ "Account already exists but is not active, and no confirmation token found. Contact an admin for assistance." ,
125
+ ) ;
126
+ }
127
+ } else {
128
+ res . status ( 400 ) . send ( "Account already exists and is active." ) ;
129
+ }
130
+ return ;
131
+ }
132
+
133
+ try {
134
+ await sendConfirmationEmail (
135
+ userEmail ,
136
+ req . headers . host ,
137
+ token ,
138
+ transporter ,
139
+ ) ;
140
+ } catch ( err ) {
141
+ console . error ( err ) ;
142
+ res . status ( 500 ) . send ( "Failed to send confirmation email" ) ;
104
143
return ;
105
144
}
106
145
@@ -118,27 +157,32 @@ app.post("/register", async (req, res) => {
118
157
if ( conn ) conn . end ( ) ;
119
158
}
120
159
121
- // Send email with the token
160
+ res . send ( "Check your email for a confirmation link" ) ;
161
+ } ) ;
162
+
163
+ function sendConfirmationEmail ( userEmail , host , token , transporter ) {
122
164
const mailOptions = {
123
165
from : `"${ process . env . MAIL_NAME } " <${ process . env . MAIL_FROM } >` ,
124
166
to : userEmail ,
125
167
subject : "Registration Confirmation for mc.chs.se" ,
126
168
text : `Hello,
127
- Please confirm your registration by clicking the following link: http://${ req . headers . host } /confirm/${ token }
169
+ Please confirm your registration by clicking the following link: http://${ host } /confirm/${ token }
128
170
129
171
If you did not request this, please ignore this email.` ,
130
172
} ;
131
173
132
- transporter . sendMail ( mailOptions , ( err ) => {
133
- if ( err ) {
134
- console . error ( "There was an error: " , err ) ;
135
- res . status ( 500 ) . send ( "Failed to send email" ) ;
136
- } else {
137
- console . log ( "Email sent" ) ;
138
- res . send ( "Check your email for a confirmation link" ) ;
139
- }
174
+ return new Promise ( ( resolve , reject ) => {
175
+ transporter . sendMail ( mailOptions , ( err , info ) => {
176
+ if ( err ) {
177
+ console . error ( "There was an error: " , err ) ;
178
+ reject ( err ) ;
179
+ } else {
180
+ console . log ( "Email sent: " , info . response ) ;
181
+ resolve ( info ) ;
182
+ }
183
+ } ) ;
140
184
} ) ;
141
- } ) ;
185
+ }
142
186
143
187
app . get ( "/confirm/:token" , async ( req , res ) => {
144
188
const token = req . params . token ;
@@ -160,7 +204,7 @@ app.get("/confirm/:token", async (req, res) => {
160
204
}, 3000);
161
205
</script>` ) ;
162
206
} else {
163
- res . send ( "Invalid token. Try again. <a href='/'>Go to back </a>" ) ;
207
+ res . send ( "Invalid token. Try again. <a href='/'>Go to start </a>" ) ;
164
208
}
165
209
} catch ( err ) {
166
210
console . error ( err ) ;
0 commit comments