lambda for CustomMessage_AdminCreateUser not rendering the right message

0

Hi, I created the following lambda to customize the forgot password message. I am willing to use the SAME message for the case of a user created directly on the Cognito user pool interface, but despite the fact that the correct event is called, the user does NOT receive the message I have configured. So,

  • if the event.triggerSource is CustomMessage_ForgotPassword: I enter the "if condition" and log the temporary code and email and the custom message is sent with the custom subject
  • if the event.triggerSource is CustomMessage_AdminCreateUser: I enter the "if condition" and log the temporary code and email but then Cognito default message is used but with the custom subject
exports.handler = (event, context, callback) => {
  console.log("EVENT", event);
 
  if (event.triggerSource === "CustomMessage_ForgotPassword" ||
    event.triggerSource === "CustomMessage_AdminCreateUser") {    
    const { codeParameter } = event.request;
    const { email } = event.request.userAttributes;
    const encodedEmail = encodeURIComponent(email);
    console.log("EMAIL", encodedEmail)
    console.log("PARAM", codeParameter)
    const link = `http://localhost:3000/sign-in/password-confirmation?email=${encodedEmail}&code=${codeParameter}`;
    event.response.emailSubject = "Reset your password";
    event.response.emailMessage = `<body >AWS           
        <div
          style="
            background-color: #fff;
            padding: 30px;
            text-align: center;
            font-size: 18px;
            font-family: Arial, Helvetica, sans-serif;
            color: #2e4057">
          <p>
            <span style="font-size: 30px; font-weight: bold">Hi!</span>           
            Forgot your password?
          </p>
          <br /><br />
          <a
              href=${link}
              target="_blank"              
              >RESET PASSWORD</a
            >         
          <br /><br />
          <p>
            Thank you           
          </p>
        </div>        
     
    </body>`;
  }

  // CallBack to the lambda for the email trigger
  callback(null, event);
};

1 Answer
0

Two elements:

  1. Code in ForgotPassword is really a verification code while in AdminCreateUser it is a temporary password and therefore does allow usage with a link defined as http://localhost:3000/sign-in/password-confirmation?email=${encodedEmail}&code=${codeParameter}
  2. There is a typo is your logic :
const { codeParameter } = event.request;

In fact it should be event.request.codeParameter

AWS
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions