I create my users through the cognito-idp API calling adminCreateUser, but the problem I am having is that if I create the user that belongs to develop or to production I have set different environments and the link to click on the email would be a different URL.
At the moment I am sending an email that looks like this:
<h3>Hello {username} !</h3>
<p>To finish your subscription please click on <a href="http://localhost:3000/signup?token={####}" >finish my subscription</a></p>
But as you can see it would only cover the developer option, I would like instead to send an email with some placeholders that I can use dynamically whether we are creating a developer user or a production user like the use of the tag {url} would be a great help.
This is what I would like to do:
<h3>Hello {name} !</h3>
<p>Your username is {username}</p>
<p>To finish your subscription to {project} please click on <a href="{url}{####}" >finish my subscription</a></p>
Does AWS Cognito accepts this? I have read that you can do something similar through lambdas but not so sure if I can create my own variables or if its only the existing variables in cognito.
Somebody with more experience with cognito that can give me a hand on this?
Many thanks!
You can use the AWS-Cognito-Custom-Message-Trigger
to accomplish this. It is a bit more complicated because you are handling messages in a lambda function, but it is the only way to get the ability to access attributes and group names and such in your response.
Example code for the trigger lambda:
exports.handler = (event, context, callback) => {
//
if(event.userPoolId === "theSpecialUserPool") {
// Identify why was this function invoked
if(event.triggerSource === "CustomMessage_AdminCreateUser") {
// Ensure that your message contains event.request.codeParameter event.request.usernameParameter. This is the placeholder for the code and username that will be sent to your user.
event.response.smsMessage = "Welcome to the service. Your user name is " + event.request.usernameParameter + " Your temporary password is " + event.request.codeParameter;
event.response.emailSubject = "Welcome to the service";
event.response.emailMessage = "Welcome to the service. Your user name is " + event.request.usernameParameter + " Your temporary password is " + event.request.codeParameter;
}
// Create custom message for other events
}
// Customize messages for other user pools
// Return to Amazon Cognito
callback(null, event);
};
Docs here: https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-custom-message.html