用户注册时发送电子邮件 - AWS Cognito 联合身份

2024-03-13

当新用户注册时,如何发送电子邮件/触发 lambda 函数?

在“编辑身份池”下我只找到了一个同步触发器。 如果我理解正确的话:每次用户同步他的数据时都会触发这个......
有没有办法仅在“初始”同步或为用户创建某个数据集时触发 lambda 函数?

Edit:
更具体地说:我确实使用 JS SDK 通过 lambda 创建用户。我将开发人员身份验证与我自己的 oauth2 流程一起使用。我不知道如何区分授予访问权限的用户,例如第一次通过谷歌,有人第二次这样做。带有访问代码的 json 对我来说是一样的......也许我错了。

还使用getOpenIdTokenForDeveloperIdentity我不知道如何区分 Cognito 新的 ID 和 Cognito 已经知道的 ID。

编辑2: 更准确地说: 我正在这个项目的基础上进行构建:https://github.com/laardee/serverless-authentication-boilerplate/blob/master/authentication/lib/storage/usersStorage.js https://github.com/laardee/serverless-authentication-boilerplate/blob/master/authentication/lib/storage/usersStorage.js

这是我目前将用户保存到 cognito 的方法。 我确实为第一次用户以及第n次用户运行此代码。我的问题是我不知道如何区分...

const saveCognito = (profile) => new Promise((resolve, reject) => {
  if (profile) {
    cognitoidentity.getOpenIdTokenForDeveloperIdentity({
      IdentityPoolId: process.env.COGNITO_IDENTITY_POOL_ID,
      Logins: {
        // profile.userId = encrypted id of the e.g. google oauth2 id
        [process.env.COGNITO_PROVIDER_NAME]: profile.userId 
      }
    }, (err, dat) => {
      if (err) {
        reject(err);
      } else {
        var list_params = {
          DatasetName: 'user-data', /* dataset name */
          IdentityId: dat.IdentityId, /* cognito id */
          IdentityPoolId: process.env.COGNITO_IDENTITY_POOL_ID
        };
        cognitosync.listRecords(list_params, function(err, data) {
          if (err) {
            reject(err); // an error occurred
          } else {

            var RecordPatches = //[Parts of the i want to write to the user]
            // SyncSessionToken is returned by the cognitosync.listRecords call
            list_params["SyncSessionToken"] = data.SyncSessionToken; 
            list_params["RecordPatches"] = RecordPatches;

            cognitosync.updateRecords(list_params, function(err, update_data) {
              if (err){
                reject(err);
              } else {
                resolve();
              }
            });
          }
        });
      }
    });
  } else {
    reject('Invalid profile');
  }
});

因此,Cognito 目前不支持这种开箱即用的功能。您的说法是正确的,唯一会触发 Lambda 函数的内置 Cognito 事件是“同步触发器”事件。每次 Cognito IdentityId 将其部分数据同步到 Cognito Sync 云数据存储时,都会触发此同步事件。

此事件与 Cognito 联合身份创建新的 IdentityId 无关。

理论上你可以:

  • 在用户登录之前,在 IdentityPool 上运行 list-identities 调用 在。
  • 登录用户。检查已提供给用户的 IdentityId 是否存在于您在登录之前检索到的列表中 这会告诉你他们的身份是否 给出的信息在此登录之前已存在。
  • 根据这些信息你可以 决定是否以编程方式调用 Lambda 来自您的应用程序的功能。

上述设置会很复杂,因为出于安全原因,您需要在服务器端维护此服务。 list-identities 调用需要 AWS 凭证才能调用。我怀疑您是否希望在针对未经身份验证的用户的 IAM 策略中包含该调用的权限。

除了上述之外,目前您无能为力。 为此,您需要设置一个 DynamoDB 表(或一些类似的低延迟数据存储),您可以在其中维护 IdentityId 列表的状态,然后在您登录用户时查询此服务/存储以将新登录与预先存在的列表。

如果这对您的使用案例至关重要,我建议您前往 AWS Support,并创建一个案例,您可以将其记录为功能请求。

https://aws.amazon.com/premiumsupport/ https://aws.amazon.com/premiumsupport/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用户注册时发送电子邮件 - AWS Cognito 联合身份 的相关文章

随机推荐