0

I'm trying to use the cognito user pool js sdk to create a user in cognito user pool. 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

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'; doesnt work because AWSCognito doesnt seem to have the attribute 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. 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]

I'd appreciate it if someone could help me in including the sdk and getting these lines to work in 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

---UPDATE---

I did the npm install based on Patrick's answer. 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');, 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] }
user1807157
  • 101
  • 2
  • 9

2 Answers2

0

Install the amazon-cognito-identity-js module using the Node.js package manager (npm) by executing this command in your project's root directory:

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

This will put the module in a special directory named 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:

const AWSCognito = require('amazon-cognito-identity-js');
Patrick Hund
  • 19,163
  • 11
  • 66
  • 95
  • Thanks for the quick response. Unfortunately, that didn't seem to work. I updated the post with the error I get when doing it this way as well. Please check the post for the update. – user1807157 Feb 19 '18 at 15:24
0

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.

user1807157
  • 101
  • 2
  • 9