javascript - 如何以编程方式在 Cognito 用户池中创建用户?

标签 javascript amazon-web-services amazon-cognito aws-sdk-js

AWS 文档表明管理员可以使用 API 在 AWS Cognito 中创建用户池用户。

这是我所指的文档:https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminCreateUser.html

但是文档提供的细节很少,甚至没有提供如何完成的示例。它没有提及要调用什么端点、要使用什么 SDK 函数或任何与身份验证相关的内容等。

有没有人有直接从您的代码创建新用户的经验?

最佳答案

如果您遵循开发文档 (https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html),更具体地说是“注册”功能,这实际上非常容易。

来自文档:

var params = {
  ClientId: 'STRING_VALUE', /* required */
  Password: 'STRING_VALUE', /* required */
  Username: 'STRING_VALUE', /* required */
  AnalyticsMetadata: {
    AnalyticsEndpointId: 'STRING_VALUE'
  },
  SecretHash: 'STRING_VALUE',
  UserAttributes: [
    {
      Name: 'STRING_VALUE', /* required */
      Value: 'STRING_VALUE'
    },
    /* more items */
  ],
  UserContextData: {
    EncodedData: 'STRING_VALUE'
  },
  ValidationData: [
    {
      Name: 'STRING_VALUE', /* required */
      Value: 'STRING_VALUE'
    },
    /* more items */
  ]
};
cognitoidentityserviceprovider.signUp(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

使用它,创建用户很简单(Lambda 中的示例,但可以很容易地自行修改为 JS):

'use strict'
var AWS = require('aws-sdk');
var resp200ok = { statusCode: 200, headers: {'Content-Type': 'application/json'}, body: {} };
var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18'});
// ^ Hard to find that this is the way to import the library, but it was obvious in docs

exports.handler = function(event, context, callback){
    var params = {
        ClientId: 'the App Client you set up with your identity pool (usually 26 alphanum chars)',
        Password: 'the password you want the user to have (keep in mind the password restrictions you set when creating pool)',
        Username: 'the username you want the user to have',
        UserAttributes:[ {
            {
                Name: 'name', 
                Value: 'Private'
            }, 
            {
                Name: 'family_name', 
                Value: 'Not-Tellinglol'
            },
        }],
    };

    cognitoidentityserviceprovider.signUp(params, function(err, data) {
        if (err){ console.log(err, err.stack); }
        else{ resp200ok.body = JSON.stringify(data); callback(null, resp200ok); }
    });
};

您在 Cognito 池设置中设置为必需 的任何内容都必须位于 UserAttributes 部分(通常电子邮件默认为必需,请检查您的是否为必需)。您可以在(Cognito pool) General Settings -> App Clients -> Show Details -> Set Read/Write -> (list of things) 中找到你可以赋值的事物列表,在这里你可以添加自定义属性(例如,如果您想指定用户来自哪个城市,或者您是否想添加其他任何内容(字符串/数字))。

为自定义字段赋值时,UserAttributes 中的“名称”将为“custom:whatever”,因此如果自定义字段为“city”,则名称为“custom:city”。

希望我没有说太多显而易见的事情,但这些是我花了一段时间才弄清楚分解的 SO 信息和 AWS 文档的东西,我想我会把它们放在一起。

关于javascript - 如何以编程方式在 Cognito 用户池中创建用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48327598/

相关文章:

javascript - YouTube iFrame API "setPlaybackQuality"或 "suggestedQuality"不起作用

javascript - 使用 Animate.css 在 HTML 中制作多字动画

java - 如何为 Amazon Web Service 重构 Java Web 应用程序?

amazon-web-services - DynamoDB : Remove old entries automatically

postgresql - 加载到 AWS Athena 时如何使 csv 文件的第一行成为列名?

Azure AD SSO SingleLogoutService 在联合元数据中没有 POST 绑定(bind)

javascript - 页面加载完成后预加载大图像

amazon-web-services - AWS CloudFormer 支持哪些 AWS 服务?

amazon-cognito - Cognito 用户池 - 刷新 token 是否与客户端 ID 相关联?

javascript - 带参数的数组平均值 (...rest)