AWS Cognito 自定义属性的解决方法?使用 JavaScript 与 Angular

A Work-Around For AWS Cognito Custom Attributes? Using JavaScript With Angular

目前 AWS 的自定义属性设置不当。例如,您无法在各种 JS SDK api 返回的用户对象中找到您在控制台中输入的自定义字段,除非您在其中有值。 (我已经检查了自定义属性的读/写框。)为此,您必须使用 CLI 输入值,这对于用户使用 GUI 添加数据的真实应用程序是行不通的。

此外,adminCreateUser api 文档指出: "For custom attributes, you must prepend the custom: prefix to the attribute name."

显然您应该能够在创建时添加自定义字段,但祝您好运。至少在 JavaScript 中,我还没有找到一种方法让它接受 custom:whatever 而不会导致应用程序构建崩溃。我无法找到任何方法将这些字段与任何 api 一起使用,因为自定义:被 JS 拒绝。

有没有人找到解决此问题的变通方法/技巧?

这在自定义的 adminCreateUser api 中不起作用:但对其他人没问题:

const createUserParams = {
          UserPoolId: this.cognitoUserPoolID,
          Username: enteredData.user_name,
          DesiredDeliveryMediums: ['EMAIL'],
          ForceAliasCreation: false,
          MessageAction: 'SUPPRESS',
          UserAttributes: [
            {
              Name: 'custom:user_id',
              Value: enteredData.custom:user_id
            },
            {
              Name: 'given_name',
              Value: enteredData.given_name
            },  ...

调用 listUsers api 在控制台中给出错误消息。

Error in getCognitoUsers:  Error: One or more requested attributes do not exist.

listUsers api 需要这些参数:

return new Observable(observer => {
      const listUsersParams = {
        AttributesToGet: [
          'custom:user_id',
          'given_name',
          'family_name',
          'locale',
          'email',
        ],
        // Filter: '',
        UserPoolId: this.cognitoApisService.cognitoUserPoolID
      };

      const cognitoidentityserviceprovider = new CognitoIdentityServiceProvider(AWSconfig);

      cognitoidentityserviceprovider.listUsers(listUsersParams, function (err, userData) {

这一行有问题:Value: enteredData.custom:user_id。问题是您正在尝试使用字符串 custom: 更改变量名称。相反,您将 custom: 添加到名称值。

{
    Name: 'custom:user_id',
    Value: enteredData.user_id
},

John Hanley 通过将 custom: 附加到 属性 名称解决了部分问题。我无法找到 JavaScript 拒绝 AttributesToGet: [ 'custom:user_id' 的解决方案,因为这不是必需的,所以我在请求参数中删除了 AttributesToGet 并且所有属性都在响应对象中返回。

因为 JavaScript 真的不喜欢对象中的冒号 属性 我创建了这个解决方法:

let userID = data['custom:user_id'];

如果遍历数组,这很有用:

const user_id = usersArray[i]['custom:user_id'];

要修复响应对象以访问数据 table 只需将新字段和值添加到对象。

usersArray[i].user_id = usersArray[i]['custom:user_id'];

之后只需访问点符号中的 属性。