4
votes

I am working on AWS Cognito and below are two questions for Message Customizations.

1) I am working on AWS Cognito for "email verification messages" using "Link" Verification type. I am getting an issue with "Email message" to make it dynamic.

2) How to send different "Email message" content based on user group or conditionally?

Please suggest.

Thanks

1
There is a cognito trigger, "Custom message" which is invoked before a verification or MFA message is sent, allowing you to customize the message dynamically. You can create a lambda function to create content based on the user group or whatever conditon you have.Aman Gupta
Yes trigger is working but it's sending default Email Message from "Email message" section of Cognito.Somnath Rokade
it looks like an issue with your custom msg, {##link##} is not configured correctlyAman Gupta
Yes the HTML tags works as well. Just replace you plain text with a your HTML content. mAke sure your linkParameter is properly added in the content. It will work.Aman Gupta
Also you need to authorize cognito to send emails using ses. Check this docs.aws.amazon.com/cognito/latest/developerguide/…Aman Gupta

1 Answers

2
votes

This is what worked for me. So if someone knows a better answer please point me. Sending a link selecting the "link" option didn't work for me. So I tried adding the link manually in the body selecting the "code" option. But that didn't work at the start either as @SomnathRokade and @snehanshu.js suggested.

This is my lambda function.

exports.handler = async (event, context) => {
var welcomeMessage = `
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<p>Dear ${event.userName},</p>

<p>Anything</p>

<p>Any text before the link <a href="http://localhost:3000/changePassword?username=${event.userName}">here.</a> Your username is your Network Rail email address. Your temporary password is ${event.request.codeParameter}.</p>

<p>Any text</p>

<p>Any other text</p>

<p>Many Thanks </p>
</body>
</html>
`;

if (event.triggerSource === "CustomMessage_AdminCreateUser") {
event.response.emailSubject = "CAT Welcome Email";
event.response.emailMessage = welcomeMessage;
}
return context.done(null,event);
};

This DID NOT work.

I googled and found different places referring to "event.request.userName", "event.request.userAttributes.sub" etc instead of "event.userName". But they didn't work either.

Then I tried printing the event object in lambda console logs. I found the value for all of the above fields were same. Interestingly there was no "event.request.userName" field in the object. Instead there was "event.request.usernameParameter". Still, the value was the same as "event.userName". Anyway, I tried adding "event.request.usernameParameter" and surprisingly it worked.

After all, I found this doc explaining it.

So the bottom line is use "Code" option with having "event.request.usernameParameter" and "event.request.codeParameter" in the email body. It worked.

Hope I helped somebody.