I’m using AWS Cognito for user authentication and have implemented a Lambda function to send verification emails via the SendGrid email service. The reason for using Lambda and SendGrid is that my requests to move AWS SES to production mode were always rejected.
However, I’m facing an issue with retrieving the OTP in my trigger. When I access event.request.codeParameter
, it returns {####} instead of the actual OTP.
Here is the Lambda function:
const sgMail = require('@sendgrid/mail');
require('dotenv').config();
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
exports.handler = async (event, context, callback) => {
console.log("Received event:", JSON.stringify(event, null, 2));
const msg = {
to: event.request.userAttributes.email,
from: '[email protected]',
subject: 'Verify Your Email',
text: `Please verify your email by entering the following code: ${event.request.codeParameter}`,
html: `<strong>Please verify your email by entering the following code: ${event.request.codeParameter}</strong>`
};
try {
await sgMail.send(msg);
callback(null, event);
} catch (error) {
console.error('Error sending email:', error);
callback(error, null);
}
};
and here is the received event
2024-05-08T17:48:27.926Z INFO Received event:
{
"version": "1",
"region": "eu-north-1",
"userPoolId": "String",
"userName": "String",
"callerContext": {
"awsSdkVersion": "aws-sdk-unknown-unknown",
"clientId": "String"
},
"triggerSource": "CustomMessage_SignUp",
"request": {
"userAttributes": {
"sub": "40e32520-....",
"cognito:email_alias": "[email protected]",
"email_verified": "false",
"cognito:user_status": "UNCONFIRMED",
"email": "[email protected]"
},
"codeParameter": "{####}",
"linkParameter": "{##Click Here##}",
"usernameParameter": null
},
"response": {
"smsMessage": null,
"emailMessage": null,
"emailSubject": null
}
}
How can I fix the Lambda trigger to correctly retrieve the OTP instead of {####}?
Any guidance on resolving this issue would be greatly appreciated. Thank you!