Custom Amazon Cognito Messages to Phones or E-Mails

To create a custom message for Cognito do the following:

  1. Create a new lambda function NodeJS
  2. Go to your Amazon Cognito User Pool Triggers and assign your new lambda function to the “Custom Message” section.
  3. Choose which messages you want to modify
    1. The available options are(there might be more)
      1. CustomMessage_AdminCreateUser
      2. CustomMessage_ResendCode
      3. CustomMessage_ForgotPassword
        1. NOTICE: when setting the verification code for the forgot password you must have the following data in your emailMessage: {####}
          1. If you don’t have this AWS Cognito will replace your message content with it’s default.
      4. CustomMessage_UpdateUserAttribute
      5. CustomMessage_VerifyUserAttribute
      6. CustomMessage_Authentication
      7. More info: https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-lambda-trigger-syntax-shared.html

 

Sample code below:

const https = require("https");

    //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;
        }

        if(event.triggerSource === "CustomMessage_ForgotPassword") {
            // 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 = "Your temporary password is " + event.request.codeParameter;
            event.response.emailSubject = "Forgot password";
            event.response.emailMessage = "Your temporary password is " + event.request.codeParameter;
        }

        if(event.triggerSource === "CustomMessage_UpdateUserAttribute") {
            // 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 = "Your temporary password is " + event.request.codeParameter;
            event.response.emailSubject = "Verify email";
            event.response.emailMessage = "Your temporary password is " + event.request.codeParameter;
        }

        if(event.triggerSource === "CustomMessage_VerifyUserAttribute") {
            // 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 = "Your temporary password is " + event.request.codeParameter;
            event.response.emailSubject = "Verify email";
            event.response.emailMessage = "Your temporary password is " + event.request.codeParameter;
        }

        if(event.triggerSource === "CustomMessage_ForgotPassword") {
            // 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 = "Your temporary password is " + event.request.codeParameter;
            event.response.emailSubject = "Verify email";
            event.response.emailMessage = "Your temporary password is " + event.request.codeParameter;
        }

        if(event.triggerSource === "CustomMessage_Authentication") {
            // 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 = "Your temporary password is " + event.request.codeParameter;
            event.response.emailSubject = "Verify email";
            event.response.emailMessage = "Your temporary password is " + event.request.codeParameter;
        }
        // Create custom message for other events
    //}
    // Customize messages for other user pools

    //

    // Return result to Cognito
    context.done(null, event);
};

For example you can use the following for custom email verification messages.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.