lambda-trigger-auth-challenge-create.js - AWS Code Sample


/* Copyright, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3), which is pending release. The preview version of the SDK is available at The 'SDK for JavaScript Developer Guide' for v3 is also scheduled for release later in 2020, and the topic containing this example will be hosted at Purpose: In lambda-trigger-auth-challenge-create.js a CAPTCHA is created as a challenge to the user. The URL for the CAPTCHA image is added to the public challenge parameters as "captchaUrl", and the expected answer is added to the private challenge parameters. Running the code: 1. On the AWS Lambda service dashboard, choose Create function. 2. On the Create function page, name the function, and choose Create function. 3. Copy and paste the code into the index.js file in the editor, and save the function. 4. Open the Amazon Cognito service. 5. Choose Manage User pools. 6. Choose the User Pool you want to add the trigger to. (If you don't have a user pool, create one.) 7. In General Settings, click Triggers. 8. In the Create Auth Challenge pane, select the Lambda function. */ exports.handler = async (event, context) => { try { if (event.request.challengeName == "CUSTOM_CHALLENGE") { event.response.publicChallengeParameters = {}; event.response.publicChallengeParameters.captchaUrl = "url/123.jpg"; event.response.privateChallengeParameters = {}; event.response.privateChallengeParameters.answer = "5"; event.response.challengeMetadata = "CAPTCHA_CHALLENGE"; } } catch (err) { return null; } };