AWS Cognito通过管理Node.js创建用户

时间:2019-01-15 06:10:57

标签: node.js amazon-cognito

我正在使用javascript sdk amazon-cognito-identity-js,并且尝试在nodejs中的cognito中创建用户,但错误即将来临 下面是我的代码:-

    var AWS = require("aws-sdk")
    var params = {
    UserPoolId: "id",
    Username: req.body.username,
    DesiredDeliveryMediums: ["EMAIL"],
    ForceAliasCreation: false,
    TemporaryPassword: req.body.password,
    UserAttributes: [
      { Name: "name", Value: req.body.name },
      { Name: "email", Value: req.body.user_email}
    ],
  };
     let client = new AWS.CognitoIdentityServiceProvider();


client.adminCreateUser(params, function(err, data) {
    if (err) {
        console.log("EE",err);
      //  reject(err);
    } else {
        console.log("DDD",data);
        //resolve(data);
    }
})

但是我使用代码得到此错误:-

   EE { UnknownError: Not Found
   at Request.extractError (/var/task/node_modules/aws- 
  sdk/lib/protocol/json.js:48:27)
  }

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

这是解决方案。

先决条件 如果要在aws-sdk中使用凭据(手动过程)  1.创建IAM用户,然后为您的用户分配一个Cognito角色。  2.为该用户生成访问密钥和秘密密钥。  3.在aws-sdk中使用该访问密钥和秘密密钥。

喜欢这个

let AWS = require("aws-sdk");
AWS.config.update({
    accessKeyId: "YOURKEY",
    secretAccessKey: "YOURSECRET",
    region: "YOURREGION"
});

创建 CognitoIdentityServiceProvider 类的对象

const COGNITO_CLIENT = new AWS.CognitoIdentityServiceProvider({
  apiVersion: "2016-04-19",
  region: "us-east-1"
});


var poolData = {
    UserPoolId: "XXXXXXXXX",
    Username: "kurulezomi@themailpro.net",
    DesiredDeliveryMediums: ["EMAIL"],
    TemporaryPassword: "Abc@321",
    UserAttributes: [
      {
        Name: "email",
        Value: "kuri@themailpro.net"
      },
      {
        Name: "email_verified",
        Value: "true"
      }
    ]
  };
  COGNITO_CLIENT.adminCreateUser(poolData, (error, data) => {
    console.log(error);
    console.log(data);
    callback(null, {
      statusCode: 200,
      body: JSON.stringify(data)
    });
  });

否则,您无需在 AWS.config 部分中设置凭据,就可以直接将IAM角色分配给EC2实例。