首页 > 解决方案 > Cognito 从 Pre Sign-up Lambda Trigger 发送什么样的事件

问题描述

PreSignup使用以下代码创建了一个 Lambda 函数以与 Cognito Pre-SignUp 触发器一起使用:

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;
}

我宁愿指定事件的类型,而不是设置eventto 。anyfunction PreSignup(event: any)

我试图这样指定它APIGatewayProxyEvent

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

但它不起作用,因为eventCognito Pre Sign-Up 触发器发送的不是一种APIGatewayProxyEvent类型。这是什么活动?

后来编辑:

以下是 Cognito PreSignUp Trigger 推送的复制/粘贴事件:

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

下面是这次推送的另一个 AWS 事件的示例AWS EventBridge(类似于 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 }
}

标签: javascripttypescriptamazon-web-servicesamazon-dynamodbamazon-cognito

解决方案


PreSignUp_SignUp:您的标准 Cognito 池(用户名/密码)注册

PreSignUp_AdminCreateUser:当使用 admin 方法创建用户时(要清楚,不是在您创建管理员时,而是当您是管理员时创建不同的用户)。

PreSignUp_ExternalProvider:如果您有第三方注册,例如 Google 或 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":"xxxxxxxxxxxxxxx@gmail.com"
      },
      "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":"xxxxxx@xxxxxxx.com"
      },
      "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":"xxxxxx@xxxxxx.com"
      },
      "validationData":{
         
      }
   },
   "response":{
      "autoConfirmUser":false,
      "autoVerifyEmail":false,
      "autoVerifyPhone":false
   }
}

推荐阅读