Search code examples
javascripttypescriptamazon-web-servicesamazon-dynamodbamazon-cognito

What kind of event does Cognito send from Pre Sign-up Lambda Trigger


I have created a PreSignup Lambda function to be used with Cognito Pre-SignUp trigger with the following code:

import { APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';

export async function PreSignup(event: any, context: APIGatewayEventDefaultAuthorizerContext,) {
  console.log("...event:", event);
  console.log("...context:", context);

  let userName = event.userName;
  let email = event.request.userAttributes.email;

  console.log("...userName:", userName);
  console.log("...email:", email);

  // Confirming the event so Cogntio doesnt resend it again:
  event.response.autoConfirmUser = true;
  event.response.autoVerifyPhone = true;
  event.response.autoVerifyEmail = true;
  return event;
}

Instead of setting up the event to any with function PreSignup(event: any) I would rather specify a type of the event.

I tried to specify it as APIGatewayProxyEvent like so:

function PreSignup(event: APIGatewayProxyEvent, context: APIGatewayEventDefaultAuthorizerContext,) {...}

but it doesn't work as the event sent by Cognito Pre Sign-Up trigger is not a APIGatewayProxyEvent type. What kind of event is it?

Edited later:

Below is copy/pasted event that was pushed by Cognito PreSignUp Trigger:

{
  version: '1',
  region: 'us-east-1',
  userPoolId: 'us-east-1_abcdef',
  userName: '[email protected]',
  callerContext: {
    awsSdkVersion: 'aws-sdk-nodejs-2.799.0',
    clientId: 'CLIENT_ID_NOT_APPLICABLE'
  },
  triggerSource: 'PreSignUp_AdminCreateUser',
  request: {
    userAttributes: { email: '[email protected]' },
    validationData: null
  },
  response: {
    autoConfirmUser: false,
    autoVerifyEmail: false,
    autoVerifyPhone: false
  }
}

Below is an example of another AWS event this time pushed by AWS EventBridge (similar to the one pushed by Cognito Trigger):

{
  version: '0',
  id: '0ee136cb-ea53-f9e0-a6a9-232dfb78b7d0',
  'detail-type': 'UserCreated',
  source: 'my.company.endpointname',
  account: '123456789012',
  time: '2021-01-29T03:05:54Z',
  region: 'us-east-1',
  resources: [],
  detail: { foo: 'bar', createdAt: 1611889553709 }
}

Solution

  • PreSignUp_SignUp: Your standard Cognito pool (username/password) signup

    PreSignUp_AdminCreateUser: When a user is created using the admin method (to be clear, not when your creating an admin, but when you are an admin creating a different user).

    PreSignUp_ExternalProvider: If you have third party signup, like Google or facebook

    {
       "version":"1",
       "region":"us-east-1",
       "userPoolId":"us-east-1_xxxxxxx",
       "userName":"2d4eb80f-7998-xxxx-xxxx-xxxxxxxxxx",
       "callerContext":{
          "awsSdkVersion":"aws-sdk-unknown-unknown",
          "clientId":"xxxxxxxxxxxxxxxxxxxxxxxxxx"
       },
       "triggerSource":"PreSignUp_SignUp",
       "request":{
          "userAttributes":{
             "email":"[email protected]"
          },
          "validationData":null
       },
       "response":{
          "autoConfirmUser":false,
          "autoVerifyEmail":false,
          "autoVerifyPhone":false
       }
    }
    
    {
       "version":"1",
       "region":"us-east-1",
       "userPoolId":"us-east-1_xxxxxxxxxxx",
       "userName":"efce8c11-e0ff-4400-xxxx-xxxxxxxxxx",
       "callerContext":{
          "awsSdkVersion":"aws-sdk-java-1.11.856",
          "clientId":"CLIENT_ID_NOT_APPLICABLE"
       },
       "triggerSource":"PreSignUp_AdminCreateUser",
       "request":{
          "userAttributes":{
             "email_verified":"true",
             "email":"[email protected]"
          },
          "validationData":null
       },
       "response":{
          "autoConfirmUser":false,
          "autoVerifyEmail":false,
          "autoVerifyPhone":false
       }
    }
    
    {
       "version":"1",
       "region":"us-east-1",
       "userPoolId":"us-east-1_xxxxxx",
       "userName":"Google_xxxxxxxxxxxxxxxxxxxxx",
       "callerContext":{
          "awsSdkVersion":"aws-sdk-unknown-unknown",
          "clientId":"xxxxxxxxxxxxxxxxxxxxxxxxxx"
       },
       "triggerSource":"PreSignUp_ExternalProvider",
       "request":{
          "userAttributes":{
             "email_verified":"false",
             "cognito:email_alias":"",
             "cognito:phone_number_alias":"",
             "email":"[email protected]"
          },
          "validationData":{
             
          }
       },
       "response":{
          "autoConfirmUser":false,
          "autoVerifyEmail":false,
          "autoVerifyPhone":false
       }
    }