Email Verification API
Send Verification Email
Generate and send a verification code to the user's email.
Request
- Method: GET
- Can be accessed by: Any user with a valid temporary token
- URL:
/api/verification/mail
- Headers:
- Authorization: Bearer
<temp_token>
- Authorization: Bearer
Response
- Status: 200 OK
- Body:
{ "success": true, "data": "Email sent successfully", "error": null }
Error Responses
-
Unauthorized Access
- Status: 401 Unauthorized
- Body:
{ "success": false, "data": null, "error": "Unauthorized" }
-
Error Sending Email
- Status: 500 Internal Server Error
- Body:
{ "success": false, "data": null, "error": "Error sending email" }
Code Overview
The code defines an asynchronous GET request handler to send a verification code to the user's email.
- The token is extracted from the 'Authorization' header.
- The presence of the token is checked.
- The token is verified using the
verifyTempToken
function. - A verification code is generated.
- A connection to the database is established.
- The verification code is saved in the
TempUser
collection. - The email address is retrieved from the
TempUser
collection. - The email is sent using Nodemailer with the verification code.
- The response is returned indicating the successful email sending.
- Appropriate error responses are returned for unauthorized access or email sending failure.