【问题标题】:Creating a User With cognito User Pool in node.js在 node.js 中使用 cognito 用户池创建用户
【发布时间】:2018-07-27 22:39:04
【问题描述】:

我正在尝试使用 cognito 用户池 js sdk 在 cognito 用户池中创建用户。通过关注this tutorial,我能够在前端成功地做到这一点。但是,我想在后端使用 node.js 来做。我正在尝试像这样包含 sdk

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

但是我正在使用的东西的节点是定义的。例如AWSCognito.config.region = 'us-east-1'; 不起作用,因为 AWSCognito 似乎没有属性 config.region。我用AWSCognitoSDK 尝试了它,但是当我将它记录到控制台时它作为一个空对象出现,所以我并没有真正期望它能够正常工作。我尝试使用以下答案中的解决方案,但我无法让它工作 AWS Cognito unauthenticated login error (window is not defined) [JS]

如果有人可以帮助我包含 sdk 并让这些行在节点中工作,我将不胜感激

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

注意:我也尝试通过 npm 安装 amazon-cognito-identity-js,但遇到了同样的问题

---更新---

我根据 Patrick 的回答安装了 npm。这是我得到的错误

    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

当我控制台记录此 AWSCognito 对象 const AWSCognito = require('amazon-cognito-identity-js'); 时,以下是显示的属性

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

【问题讨论】:

    标签: javascript node.js amazon-web-services amazon-cognito


    【解决方案1】:

    通过在项目的根目录中执行以下命令,使用 Node.js 包管理器 (npm) 安装 amazon-cognito-identity-js 模块:

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

    这会将模块放在一个名为 node_modules 的特殊目录中。

    然后你可以在你的代码中使用 require 而不需要指定路径,只要 npm 包名就足够了,Node.js 会自动解析路径:

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

    【讨论】:

    • 感谢您的快速回复。不幸的是,这似乎不起作用。我也用这种方式更新了帖子时遇到的错误。请检查帖子以获取更新。
    【解决方案2】:

    我能够让那条线路正常工作。我只是将其更改为new AWSCognito.CognitoUserPool(poolData) 而不是new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData),并且它起作用了。

    【讨论】:

      猜你喜欢
      • 2017-12-03
      • 2019-09-04
      • 1970-01-01
      • 2018-05-11
      • 2018-06-27
      • 2021-08-13
      • 2017-04-19
      • 1970-01-01
      • 2019-09-13
      相关资源
      最近更新 更多