Redirect User using-Amazon cognito confirmation URL

Jacob Joy
4 min readNov 11, 2019

--

Many developer are raising issues with AWS for the following uncomplicated feature. There are few workaround mentioned in the stack overflow which is valid but not complete. Btw they have invented the wheel I just made it to run effectively.

Please find the complete setup guide. Starting with simple image to make it clear how we are doing this work around,

Step 1: Create a lambda function with the language of your choice, I am going to use node.js for the following example.

Please read Custom Message Lambda Trigger before you proceed, https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-custom-message.html

In this example I am handling only for CustomMessage_SignUp you can customise for others like CustomMessage_ForgotPassword etc. as per your requirement.

Copy the below code to your lambda function,

// Creating a custom URL for the user
exports
.handler = (event, context, callback) => {
if(event.triggerSource === "CustomMessage_SignUp") {
const { codeParameter } = event.request;
const { userName, region } = event;
const { clientId } = event.callerContext;
const { email } = event.request.userAttributes;
const url = 'https://xxxxxxx.execute-api.eu-west-2.amazonaws.com/prod/redirect'
const link = `<a href="${url}?code=${codeParameter}&username=${userName}&clientId=${clientId}&region=${region}&email=${email}" target="_blank">Click the link to verify</a>`;
event.response.emailSubject = "Your verification link";
event.response.emailMessage = `Thank you for signing up. Click ${link} to verify your email.`;
}
// CallBack to the lambda for the email trigger
callback(null, event);
};

Note: The const URL should be updated once you have API Gateway setup.

Step 2: Under you Cognito Trigger choose the custom message and select the lambda function which you have created

Step 3: Create a GET API in your API Gateway

Don’t set any authorization or don’t enable any API key required.

Create a REST API
Adding resource to the API

Step 4: Create another Lambda Function to validate and confirm the user.

'use strict';
var AWS = require('aws-sdk');
AWS.config.setPromisesDependency(require('bluebird'));
var CognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider({
apiVersion: '2019-11-07',
region: process.env.REGION
});
exports.handler = (req, context, callback) => {console.log(req);
const confirmationCode = req.code;
const username = req.username;
const clientId = req.clientId;
let params = {
ClientId: clientId,
ConfirmationCode: confirmationCode,
Username: username
};
//Validating the user
let confirmSignUp = CognitoIdentityServiceProvider.confirmSignUp(params).promise();
//Returning the redirect url
confirmSignUp.then(
(data) => {
context.succeed({
location: process.env.POST_REGISTRATION_VERIFICATION_REDIRECT_URL
});
}
).catch(
(error) => {
callback(error.message)
}
)
};

Create a node application in the IDE of your choice and then build using npm install and once your application is build.
Create a zip (Inside the root folder 5 file approx. based on your IDE) and upload to your lambda application.

Guide for node beginner,
Lambda Handler method should be = index.handler

Set the following in the lambda env variable
POST_REGISTRATION_VERIFICATION_REDIRECT_URL
REGION

In your Application you are validating the user in your cognito user pool and returning the URL for the re-direct.

Not in Scope: The error scenario can be also handled based on your requirement.

Step5: Go back to the API Gateway, Create a GET request and Put in the Lambda function which you have to create in Step4 and update the mapping template.

Create a GET request
Add the Mapping Template under Integration Request

Step6: Now we have to redirect the request based on your lambda response,
Under Method Response in the API Gateway delete the 200 and create a 302 and add the response header Location as per the image,

And then in the Integration Response you need remove the 200 and add the 302 and then add the header Mappings value as integration.response.body.location (Be careful with the case)

Most important Step: Once all setup Deploy the API and update the const URL API in the Lambda Function, created in the step1.

Try creating a test user and user must have got email link something like this,

https://xxxx-api.eu-west-2.amazonaws.com/xx/xx/xx/redirect?code=xx&amp;username=xx-0105-486xxf-xxx-xxx&amp;clientId=xxxxx&amp;region=eu-west-2&amp;email=test@gmail.com

Thank You, Hope it helps.

--

--