0

i have followed all the solution and answer from this stack overflow question How to redirect after confirm amazon cognito using confirmation URL?

but I am getting this error

{"errorType":"TypeError","errorMessage":"Cannot read property 'code' of undefined","trace":["TypeError: Cannot read property 'code' of undefined","    at Runtime.module.exports.handler (/var/task/index.js:10:54)","    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}

code of customize message lambda is:

exports.handler = (event, context, callback) => {
    // Identify why was this function invoked
    if(event.triggerSource === "CustomMessage_SignUp") {
        console.log('function triggered');
        console.log(event);
        // Ensure that your message contains event.request.codeParameter. This is the placeholder for code that will be sent
        const { codeParameter } = event.request
        const { userName, region } = event
        const { clientId } = event.callerContext
        const { email } = event.request.userAttributes
        const url = 'https://xxxxxxxxx.execute-api.ap-southeast-1.amazonaws.com/confirm'
        const link = `<a href="${url}?code=${codeParameter}&username=${userName}&clientId=${clientId}&region=${region}&email=${email}" target="_blank">here</a>`
        event.response.emailSubject = "Your verification link"; // event.request.codeParameter
        event.response.emailMessage = `Thank you for signing up. Click ${link} to verify your email.`;
    }

    // Return to Amazon Cognito
    callback(null, event);
};

code for redirect lambda is

'use strict';
var AWS = require('aws-sdk');
AWS.config.setPromisesDependency(require('bluebird'));
var CognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider({ apiVersion: '2016-04-19', region: process.env.REGION });

module.exports.handler = (req, context, callback) => {

  console.log('req');
  console.log(req);
  const confirmationCode = req.queryStringParameters.code
  const username = req.queryStringParameters.username
  const clientId = req.queryStringParameters.clientId
  const region = req.queryStringParameters.region
  const email = req.queryStringParameters.email

  let params = {
    ClientId: clientId,
    ConfirmationCode: confirmationCode,
    Username: username
  }

  var confirmSignUp = CognitoIdentityServiceProvider.confirmSignUp(params).promise()

  confirmSignUp.then(
    (data) => {
      let redirectUrl = process.env.POST_REGISTRATION_VERIFICATION_REDIRECT_URL;
      const response = {
        statusCode: 301,
        headers: {
          Location: redirectUrl,
        }
      };
    
      return callback(null, response);
    }
  ).catch(
    (error) => {
      callback(error)
    }
  )
}
  • customize_message lambda is triggered by the aws cognito which send
    code for verification
  • redirect lambda is triggered by the API gateway

please help thank you in advance

1 Answers1

0

sometihing wrong with access your req parameter. try to use this code

    'use strict';
var AWS = require('aws-sdk');
AWS.config.setPromisesDependency(require('bluebird'));
var CognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider({ apiVersion: '2016-04-19', region: process.env.REGION });

module.exports.handler = (req, context, callback) => {

  console.log('req');
  console.log(req);
  const confirmationCode = req.code;
  const username = req.username;
  const clientId = req.clientId;
  const region = req.region;
  const email = req.email;

  let params = {
    ClientId: clientId,
    ConfirmationCode: confirmationCode,
    Username: username
  }

  var confirmSignUp = CognitoIdentityServiceProvider.confirmSignUp(params).promise()

  confirmSignUp.then(
    (data) => {
      let redirectUrl = process.env.POST_REGISTRATION_VERIFICATION_REDIRECT_URL;
      const response = {
        statusCode: 301,
        headers: {
          Location: redirectUrl,
        }
      };
    
      return callback(null, response);
    }
  ).catch(
    (error) => {
      callback(error)
    }
  )
}

instead of your code.

only change is instead of all this

const confirmationCode = req.queryStringParameters.code;
const username = req.queryStringParameters.username;
const clientId = req.queryStringParameters.clientId;
const region = req.queryStringParameters.region;
const email = req.queryStringParameters.email;

you need to use this

const confirmationCode = req.code;
const username = req.username;
const clientId = req.clientId;
const region = req.region;
const email = req.email;