简体   繁体   English

在node.js中使用Cognito用户池创建用户

[英]Creating a User With cognito User Pool in node.js

I'm trying to use the cognito user pool js sdk to create a user in cognito user pool. 我正在尝试使用cognito用户池js sdk在cognito用户池中创建用户。 I was able to successfully do so on the front end by following this tutorial . 通过遵循本教程,我可以在前端成功完成此操作 However, I wanted to do it in the backend with node. 但是,我想在带有节点的后端中执行此操作。 I am trying to include the sdk like this 我正在尝试包括这样的SDK

const AWSCognito = require('./amazon-cognito-identity.min.js');
const `AWSCognitoSDK` = require('./aws-cognito-sdk.min.js');

But node of the stuff I am using is defined. 但是我正在使用的东西的节点已定义。 For example AWSCognito.config.region = 'us-east-1'; 例如, AWSCognito.config.region = 'us-east-1'; doesnt work because AWSCognito doesnt seem to have the attribute config.region. 不起作用,因为AWSCognito似乎没有属性config.region。 I tryed it with AWSCognitoSDK but that comes as an empty object when I log it to the console, so I wasn't really expecting that to work anyways. 我使用AWSCognitoSDK但是当我将其记录到控制台时,它作为一个空对象出现,因此我并不是真的希望它能正常工作。 I tried using the solution in the below answer, but I couldn't get it to work AWS Cognito unauthenticated login error (window is not defined) [JS] 我尝试在以下答案中使用该解决方案,但无法使它运行AWS Cognito未经身份验证的登录错误(未定义窗口)[JS]

I'd appreciate it if someone could help me in including the sdk and getting these lines to work in node 如果有人可以帮助我包括SDK和使这些行在Node中起作用,我将不胜感激

var poolData = {
    UserPoolId : '...', // your user pool id here
    ClientId : '...' // your app client id here
};
var userPool = 
new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);

Note: I also tried installing amazon-cognito-identity-js through npm but faced the same issues 注意:我也尝试通过npm安装amazon-cognito-identity-js,但是遇到了同样的问题

---UPDATE--- -更新-

I did the npm install based on Patrick's answer. 我根据Patrick的回答进行了npm安装。 Here is the errror I get 这是我得到的错误

    let userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
                                                             ^

TypeError: Cannot read property 'CognitoUserPool' of undefined
    at Object.<anonymous> (/Users/bbhakhrani/projects/nodekb/app.js:19:62)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
    at startup (bootstrap_node.js:190:16)
    at bootstrap_node.js:662:3

When I console.log this AWSCognito object, const AWSCognito = require('amazon-cognito-identity-js'); 当我console.log这个AWSCognito对象时, const AWSCognito = require('amazon-cognito-identity-js'); , here are the properties that show ,这是显示的属性

    { __esModule: true,
  AuthenticationDetails: [Getter],
  AuthenticationHelper: [Getter],
  CognitoAccessToken: [Getter],
  CognitoIdToken: [Getter],
  CognitoRefreshToken: [Getter],
  CognitoUser: [Getter],
  CognitoUserAttribute: [Getter],
  CognitoUserPool: [Getter],
  CognitoUserSession: [Getter],
  CookieStorage: [Getter],
  DateHelper: [Getter] }

Install the amazon-cognito-identity-js module using the Node.js package manager (npm) by executing this command in your project's root directory: 通过在项目的根目录中执行以下命令,使用Node.js包管理器(npm)安装amazon-cognito-identity-js模块:

npm install --save amazon-cognito-identity-js

This will put the module in a special directory named node_modules . 这会将模块放在名为node_modules的特殊目录中。

You can then use require in your code without specifying a path, just the npm package name suffices, Node.js will automatically resolve the path: 然后,您可以在代码中使用require而不指定路径,仅npm软件包名称就足够了,Node.js会自动解析路径:

const AWSCognito = require('amazon-cognito-identity-js');

I was able to get that line to work. 我能够使那条线工作。 I just changed it to new AWSCognito.CognitoUserPool(poolData) instead of new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData) , and it worked. 我只是将其更改为new AWSCognito.CognitoUserPool(poolData)而不是new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData) ,并且它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM