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

2

5

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 }
}
Vitriolic answered 28/1, 2021 at 18:31 Comment(1)
You can find our by printing the event any, as you are are already doing. So just need to trigger that event and check the console/logs for json of that event.Leolaleoline
G
6

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
   }
}
Genie answered 31/1, 2021 at 7:30 Comment(2)
I don't understand why they would not include some example payloads for each use-case in the official docs.Midshipman
I know.... why wouldn't they?! blows my mind. Also OP is the real MVP for posting these.Doe
G
4

What kind of event is it?

It is a PreSignUpTriggerEvent. You can import it from "aws-lambda".

If you also want to set the correct type of the context and callback objects automatically, you can instead set the type of the whole handler function to PreSignUpTriggerHandler and all of the arguments would have the appropriate types.

Example:

import {PreSignUpTriggerHandler} from "aws-lambda";

export const handler: PreSignUpTriggerHandler = async (event, context, callback) => {
   // code
}
  • event is of type PreSignUpTriggerEvent
  • context is of type Context
  • callback is of type Callback<any>

Another example:

import {PreSignUpTriggerEvent, Context, Callback} from "aws-lambda";

export const handler = async (event: PreSignUpTriggerEvent, context: Context, callback: Callback<any>) => {
   // code
}
Grosgrain answered 22/4, 2023 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.