首页 > 解决方案 > AWS Lambda UserMigration_ForgotPassword 触发器 | 不迁移用户

问题描述

因此,我试图将存储在 dynamodb 中的用户迁移到 cognito 用户池,但UserMigration_ForgotPassword触发器根本不起作用。我到处搜索,没有找到解决我问题的方法。我也按照文件来信,仍然没有。这是我的代码在 lambda 中的样子:

else if (event.triggerSource === 'UserMigration_ForgotPassword') {
  console.log('forgot password trigger working')
  user = await findUser(event.userName) 
    if (user) {
     console.log('user found!')
     const { Item } = user 
     console.log(Item)

     event.response.userAttributes = {
       'email': Item.email,
       'email_verified': Item.emailVerified
     }
     event.response.messageAction = 'SUPPRESS'
     console.log(event)
     context.succeed(event);
   } else {
     console.log('User does not exists')
     callback(Error('Bad Password'))
 } 

这是我在 cloudwatch 上得到的:

2020-09-04T12:13:12.895Z    786f09ce-91b7-4051-ade6-************    INFO    forgot password trigger working
2020-09-04T12:13:12.975Z    786f09ce-91b7-4051-ade6-************    INFO    user found!
2020-09-04T12:13:12.977Z    786f09ce-91b7-4051-ade6-************    INFO    {
  emailVerified: true,
  password: '************************',
  salt: '',
  phone_number: '+1111111111',
  internal_user_id: '*****',
  username: 'name@email.com',
  email: 'name@email.com',
  name: 'name'
}
2020-09-04T12:13:12.977Z    786f09ce-91b7-4051-ade6-************    INFO    {
  version: '1',
  triggerSource: 'UserMigration_ForgotPassword',
  region: 'us-****-*',
  userPoolId: 'us-****-*_********',
  userName: 'name@email.com',
  callerContext: {
    awsSdkVersion: 'aws-sdk-unknown-unknown',
    clientId: '**************'
  },
  request: { password: null, validationData: null, userAttributes: null },
  response: {
    userAttributes: {
      email: 'name@email.com',
      email_verified: true
    },
    forceAliasCreation: null,
    messageAction: 'SUPPRESS',
    desiredDeliveryMediums: null
  }
}

我可以安全地得出结论,触发器正在工作,它能够从 dynamodb 获取用户,并且能够构建正确的响应对象。但是,仍然由于某种原因,它无法导入用户。这是我得到的错误:

{code: "UserNotFoundException", name: "UserNotFoundException", message: "Exception migrating user in app client *************************"}

此外,lambda 触发器可以访问 dynamodb,它可以调用函数,它可以登录 cloudwatch,当然,它连接到 cognito 中的触发器。如果您想知道,我正在使用无服务器框架。

最后,我想指出它UserMigration_Authentication工作正常,这使它变得更加奇怪(至少对我而言)。

很高兴知道问题的根源。

非常感谢!

编辑 1

我不确定这是否与当前的问题有关,但很可能是。我为防止做的另一件事是UserNotFoundException“启用” PreventUserExistenceErrors。这是文档的链接。仍然出现错误。

标签: node.jsamazon-web-servicesaws-lambdaamazon-cognitoaws-serverless

解决方案


关于触发器为什么不起作用的问题是因为ClientMetadata密钥中没有传递任何内容。出于某种原因,我发现文档不够清晰。所以在我的情况下:

$result = $client->forgotPassword([
    'AnalyticsMetadata' => [
        'AnalyticsEndpointId' => '<string>',
    ],
    'ClientId' => '<string>', 
    'ClientMetadata' => ['<string>', ...], // <- This needs to be filled
    'SecretHash' => '<string>',
    'UserContextData' => [
        'EncodedData' => '<string>',
    ],
    'Username' => '<string>', 
]);

ClientMetadatakey被填满时,会触发预注册、自定义消息、用户迁移lambda函数。

我把这个留在这里,以防有人需要帮助!


推荐阅读